Updated on 2023-11-09 GMT+08:00

Modifying Object Metadata

API Description

You can use this API to send a PUT request to the object in a specified bucket to modify its metadata.

Method Definition

ObsClient.setObjectMetadata

Request Parameter

Field

Type

Optional or Mandatory

Description

Bucket

String

Mandatory

Bucket name

Key

String

Mandatory

Object name

VersionId

String

Optional

Object version ID

Origin

String

Optional

Origin of the cross-domain request specified by the pre-request. Generally, it is a domain name.

RequestHeader

String

Optional

HTTP headers in the cross-domain request

MetadataDirective

String

Mandatory

Metadata operation indicator.

The value can be REPLACE_NEW or REPLACE.

REPLACE_NEW: If the metadata of an existing value is replaced, a value is assigned to the metadata that does not have a value. The metadata that is not specified remains unchanged.

REPLACE: Use the header field carried in the current request to replace the original metadata. The metadata that is not specified (except StorageClass) will be deleted.

CacheControl

String

Optional

Specifies the cache behavior of the web page when the object is downloaded.

ContentDisposition

String

Optional

Specifies the name of the object when it is downloaded.

ContentLanguage

String

Optional

Specifies the content language format when an object is downloaded.

ContentEncoding

String

Optional

Specifies the content encoding format when an object is being uploaded.

ContentType

String

Optional

Object file type

Expires

String

Optional

Request expiration time

Metadata

Object

Optional

Customized metadata of the object

StorageClass

String

Optional

Specifies the storage class of an object.

OBS provides three storage classes: Standard (STANDARD), Infrequent Access (WARM), and Archive (COLD). Therefore, values for this element can be STANDARD, WARM, and COLD. The spell of these values is case sensitive.

WebsiteRedirectLocation

String

Optional

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

Returned Result (InterfaceResult)

Field

Type

Description

RequestId

String

Request ID returned by the OBS server

MetadataDirective

String

Metadata operation indicator.

The value can be REPLACE_NEW or REPLACE.

CacheControl

String

Specifies the cache behavior of the web page when the object is downloaded.

Content-Disposition

String

Object ETag

Content-Encoding

String

Object version ID

WebsiteRedirectLocation

String

Location where the object is redirected to, when the bucket is configured with website hosting

StorageClass

String

Storage class of the object. When the storage class is Standard, the value is null.

Content-Language

String

If Origin in the request meets the CORS rules of the bucket, AllowedOrigin in the CORS rules is returned.

Expires

String

If AccessControlRequestHeaders in the request meets the CORS rules of the bucket, AllowedHeader in the CORS rules is returned.

Metadata

Object

Customized metadata of the object

Sample Code

const metadata = { "test": "newmeta" };
const bucketName = 'bucketname';
const objectname = 'objectkey';

obsClient.setObjectMetadata({
       Bucket: bucketName,
       Key: objectname, 
       ContentType: 'application/zip',
       StorageClass: obsClient.enums.StorageClassStandard,
       MetadataDirective: 'REPLACE_NEW',
       Metadata: metadata
},(err, result) => {
       if(err){
              console.error('Error-->' + err);
       }else{
              if(result.CommonMsg.Status < 300){
                     console.log('RequestId-->' + result.InterfaceResult.RequestId);
                     console.log('MetadataDirective-->' + result.InterfaceResult.MetadataDirective);
                     console.log('Metadata-->' + JSON.stringify(result.InterfaceResult.Metadata));
                     console.log('StorageClass-->' + result.InterfaceResult.StorageClass);
              }else{
                     console.log('Status-->' + result.CommonMsg.Status);
              }
       }
});