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, system-defined and custom metadata are supported.
System-defined metadata consists of system-controlled metadata and user-controlled metadata. The kind of metadata like Last-Modified is controlled by the system and cannot be modified. However, the kind of metadata configured for objects such as ContentLanguage can be modified by calling APIs.
This API adds, modifies, or deletes metadata of objects in a bucket.
Restrictions
- To configure object metadata, you must be the bucket owner or have the required permission (obs:object:ModifyObjectMetaData in IAM or ModifyObjectMetaData in a bucket policy). For details, see Introduction to OBS Access Control, IAM Custom Policies, and Configuring an Object Policy.
- If versioning is enabled for a bucket, you can set metadata for objects of the latest version but cannot set metadata for historical objects.
- You cannot set metadata for Archive objects.
Method
ObsClient.setObjectMetadata(params)
Request Parameters
Parameter |
Type |
Mandatory (Yes/No) |
Description |
---|---|---|---|
Bucket |
string |
Yes |
Explanation: Bucket name Restrictions:
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 |
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 ID, for 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:
Value range: None Default value: None |
StorageClass |
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:
Value range: None Default value: None |
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
Type |
Description |
---|---|
NOTE:
This API returns a Promise response, which requires the Promise or async/await syntax. |
Explanation: Returned results. For details, see Table 4. |
Parameter |
Type |
Description |
---|---|---|
CommonMsg |
Explanation: Common information generated after an API call is complete, including the HTTP status code and error code. For details, see Table 5. |
|
InterfaceResult |
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. |
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. |
Parameter |
Type |
Description |
---|---|---|
RequestId |
string |
Explanation: Request ID returned by the OBS server |
MetadataDirective |
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 the content encoding format when an object is 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:
|
StorageClass |
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:
|
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). |
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(); |
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.