Updated on 2024-05-16 GMT+08:00

PUT Object

API Description

You can use this API to upload an object to a specified bucket.

Method Definition

ObsClient.putObject

Request Parameter

Field

Type

Optional or Mandatory

Description

Bucket

String

Mandatory

Bucket name

Key

String

Mandatory

Object name

RequestDate

String

or

Date

Optional

Request time

NOTE:

When the parameter type is String, the value must comply with the ISO8601 or RFC822 standards.

ACL

String

Optional

Pre-defined access control policy specified during object creation

StorageClass

String

Optional

Storage class, which can be specified during the object creation

Body

String

Optional

Object content to be uploaded

SourceFile

File

or

Blob

Optional

File to be uploaded (The browser must support FileReader.)

ProgressCallback

Function

Optional

Callback function for obtaining the upload progress

NOTE:

This callback function contains the following parameters in sequence: Number of uploaded bytes, total bytes, and used time (unit: second).

Offset

Number

Optional

Start offset (in bytes) of a part in the source file. The default value is 0. This field is effective only when SourceFile is set.

Metadata

Object

Optional

Customized metadata of the object

WebsiteRedirectLocation

String

Optional

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

Expires

Number

Optional

Expiration time of the object, in days

SuccessActionRedirect

String

Optional

Redirection address after the upload is successful

ContentType

String

Optional

MIME type of the object

ContentLength

Number

Optional

Object size in bytes. This parameter is effective when SourceFile is set.

ContentMD5

String

Optional

Base64-encoded MD5 value of the object data to be uploaded. It is provided for the OBS server to verify data integrity.

SseKms

String

Optional

Algorithm used in SSE-KMS encryption. The value can be:

  • kms

SseKmsKey

String

Optional

Master key used in SSE-KMS encryption. The value can be null.

SseC

String

Optional

Algorithm used in SSE-C encryption. The value can be:

  • AES256

SseCKey

String

Optional

Key used in SSE-C encryption. It is calculated by using AES-256.

  • Body and SourceFile cannot be used together.
  • If both Body and SourceFile are null, the size of the object to be uploaded is 0 bytes.

Returned Result (InterfaceResult)

Field

Type

Description

RequestId

String

Request ID returned by the OBS server

ETag

String

Object ETag

VersionId

String

Object version ID

StorageClass

String

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

SseKms

String

Algorithm used in SSE-KMS

SseKmsKey

String

ID of the KMS master key when SSE-KMS is used

SseC

String

Algorithm used in SSE-C

SseCKeyMd5

String

MD5 value of the key for encrypting objects when SSE-C is used. This value is used to check whether any error occurs during the transmission of the key.

Sample Code

obsClient.putObject({
       Bucket : 'bucketname',
       Key : 'objectkey',
       Metadata:{meta1:'value1', meta2:'value2'},
       Body : 'Hello OBS',
       ContentType: 'text/plain'
},function (err, result) {
       if(err){
              console.error('Error-->' + err);
       }else{
              if(result.CommonMsg.Status < 300){
                     console.log('RequestId-->' + result.InterfaceResult.RequestId);
                     console.log('ETag-->' + result.InterfaceResult.ETag);
                     console.log('VersionId-->' + result.InterfaceResult.VersionId);
                     console.log('StorageClass-->' + result.InterfaceResult.StorageClass);
              }else{
                     console.log('Code-->' + result.CommonMsg.Code);
                     console.log('Message-->' + result.CommonMsg.Message);
              }
       }
});