• Object Storage Service

obs
  1. Help Center
  2. Object Storage Service
  3. Developer Guide (Android 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.

NOTE:
  • If the source object is a Cold object, you must restore it before copying it.

Copying an Object in Simple Mode

Sample code:

String endPoint = "yourdomainname";
String ak = "*** Provide your Access Key ***";
String sk = "*** Provide your Secret Key ***";

// Create an instance of ObsClient.
ObsClient obsClient = new ObsClient(ak, sk, endPoint);

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

// Close obsClient.
obsClient.close();

Rewriting Object Properties

The following sample code shows how to rewrite object properties.

String endPoint = "yourdomainname";
String ak = "*** Provide your Access Key ***";
String sk = "*** Provide your Secret Key ***";

// Create an instance of ObsClient.
ObsClient obsClient = new ObsClient(ak, sk, endPoint);

CopyObjectRequest request = new CopyObjectRequest("sourcebucketname", "sourceobjectkey", "destbucketname", "destobjectkey");
// 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());

// Close obsClient.
obsClient.close();
NOTE:

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 is changed after the time specified by this parameter; otherwise, an exception is thrown.

CopyObjectRequest.setIfModifiedSince

Copy-Source-If-Unmodified-Since

Copies the source object if it is changed before the time specified by this parameter; 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

NOTE:
  • 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:

String endPoint = "yourdomainname";
String ak = "*** Provide your Access Key ***";
String sk = "*** Provide your Secret Key ***";

// Create an instance of ObsClient.
ObsClient obsClient = new ObsClient(ak, sk, endPoint);

CopyObjectRequest request = new CopyObjectRequest("sourcebucketname", "sourceobjectkey", "destbucketname", "destobjectkey");

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());

// Close obsClient.
obsClient.close();

Rewriting an Object ACL

Sample code:

String endPoint = "yourdomainname";
String ak = "*** Provide your Access Key ***";
String sk = "*** Provide your Secret Key ***";

// Create an instance of ObsClient.
ObsClient obsClient = new ObsClient(ak, sk, endPoint);

CopyObjectRequest request = new CopyObjectRequest("sourcebucketname", "sourceobjectkey", "destbucketname", "destobjectkey");

// 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());

// Close obsClient.
obsClient.close();