You can copy an object from a source Object Storage Service (OSS) on CloudBox bucket to a destination OSS on CloudBox bucket that is located in the same region without modifying the content of the object.
Limits
Objects cannot be copied between OSS on CloudBox buckets located in different regions. For example, you cannot copy objects from OSS on CloudBox buckets in the China (Hangzhou) region to OSS on CloudBox buckets in the China (Shanghai) region.
Appendable objects cannot be copied.
Usage notes
You must have read permissions on the source object and read and write permissions on the destination bucket. Otherwise, the copy operation fails.
By default, when you copy an object that has the same name as an existing object in the destination OSS on CloudBox bucket, the copied object overwrites the existing object in the destination OSS on CloudBox bucket. You can use one of the following methods to prevent your objects from being unexpectedly overwritten:
Enable versioning for the destination OSS on CloudBox bucket
If versioning is enabled for the destination OSS on CloudBox bucket, deleted and overwritten objects in the destination OSS on CloudBox bucket are saved as previous versions. You can restore previous versions of an object at any time.
Include the x-oss-forbid-overwrite header in the copy request
You can include the x-oss-forbid-overwrite header in the copy request and set the header to true to disable object overwriting. If you copy an object that has the same name as an existing object in the destination bucket, the object fails to be copied and the FileAlreadyExists
error code is returned.
Copy a small object
OSS on CloudBox allows you to copy an object less than 1 GB in size by calling the CopyObject operation.
Use OSS SDK for Java
You can copy an object by using only OSS SDK for Java. The version of OSS SDK for Java must be 3.15.0 or later. The following sample code provides an example on how to call the CopyObject operation to copy an object less than 1 GB in size by using OSS SDKs for Java:
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.*;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
public class Demo {
public static void main(String[] args) throws Exception {
String endpoint = "https://cb-f8z7yvzgwfkl9q0h****.cn-hangzhou.oss-cloudbox.aliyuncs.com";
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
String region = "cn-hangzhou";
String cloudBoxId = "cb-f8z7yvzgwfkl9q0h****";
String sourceBucketName = "srcexamplebucket";
String sourceKey = "srcexampleobject.txt";
String destinationBucketName = "desexamplebucket";
String destinationKey = "desexampleobject.txt";
ClientBuilderConfiguration conf = new ClientBuilderConfiguration();
conf.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(new DefaultCredentialProvider(credentialsProvider.getCredentials()))
.clientConfiguration(conf)
.region(region)
.cloudBoxId(cloudBoxId)
.build();
try {
CopyObjectRequest copyObjectRequest = new CopyObjectRequest(sourceBucketName, sourceKey, destinationBucketName, destinationKey);
ObjectMetadata meta = new ObjectMetadata();
meta.setContentType("text/txt");
copyObjectRequest.setNewObjectMetadata(meta);
CopyObjectResult result = ossClient.copyObject(copyObjectRequest);
System.out.println("ETag: " + result.getETag() + " LastModified: " + result.getLastModified());
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}
Use ossutil
You can use ossutil to copy an object less than 1 GB in size. For more information, see Copy objects.
Use ossutil
You can use ossutil to copy an object larger than 1 GB in size. For more information, see Copy objects.
Copy a large object
OSS on CloudBox allows you to copy an object greater than 1 GB in size by calling the UploadPartCopy operation.
Use OSS SDK for Java
You can copy an object by using only OSS SDK for Java. The version of OSS SDK for Java must be 3.15.0 or later. The following sample code provides an example on how to call the UploadPartCopy operation to copy an object greater than 1 GB in size by using OSS SDKs for Java:
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.*;
import java.util.ArrayList;
import java.util.List;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
public class Demo {
public static void main(String[] args) throws Exception {
String endpoint = "https://cb-f8z7yvzgwfkl9q0h****.cn-hangzhou.oss-cloudbox.aliyuncs.com";
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
String region = "cn-hangzhou";
String cloudBoxId = "cb-f8z7yvzgwfkl9q0h****";
String sourceBucketName = "srcexamplebucket";
String sourceKey = "srcexampleobject.txt";
String destinationBucketName = "desexamplebucket";
String destinationKey = "desexampleobject.txt";
ClientBuilderConfiguration conf = new ClientBuilderConfiguration();
conf.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(new DefaultCredentialProvider(credentialsProvider.getCredentials()))
.clientConfiguration(conf)
.region(region)
.cloudBoxId(cloudBoxId)
.build();
try {
ObjectMetadata objectMetadata = ossClient.getObjectMetadata(sourceBucketName, sourceKey);
long contentLength = objectMetadata.getContentLength();
long partSize = 1024 * 1024 * 10;
int partCount = (int) (contentLength / partSize);
if (contentLength % partSize != 0) {
partCount++;
}
System.out.println("total part count:" + partCount);
InitiateMultipartUploadRequest initiateMultipartUploadRequest = new InitiateMultipartUploadRequest(destinationBucketName, destinationKey);
InitiateMultipartUploadResult initiateMultipartUploadResult = ossClient.initiateMultipartUpload(initiateMultipartUploadRequest);
String uploadId = initiateMultipartUploadResult.getUploadId();
List<PartETag> partETags = new ArrayList<PartETag>();
for (int i = 0; i < partCount; i++) {
long skipBytes = partSize * i;
long size = partSize < contentLength - skipBytes ? partSize : contentLength - skipBytes;
UploadPartCopyRequest uploadPartCopyRequest =
new UploadPartCopyRequest(sourceBucketName, sourceKey, destinationBucketName, destinationKey);
uploadPartCopyRequest.setUploadId(uploadId);
uploadPartCopyRequest.setPartSize(size);
uploadPartCopyRequest.setBeginIndex(skipBytes);
uploadPartCopyRequest.setPartNumber(i + 1);
UploadPartCopyResult uploadPartCopyResult = ossClient.uploadPartCopy(uploadPartCopyRequest);
partETags.add(uploadPartCopyResult.getPartETag());
}
CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(
destinationBucketName, destinationKey, uploadId, partETags);
ossClient.completeMultipartUpload(completeMultipartUploadRequest);
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}
Use ossutil
You can use ossutil to copy an object larger than 1 GB in size. For more information, see Copy objects.
Use the OSS API
If your business requires a high level of customization, you can directly call the OSS API. To directly call an API operation, you must include the signature calculation in your code. For more information, see UploadPartCopy.