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

Copying an Object

If you have any questions during development, post them on the Issues page of GitHub. For details about parameters and usage of each API, see the API Reference.

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.

  • If the source object to be copied is in the Archive storage class, you must restore it first.

Copying an Object in Simple Mode

Sample code:

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

// Create an ObsClient instance.
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/intl/en-us/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'
});

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

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.
// Use npm to install the client.
var ObsClient = require('esdk-obs-nodejs');
// Use the source code to install the client.
// var ObsClient = require('./lib/obs');

// Create an ObsClient instance.
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/intl/en-us/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'
});

obsClient.copyObject({
    Bucket: 'destbucketname',       
    Key : 'destobjectname',       
    CopySource:'sourcebucketname/sourceobjectname',
    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);
       }
});

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 has been modified since the specified time; otherwise, an exception is thrown.

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 has not been modified since the specified time; otherwise, an exception is thrown.

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

  • The ETag of the source object is the MD5 check value of the source object.
  • If the object copy request includes CopySourceIfUnmodifiedSince, CopySourceIfMatch, CopySourceIfModifiedSince, 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.
// Use npm to install the client.
var ObsClient = require('esdk-obs-nodejs');
// Use the source code to install the client.
// var ObsClient = require('./lib/obs');

// Create an ObsClient instance.
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/intl/en-us/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'
});

obsClient.copyObject({
    Bucket: 'destbucketname',       
    Key : 'destobjectname',       
    CopySource:'sourcebucketname/sourceobjectname',
    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.
// Use npm to install the client.
var ObsClient = require('esdk-obs-nodejs');
// Use the source code to install the client.
// var ObsClient = require('./lib/obs');

// Create an ObsClient instance.
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/intl/en-us/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'
});

obsClient.copyObject({
    Bucket: 'destbucketname',       
    Key : 'destobjectname',       
    CopySource:'sourcebucketname/sourceobjectname',
    // 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);
       }
});

Use the ACL parameter to modify the object ACL.