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 creates 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.
Constraints
- The user has the read permission on the source object to be copied.
- Cross-region replication is not supported.
- The source object to be copied cannot be larger than 5 GB. If the size is less than 1 GB, you are advised to copy it directly. If the size is greater than 1 GB, you are advised to perform a multipart copy.
- If the source object to be copied is in the Archive storage class, you must restore it first.
Copying an Object Directly
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);
try{
CopyObjectResult result = obsClient.copyObject("sourcebucketname", "sourceobjectname", "destbucketname", "destobjectname");
System.out.println("\t" + result.getStatusCode());
System.out.println("\t" + result.getEtag());
}
catch (ObsException e)
{
....// Failed to copy.
System.out.println("HTTP Code: " + e.getResponseCode());
System.out.println("Error Code:" + e.getErrorCode());
System.out.println("Error Message: " + e.getErrorMessage());
System.out.println("Request ID:" + e.getErrorRequestId());
System.out.println("Host ID:" + e.getErrorHostId());
}
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);
System.out.println("\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 Java 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);
System.out.println("\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);
System.out.println("\t" + result.getEtag());
Performing a Multipart Copy
As a special case of multipart upload, multipart copy implements multipart upload by copying the whole or part of an object in a bucket. You can call ObsClient.copyPart to copy parts. Sample code is as follows:
String endPoint = "https://your-endpoint";
String ak = "*** Provide your Access Key ***";
String sk = "*** Provide your Secret Key ***";
final String destBucketName = "destbucketname";
final String destObjectKey = "destobjectname";
final String sourceBucketName = "sourcebucketname";
final String sourceObjectKey = "sourceobjectname";
// Create an instance of ObsClient.
final ObsClient obsClient = new ObsClient(ak, sk, endPoint);
// Initialize the thread pool.
ExecutorService executorService = Executors.newFixedThreadPool(20);
// Initialize the multipart upload.
InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(destBucketName, destObjectKey);
InitiateMultipartUploadResult result = obsClient.initiateMultipartUpload(request);
final String uploadId = result.getUploadId();
System.out.println("\t"+ uploadId + "\n");
// Obtain information about the large object.
ObjectMetadata metadata = obsClient.getObjectMetadata(sourceBucketName, sourceObjectKey);
// Set the part size to 100 MB.
long partSize = 100 * 1024 * 1024L;
long objectSize = metadata.getContentLength();
// Calculate the number of parts need to be copied.
long partCount = objectSize % partSize == 0 ? objectSize / partSize : objectSize / partSize + 1;
final List<PartEtag> partEtags = Collections.synchronizedList(new ArrayList<PartEtag>());
// Start copying parts concurrently.
for (int i = 0; i < partCount; i++)
{
// Start position for copying parts
final long rangeStart = i * partSize;
// End position for copying parts
final long rangeEnd = (i + 1 == partCount) ? objectSize - 1 : rangeStart + partSize - 1;
// Part number
final int partNumber = i + 1;
executorService.execute(new Runnable()
{
@Override
public void run()
{
CopyPartRequest request = new CopyPartRequest();
request.setUploadId(uploadId);
request.setSourceBucketName(sourceBucketName);
request.setSourceObjectKey(sourceObjectKey);
request.setDestinationBucketName(destBucketName);
request.setDestinationObjectKey(destObjectKey);
request.setByteRangeStart(rangeStart);
request.setByteRangeEnd(rangeEnd);
request.setPartNumber(partNumber);
CopyPartResult result;
try
{
result = obsClient.copyPart(request);
System.out.println("Part#" + partNumber + " done\n");
partEtags.add(new PartEtag(result.getEtag(), result.getPartNumber()));
}
catch (ObsException e)
{
e.printStackTrace();
}
}
});
}
// Wait until the copy is complete.
executorService.shutdown();
while (!executorService.isTerminated())
{
try
{
executorService.awaitTermination(5, TimeUnit.SECONDS);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
// Combine parts.
CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(destBucketName, destObjectKey, uploadId, partEtags);
obsClient.completeMultipartUpload(completeMultipartUploadRequest);
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.