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

Append Object

API Description

You can use this API to upload an object in appendable mode and append data to the object.

Method Definition

ObsClient.appendObject

Request Parameter

Field

Type

Optional or Mandatory

Description

Bucket

String

Mandatory

Bucket name

Key

String

Mandatory

Object name

Position

Number

Mandatory

Start position for next appending. This parameter must be set to 0 when you create an appendable object.

ACL

String

Optional

Pre-defined access control policy specified during the object creation

StorageClass

String

Optional

Storage class specified during the object creation

Body

String

or

stream.Readable

Optional

Content of the object to be uploaded. Both character strings and instances of stream.Readable are supported.

Offset

Number

Optional

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

SourceFile

String

Optional

Path to the source file of the object

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 length. This field is effective when SourceFile is set.

ContentMD5

String

Optional

MD5 value of the object (Base64-encoded). 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. This field can be null.

SseC

String

Optional

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

  • AES256

SseCKey

Buffer

Optional

Key used to encrypt the object in SSE-C mode, which is calculated by using AES256

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

Returned Result (InterfaceResult)

Field

Type

Description

RequestId

String

Request ID returned by the OBS server

ETag

String

Object ETag

NextPosition

String

Start position for next appending

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 decryption

SseKmsKey

String

Key used in SSE-KMS encryption

SseC

String

Algorithm used in SSE-C decryption

SseCKeyMd5

String

MD5 value of the key used in SSE-C decryption

Sample Code

// Import the OBS library.
// Use npm to install the client.
var ObsClient = require('esdk-obs-nodejs');
// Use source codes to install the client.
// var ObsClient = require('./lib/obs');

// Create an instance of ObsClient.
var obsClient = new ObsClient({
       //Obtain an AK/SK pair using environment variables or import the 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,
       server : 'https://your-endpoint'
});

// Create an appendable object. The start position must be 0.
obsClient.appendObject({
       Bucket:'bucketname',
       Key:'objectname',
       Position : 0, 
       Body : 'Hello OBS'
}).then(function(result){
       console.log('Status-->' + result.CommonMsg.Status);
       if(result.CommonMsg.Status < 300 && result.InterfaceResult){
              console.log('NextPosition-->' + result.InterfaceResult.NextPosition);
       }
       // Append data to the object.
       obsClient.appendObject({
              Bucket:'bucketname',
              Key:'objectname',
              Position : result.InterfaceResult.NextPosition,
              Body : 'Hello OBS Again'
       }, function(err, result2){
              if(err){
                     console.error('Error-->' + err);
              }else{
                     console.log('Status-->' + result2.CommonMsg.Status);
                     if(result2.CommonMsg.Status < 300 && result2.InterfaceResult){
                           console.log('NextPosition-->' + result2.InterfaceResult.NextPosition);
                     }
              }
       });
       
       // Use the API for obtaining object properties to get the start position for next appending.
       obsClient.getObjectMetadata({
              Bucket:'bucketname',
              Key:'objectname',
       }).then(function(result3){
              console.log('Status-->' + result3.CommonMsg.Status);
              if(result3.CommonMsg.Status < 300 && result3.InterfaceResult){
                     console.log('RequestId-->' + result3.InterfaceResult.RequestId);
                     console.log('NextPosition-->' + result3.InterfaceResult.NextPosition);
              }
       }).catch(function(err){
              console.error('err:' + err);
       });
       
}).catch(function(err){
       console.error('err:' + err);
});
  • Use the Position parameter to specify the start position for next appending and set it to 0 when you create an appendable object.
  • Objects uploaded using ObsClient.putObject, referred to as normal objects, can overwrite objects uploaded using ObsClient.appendObject, referred to as appendable objects. Data cannot be appended to an appendable object anymore once the object has been overwritten by a normal object.
  • When you upload an object for the first time in appendable mode, an exception will be thrown (status code 409) if a normal object with the same name exists.
  • The ETag returned for an appendable upload is the ETag for the uploaded content, rather than that of the whole object.
  • Data size in each appendable upload cannot exceed 5 GB, and 10,000 times of appendable uploads can be performed on a single object.
  • After an appendable upload is complete successfully, you can use InterfaceResult.NextPosition obtained from the returned result or call ObsClient.getObjectMetadata, to get the location for next appending.