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 is an Archive object, you must restore it before copying it.
Copying an Object in Simple Mode
Sample code:
String endPoint = "https://your-endpoint";
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", "sourceobjectname", "destbucketname", "destobjectname");
Log.i("CopyObject","\t" + result.getEtag());
Rewriting Object Properties
The following sample code shows how to rewrite object properties.
String endPoint = "https://your-endpoint";
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", "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 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 |
- 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 = "https://your-endpoint";
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", "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:
String endPoint = "https://your-endpoint";
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", "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());
Last Article: Deleting Objects
Next Article: Temporarily Authorized Access
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.