Used to check for browser translation.
用于检测浏览器翻译。
ブラウザの翻訳を検出する

Use @did-space/client to read and write DID Space

wangqi
2024-04-17 14:40
· edited

Overview#

Most applications hope to read and write data from users on DID Spaces. If you obtain the endpoint of a DID Space, you just need to integrate the @did-space/client SDK to easily read and write the DID Space.

Prerequisite#

Operation#

Install @did-space/client#

yarn add @did-space/client
// or pnpm install @did-space/client

Write data#

Through the following code, you can write data to the user's DID Space.

const { SpaceClient, PutObjectCommand } = require('@did-space/client');

const spaceClient = new SpaceClient({
endpoint, // DID Space endpoint
wallet, // Blocklet Wallet
});

await spaceClient.send(
new PutObjectCommand({
key: 'test.txt',
data: 'hello world!',
})
);

After the data is successfully written, you can go to the root directory of the application component (z8iZmWXt56EZqA5Y2VcmUX6qFhZaj5w7pFQx9) on the DID Space to see the data that was just written.

image.png

Read data#

By using the following code, you can read user data from DID Space.

const { SpaceClient, GetObjectCommand } = require('@did-space/client');

const spaceClient = new SpaceClient({
endpoint, // DID Space endpoint
wallet, // Blocklet Wallet
});

const { data } = await spaceClient.send(
new GetObjectCommand({
key: 'test.txt',
})
);

console.log(await streamToString(data)); // 'hello world!'

After the data is successfully read, a data object will be returned, and you can retrieve the data through the data object.

Delete data#

By using the following code, you can delete user data from the DID Space.

const { SpaceClient, DeleteObjectCommand } = require('@did-space/client');

const spaceClient = new SpaceClient({
endpoint, // DID Space endpoint
wallet, // Blocklet Wallet
});

await spaceClient.send(
new DeleteObjectCommand({
key: 'test.txt',
})
);

Synchronize folders (from local to DID Space)#

const { SpaceClient, DeleteObjectCommand } = require('@did-space/client');

const spaceClient = new SpaceClient({
endpoint, // DID Space endpoint
wallet, // Blocklet Wallet
});

await spaceClient.send(
new SyncFolderPushCommand({
source: localPath,
target: remotePath,
metadata: {},
})
);

Synchronize folder (DID Space to local)#

const { SpaceClient, DeleteObjectCommand } = require('@did-space/client');

const spaceClient = new SpaceClient({
endpoint, // DID Space endpoint
wallet, // Blocklet Wallet
});

await spaceClient.send(
new SyncFolderPullCommand({
source: remotePath,
target: localPath,
})
);

Store NFT#

By using the following code, you can write NFT data into the user's (usually just the Owner) DID Space.

const { SpaceClient, DeleteObjectCommand } = require('@did-space/client');

const spaceClient = new SpaceClient({
endpoint, // DID Space endpoint
wallet, // Blocklet Wallet
});

const data = fs.createReadStream(dataPath)
await spaceClient.send(
new PutNftObjectCommand({
did: 'zjduEsT5qiQr72tVtevGG3GwKjV6J4yHR9fy',
controller: wallet,
chainHost: 'https://beta.abtnetwork.io/explorer',
display: {
key: 'NFT Display.png',
data,
},
})
);

After the NFT data is successfully written, you can go to the file directory of the application on DID Space to see the NFT data that was just written.

image.png


Sticker