Help Center/ Object Storage Service/ SDK Reference/ Node.js/ Object Upload (SDK for Node.js)/ Configuring Object Metadata (SDK for Node.js)
Updated on 2024-11-13 GMT+08:00

Configuring Object Metadata (SDK for Node.js)

If you have any questions during development, post them on the Issues page of GitHub.

Function

Object metadata contains a set of name-value pairs that are used for describing and managing objects.

Currently, only the system-defined metadata is supported. System-defined metadata consists of system-controlled metadata and user-controlled metadata.

This API sends a HEAD request for obtaining object metadata.

Restrictions

Method

ObsClient.setObjectMetadata(params)

Request Parameters

Table 1 List of request parameters

Parameter

Type

Mandatory (Yes/No)

Description

Bucket

string

Yes

Explanation:

Bucket name.

Restrictions:

  • A bucket name must be unique across all accounts and regions.
  • A bucket name:
    • Must be 3 to 63 characters long and start with a digit or letter. Lowercase letters, digits, hyphens (-), and periods (.) are allowed.
    • Cannot be formatted as an IP address.
    • Cannot start or end with a hyphen (-) or period (.).
    • Cannot contain two consecutive periods (..), for example, my..bucket.
    • Cannot contain a period (.) and a hyphen (-) adjacent to each other, for example, my-.bucket or my.-bucket.
  • If you repeatedly create buckets with the same name in the same region, no error will be reported, and the bucket attributes comply with those set in the first creation request.

Value range:

The value can contain 3 to 63 characters.

Key

string

Yes

Explanation:

Object name. An object is uniquely identified by an object name in a bucket. An object name is a complete path of the object that does not contain the bucket name.

For example, if the address for accessing the object is examplebucket.obs.eu-west-101.myhuaweicloud.com/folder/test.txt, the object name is folder/test.txt.

Restrictions:

None

Value range:

The value can contain 1 to 1,024 characters.

Default value:

None

MetadataDirective

MetadataDirectiveType

Yes

Explanation:

Policy for copying the source object's attributes

Restrictions:

None

Value range:

See Table 7.

Default value:

None

VersionId

string

No

Explanation:

Object version. Example: G001117FCE89978B0000401205D5DC9A

Restrictions:

None

Value range:

The value must contain 32 characters.

Default value:

None

CacheControl

string

No

Explanation:

Cache-Control header in the response. It specifies the cache behavior of the web page when an object is downloaded.

Restrictions:

None

Value range:

See the Cache-Control values defined in HTTP.

Default value:

None

ContentDisposition

string

No

Explanation:

Content-Disposition header in the response. It specifies the name of an object when it is downloaded.

Restrictions:

None

Value range:

See the Content-Disposition values defined in HTTP.

Default value:

None

ContentEncoding

string

No

Explanation:

Content-Encoding header in the response. It specifies the content encoding format when an object is downloaded.

Restrictions:

None

Value range:

See the Content-Encoding values defined in HTTP.

Default value:

None

ContentLanguage

string

No

Explanation:

Content-Language header in the response. It specifies what language the object content is in when being downloaded.

Restrictions:

None

Value range:

See the Content-Language values defined in HTTP.

Default value:

None

ContentType

string

No

Explanation:

Content-Type header in the response. It specifies the file type of an object when it is downloaded.

Restrictions:

None

Value range:

See What Is Content-Type (MIME)? (SDK for Node.js)

Default value:

None

Expires

string

No

Explanation:

Expires header in the response. It specifies the cache expiration time of the web page when the object is downloaded.

Restrictions:

None

Value range:

See the Expires values defined in HTTP.

Default value:

None

WebsiteRedirectLocation

string

No

Explanation:

If the bucket is configured with website hosting, the request for obtaining the object can be redirected to another object in the bucket or an external URL.

The request is redirected to object anotherPage.html in the same bucket:

WebsiteRedirectLocation:/anotherPage.html

The request is redirected to an external URL:

WebsiteRedirectLocation:http://www.example.com/

OBS obtains the specified value from the header and stores it in the object metadata WebsiteRedirectLocation.

Restrictions:

  • The value must start with a slash (/), http://, or https:// and cannot exceed 2 KB.
  • OBS only supports redirection of objects that are in the root directory.

Value range:

None

Default value:

None

StorageClass

StorageClassType

No

Explanation:

Storage class of the object.

Restrictions:

None

Value range:

See Table 2.

Default value:

None

Metadata

object

No

Explanation:

Custom metadata of the object. You can add a header starting with x-obs-meta- in the request to define metadata. The custom metadata will be returned in the response when you retrieve the object or query the object metadata.

Restrictions:

  • The custom metadata cannot exceed 8 KB in total. To measure the size, calculate the sum of bytes of all UTF-8 encoded keys and values.
  • The custom metadata keys are case insensitive, but are stored in lowercase in OBS. The key values are case sensitive.
  • Both custom metadata keys and their values must conform to US-ASCII standards. If non-ASCII or unrecognizable characters are required, they must be encoded and decoded in URL or Base64 on the client, because the server does not perform such operations.

Value range:

None

Default value:

None

Table 2 StorageClassType

Constant

Default Value

Description

ObsClient.enums.StorageClassStandard

STANDARD

Standard storage class.

Features low access latency and high throughput and is used for storing massive, frequently accessed (multiple times a month) or small objects (< 1 MB) requiring quick response.

ObsClient.enums.StorageClassWarm

WARM

Infrequent Access storage class.

Used for storing data that is semi-frequently accessed (fewer than 12 times a year) but becomes instantly available when needed.

ObsClient.enums.StorageClassCold

COLD

Archive storage class.

Used for storing rarely accessed (once a year) data.

Responses

Table 3 Responses

Type

Description

Table 4

NOTE:

This API returns a Promise response, which requires the Promise or async/await syntax.

Explanation:

Returned results. For details, see Table 4.

Table 4 Response

Parameter

Type

Description

CommonMsg

ICommonMsg

Explanation:

Common information generated after an API call is complete, including the HTTP status code and error code. For details, see Table 5.

InterfaceResult

Table 6

Explanation:

Results outputted for a successful call. For details, see Table 6.

Restrictions:

This parameter is not included if the value of Status is greater than 300.

Table 5 ICommonMsg

Parameter

Type

Description

Status

number

Explanation:

HTTP status code returned by the OBS server.

Value range:

A status code is a group of digits indicating the status of a response. It ranges from 2xx (indicating successes) to 4xx or 5xx (indicating errors). For details, see Status Codes.

Code

string

Explanation:

Error code returned by the OBS server.

Message

string

Explanation:

Error description returned by the OBS server.

HostId

string

Explanation:

Request server ID returned by the OBS server.

RequestId

string

Explanation:

Request ID returned by the OBS server.

Id2

string

Explanation:

Request ID2 returned by the OBS server.

Indicator

string

Explanation:

Error code details returned by the OBS server.

Table 6 SetObjectMetadataOutput

Parameter

Type

Description

RequestId

string

Explanation:

Request ID returned by the OBS server

MetadataDirective

MetadataDirectiveType

Explanation:

Policy for copying the source object's attributes

Value range:

See Table 7.

CacheControl

string

Explanation:

Cache-Control header in the response. It specifies the cache behavior of the web page when an object is downloaded.

ContentDisposition

string

Explanation:

Content-Disposition header in the response. It specifies the name of an object when it is downloaded.

ContentEncoding

string

Explanation:

Content-Encoding header in the response. It specifies how the object is encoded when being downloaded.

ContentLanguage

string

Explanation:

Content-Language header in the response. It specifies what language the object content is in when being downloaded.

ContentType

string

Explanation:

Content-Type header in the response. It specifies the file type of an object when it is downloaded.

Expires

string

Explanation:

Expires header in the response. It specifies the cache expiration time of the web page when the object is downloaded.

WebsiteRedirectLocation

string

Explanation:

If the bucket is configured with website hosting, the request for obtaining the object can be redirected to another object in the bucket or an external URL.

The request is redirected to object anotherPage.html in the same bucket:

WebsiteRedirectLocation:/anotherPage.html

The request is redirected to an external URL:

WebsiteRedirectLocation:http://www.example.com/

OBS obtains the specified value from the header and stores it in the object metadata WebsiteRedirectLocation.

Restrictions:

  • The value must start with a slash (/), http://, or https:// and cannot exceed 2 KB.
  • OBS only supports redirection of objects that are in the root directory.

StorageClass

StorageClassType

Explanation:

Storage class configured when copying the object. If this parameter is not specified, the object inherits the storage class of the bucket.

Value range:

See Table 8.

Metadata

object

Explanation:

Custom metadata of the object. You can add a header starting with x-obs-meta- in the request to define metadata. The custom metadata will be returned in the response when you retrieve the object or query the object metadata.

Restrictions:

  • The custom metadata cannot exceed 8 KB in total. To measure the size, calculate the sum of bytes of all UTF-8 encoded keys and values.
  • The custom metadata keys are case insensitive, but are stored in lowercase in OBS. The key values are case sensitive.
  • Both custom metadata keys and their values must conform to US-ASCII standards. If non-ASCII or unrecognizable characters are required, they must be encoded and decoded in URL or Base64 on the client, because the server does not perform such operations.
Table 7 MetadataDirectiveType

Constant

Default Value

Description

ObsClient.enums.CopyMetadata

COPY

When copying an object, the object's attributes are also copied.

NOTICE:

This value is used only in the API for Copying an Object (SDK for Node.js).

ObsClient.enums.ReplaceMetadata

REPLACE

REPLACE uses the complete header carried in the current request to replace the original one and deletes the metadata that is not specified.

ObsClient.enums.ReplaceNewMetadata

REPLACE_NEW

REPLACE_NEW replaces the metadata that already has a value, assigns a value to the metadata that does not have a value, and retains the metadata that is not specified.

NOTICE:

This value is used only in the API for Configuring Object Metadata (SDK for Node.js).

Table 8 StorageClassType

Constant

Default Value

Description

ObsClient.enums.StorageClassStandard

STANDARD

Standard storage class.

Features low access latency and high throughput and is used for storing massive, frequently accessed (multiple times a month) or small objects (< 1 MB) requiring quick response.

ObsClient.enums.StorageClassWarm

WARM

Infrequent Access storage class.

Used for storing data that is semi-frequently accessed (fewer than 12 times a year) but becomes instantly available when needed.

ObsClient.enums.StorageClassCold

COLD

Archive storage class.

Used for storing rarely accessed (once a year) data.

Code Examples

This example configures metadata for object example/objectname in bucket examplebucket. The MIME type of the object is set to image/jpeg, the storage class is set to Archive, and custom metadata is also configured for the object.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Import the OBS library.
// Use npm to install the client.
const ObsClient = require("esdk-obs-nodejs");
// Use the source code to install the client.
// var ObsClient = require('./lib/obs');

// Create an instance of ObsClient.
const obsClient = new ObsClient({
  // Obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways. Using hard coding may result in leakage.
  // Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/eu/usermanual-ca/ca_01_0003.html.
  access_key_id: process.env.ACCESS_KEY_ID,
  secret_access_key: process.env.SECRET_ACCESS_KEY,
  // (Optional) If you use a temporary AK/SK pair and a security token to access OBS, you are advised not to use hard coding, which may result in information leakage. You can obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways.
  // security_token: process.env.SECURITY_TOKEN,
  // Enter the endpoint corresponding to the region where the bucket is located. EU-Dublin is used here in this example. Replace it with the one currently in use.
  server: "https://obs.eu-west-101.myhuaweicloud.eu"
});

async function setObjectMetadata() {
  try {
    const params = {
      // Specify the bucket name.
      Bucket: "examplebucket",
      // Specify an object. example/objectname is used in this example.
      Key: "example/objectname",
      // Specify a MIME type (image/jpeg in this example) for the object.
      ContentType: "image/jpeg",
      // Specify a storage class (obsClient.enums.StorageClassCold in this example) for the object.
      StorageClass: obsClient.enums.StorageClassCold,
      // Specify custom metadata.
      Metadata: { "property1": "property-value1", "property2": "property-value2" }
    };
    // Configure the metadata for the object.
    const result = await obsClient.setObjectMetadata(params);
    if (result.CommonMsg.Status <= 300) {
      console.log("Set Object(%s)'s metadata successful with bucket(%s)!", params.Key, params.Bucket);
      console.log("RequestId: %s", result.CommonMsg.RequestId);
      return;
    };
    console.log("An ObsError was found, which means your request sent to OBS was rejected with an error response.");
    console.log("Status: %d", result.CommonMsg.Status);
    console.log("Code: %s", result.CommonMsg.Code);
    console.log("Message: %s", result.CommonMsg.Message);
    console.log("RequestId: %s", result.CommonMsg.RequestId);
  } catch (error) {
    console.log("An Exception was found, which means the client encountered an internal problem when attempting to communicate with OBS, for example, the client was unable to access the network.");
    console.log(error);
  };
};

setObjectMetadata();