• Object Storage Service

obs
  1. Help Center
  2. Object Storage Service
  3. Developer Guide (Node.js SDK)
  4. Object Management
  5. Copying an Object

Copying an Object

The object copy operation can create a copy for an existing object in OBS.

You can call ObsClient.copyObject to copy an object. When copying an object, you can rewrite properties and ACL for it, as well as set restriction conditions.

Copying an Object in Simple Mode

Sample code:

// Import the OBS library.
var ObsClient = require('./lib/obs');

// Create an instance of ObsClient.
var obsClient = new ObsClient({
    access_key_id: '*** Provide your Access Key ***',       
    secret_access_key: '*** Provide your Secret Key ***',       
    server : 'yourdomainname'
});

obsClient.copyObject({
       Bucket: 'destbucketname',
       Key : 'destobjectkey',
       CopySource:'sourcebucketname/sourceobjectkey'
}, (err, result) => {
       if(err){
              console.log('Error-->' + err);
       }else{
              console.log('Status-->' + result.CommonMsg.Status);
       }
});
NOTE:

Use the CopySource parameter to specify the information about the source object.

Rewriting Object Properties

The following sample code shows how to rewrite object properties.

// Import the OBS library.
var ObsClient = require('./lib/obs');

// Create an instance of ObsClient.
var obsClient = new ObsClient({
    access_key_id: '*** Provide your Access Key ***',       
    secret_access_key: '*** Provide your Secret Key ***',       
    server : 'yourdomainname'
});

obsClient.copyObject({
    Bucket: 'destbucketname',       
    Key : 'destobjectkey',       
    CopySource:'sourcebucketname/sourceobjectkey',
    ContentType : 'image/jpeg',
    StorageClass : obsClient.enums.StorageClassWarm,
    Metadata:{'property':'property-value'},
    MetadataDirective : ObsClient.enums.ReplaceMetadata
}, (err, result) => {
       if(err){
              console.log('Error-->' + err);
       }else{
              console.log('Status-->' + result.CommonMsg.Status);
       }
});
NOTE:

Use the Metadata parameter to specify the object's customized metadata to be rewritten and the MetadataDirective parameter to specify the rewrite mode, which can be ObsClient.enums.ReplaceMetadata (rewrite) or ObsClient.enums.CopyMetadata (copy from the source object).

Copying an Object by Specifying Conditions

When copying an object, you can specify one or more restriction conditions. If the conditions are met, the object will be copied. Otherwise, an error code will be returned and the copy will fail.

You can set the following conditions:

Parameter

Description

Format

CopySourceIfModifiedSince

Copies the source object if it is changed after the time specified by this parameter; otherwise, an error code is returned.

This parameter must conform to the HTTP time format specified in http://www.ietf.org/rfc/rfc2616.txt.

CopySourceIfUnmodifiedSince

Copies the source object if it is changed before the time specified by this parameter; otherwise, an error code is returned.

This parameter must conform to the HTTP time format specified in http://www.ietf.org/rfc/rfc2616.txt.

CopySourceIfMatch

Copies the source object if its ETag is the same as the one specified by this parameter; otherwise, an error code is returned.

Character string

CopySourceIfNoneMatch

Copies the source object if its ETag is different from the one specified by this parameter; otherwise, an error code is returned.

Character string

NOTE:
  • The ETag of the source object is the MD5 check value of the source object.
  • If the object copy request includes CopySourceIfUnmodifiedSinceCopySourceIfMatchCopySourceIfModifiedSince, or CopySourceIfNoneMatch, and the specified condition is not met, the object copy will fail with error code 412 Precondition Failed returned.
  • CopySourceIfModifiedSince and CopySourceIfNoneMatch can be used together. So do CopySourceIfUnmodifiedSince and CopySourceIfMatch.

Sample code:

// Import the OBS library.
var ObsClient = require('./lib/obs');

// Create an instance of ObsClient.
var obsClient = new ObsClient({
    access_key_id: '*** Provide your Access Key ***',       
    secret_access_key: '*** Provide your Secret Key ***',       
    server : 'yourdomainname'
});

obsClient.copyObject({
    Bucket: 'destbucketname',       
    Key : 'destobjectkey',       
    CopySource:'sourcebucketname/sourceobjectkey',
    CopySourceIfModifiedSince : 'Thu, 31 Dec 2015 16:00:00 GMT',
    CopySourceIfNoneMatch : 'none-match-etag'
}, (err, result) => {
       if(err){
              console.log('Error-->' + err);
       }else{
              console.log('Status-->' + result.CommonMsg.Status);
       }
});

Rewriting an Object ACL

Sample code:

// Import the OBS library.
var ObsClient = require('./lib/obs');

// Create an instance of ObsClient.
var obsClient = new ObsClient({
    access_key_id: '*** Provide your Access Key ***',       
    secret_access_key: '*** Provide your Secret Key ***',       
    server : 'yourdomainname'
});

obsClient.copyObject({
    Bucket: 'destbucketname',       
    Key : 'destobjectkey',       
    CopySource:'sourcebucketname/sourceobjectkey',
// Rewrite the Object ACL to public-read. 
    ACL : obsClient.enums.AclPublicRead
}, (err, result) => {
       if(err){
              console.log('Error-->' + err);
       }else{
              console.log('Status-->' + result.CommonMsg.Status);
       }
});
NOTE:

Use the ACL parameter to modify the object ACL.