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:

// Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables ACCESS_KEY_ID and SECRET_ACCESS_KEY_ID.
// 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.
String ak = System.getenv("ACCESS_KEY_ID");
String sk = System.getenv("SECRET_ACCESS_KEY_ID");
String endPoint = "https://your-endpoint";
// Create an ObsClient instance.
ObsClient obsClient = new ObsClient(ak, sk, endPoint);

CopyObjectResult result = obsClient.copyObject("sourcebucketname", "sourceobjectname", "destbucketname", "destobjectname");
Log.i("CopyObject","\t" + result.getEtag());

Rewriting Object Properties

The following sample code shows how to rewrite object properties.

// Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables ACCESS_KEY_ID and SECRET_ACCESS_KEY_ID.
// 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.
String ak = System.getenv("ACCESS_KEY_ID");
String sk = System.getenv("SECRET_ACCESS_KEY_ID");
String endPoint = "https://your-endpoint";
// Create an ObsClient instance.
ObsClient obsClient = new ObsClient(ak, sk, endPoint);

CopyObjectRequest request = new CopyObjectRequest("sourcebucketname", "sourceobjectname", "destbucketname", "destobjectname");
// Rewrite object properties.
request.setReplaceMetadata(true);
ObjectMetadata newObjectMetadata = new ObjectMetadata();
newObjectMetadata.setContentType("image/jpeg");
newObjectMetadata.addUserMetadata("property", "property-value");
newObjectMetadata.setObjectStorageClass(StorageClassEnum.WARM);
request.setNewObjectMetadata(newObjectMetadata);
CopyObjectResult result = obsClient.copyObject(request);
Log.i("CopyObject","\t" + result.getEtag());

CopyObjectRequest.setReplaceMetadata and CopyObjectRequest.setNewObjectMetadata must be used together.

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 exception will be thrown and the copy will fail.

You can set the following conditions:

Parameter

Description

Method in OBS Android SDK

Copy-Source-If-Modified-Since

Copies the source object if it has been modified since the specified time; otherwise, an exception is thrown.

CopyObjectRequest.setIfModifiedSince

Copy-Source-If-Unmodified-Since

Copies the source object if it has not been modified since the specified time; otherwise, an exception is thrown.

CopyObjectRequest.setIfUnmodifiedSince

Copy-Source-If-Match

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

CopyObjectRequest.setIfMatchTag

Copy-Source-If-None-Match

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

CopyObjectRequest.setIfNoneMatchTag

  • The ETag of the source object is the MD5 check value of the source object.
  • If Copy-Source-If-Unmodified-Since, Copy-Source-If-Match, Copy-Source-If-Modified-Since, or Copy-Source-If-None-Match is included and its specified condition is not met, an exception, whose HTTP status code is 412 Precondition Failed, will be thrown.
  • Copy-Source-If-Modified-Since and Copy-Source-If-None-Match can be used together, and so do Copy-Source-If-Unmodified-Since and Copy-Source-If-Match.

Sample code:

// Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables ACCESS_KEY_ID and SECRET_ACCESS_KEY_ID.
// 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.
String ak = System.getenv("ACCESS_KEY_ID");
String sk = System.getenv("SECRET_ACCESS_KEY_ID");
String endPoint = "https://your-endpoint";
// Create an ObsClient instance.
ObsClient obsClient = new ObsClient(ak, sk, endPoint);

CopyObjectRequest request = new CopyObjectRequest("sourcebucketname", "sourceobjectname", "destbucketname", "destobjectname");

request.setIfModifiedSince(new SimpleDateFormat("yyyy-MM-dd").parse("2016-01-01"));
request.setIfNoneMatchTag("none-match-etag");

CopyObjectResult result = obsClient.copyObject(request);
Log.i("CopyObject","\t" + result.getEtag());

Rewriting an Object ACL

Sample code:

// Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables ACCESS_KEY_ID and SECRET_ACCESS_KEY_ID.
// 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.
String ak = System.getenv("ACCESS_KEY_ID");
String sk = System.getenv("SECRET_ACCESS_KEY_ID");
String endPoint = "https://your-endpoint";
// Create an ObsClient instance.
ObsClient obsClient = new ObsClient(ak, sk, endPoint);

CopyObjectRequest request = new CopyObjectRequest("sourcebucketname", "sourceobjectname", "destbucketname", "destobjectname");

// Modify the Object ACL to public-read. 
request.setAcl(AccessControlList.REST_CANNED_PUBLIC_READ);
CopyObjectResult result = obsClient.copyObject(request);
Log.i("CopyObject","\t" + result.getEtag());