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

Put Object


This section details how to upload new files or update existing objects within your DID Space. The putObject command facilitates storing data along with optional metadata and a content hash.

For an overview of object management, refer to the Object commands. For a complete list of all SDK functionalities, see the Commands Reference.

putObject Command#

Use the putObject command to upload content to a specified key (path) in your DID Space. This command supports various data types for content and allows you to include associated metadata and a content hash.

Parameters#

Name

Type

Description

key

string

The unique identifier for the object within your DID Space. This can represent a file name or a path within a folder structure.

data

string, Buffer, ReadableStream

Optional. The content of the object to be stored. This can be a string, a Node.js Buffer, or a ReadableStream for larger files.

hash

string

Optional. The content hash of the object, ideally an IPFS v1 hash, used for content integrity verification.

metadata

Record<string, any>

Optional. A key-value map for storing custom metadata associated with the object.

Returns#

Upon successful execution, the command returns a status object.

Name

Type

Description

statusCode

number

The HTTP status code of the operation (e.g., 200 for success).

statusMessage

string

A descriptive message indicating the status of the operation.

Example#

import { SpaceClient } from '@did-spaces/client';
import { createReadStream } from 'fs';
import { resolve } from 'path';

async function putObjectExample() {
// Initialize the SpaceClient with your endpoint and wallet
const client = new SpaceClient({
endpoint: 'YOUR_DID_SPACE_ENDPOINT',
wallet: {
did: 'YOUR_WALLET_DID',
secretKey: 'YOUR_WALLET_SECRET_KEY',
},
});

const objectKey = 'documents/my-document.txt';
const textContent = 'Hello, DID Spaces! This is a test document.';
const filePath = resolve(__dirname, 'example.json'); // Path to a local file

try {
// 1. Upload a string as object content
const stringUploadResult = await client.putObject({
key: objectKey,
data: textContent,
metadata: { author: 'DID Spaces User' },
hash: 'QmStringHashExample',
});
console.log(`String upload result: ${stringUploadResult.statusCode} - ${stringUploadResult.statusMessage}`);

// 2. Upload a file using a ReadableStream
// Ensure 'example.json' exists in your project directory for this example
const fileStream = createReadStream(filePath);
const fileUploadResult = await client.putObject({
key: 'data/config.json',
data: fileStream,
metadata: { type: 'configuration', version: '1.0' },
hash: 'QmFileHashExample',
});
console.log(`File stream upload result: ${fileUploadResult.statusCode} - ${fileUploadResult.statusMessage}`);

} catch (error) {
console.error('Error putting object:', error.message);
}
}

putObjectExample();

This example demonstrates two ways to use the putObject command:

  1. Uploading a string: A simple string textContent is uploaded to documents/my-document.txt with custom metadata and a placeholder hash. This is suitable for small text files or configuration data.
  2. Uploading a file via ReadableStream: A local file (example.json) is read as a stream and uploaded to data/config.json. Using a ReadableStream is recommended for uploading larger files efficiently, as it avoids loading the entire file into memory.

The key parameter determines the object's path in your DID Space, acting like a file path. The data parameter accepts various content types, making it flexible for different use cases. The optional metadata parameter allows you to associate custom key-value pairs with your object, while hash provides a mechanism for content addressability and integrity checks.


Now that you know how to upload objects, you might want to learn how to retrieve them. Proceed to the Get section to understand how to fetch object content. To manage or remove objects, refer to Delete or List.