Amazon Simple Storage Service
开发人员指南 (API Version 2006-03-01)
AWS 服务或AWS文档中描述的功能,可能因地区/位置而异。点 击 Getting Started with Amazon AWS to see specific differences applicable to the China (Beijing) Region.

使用AWS SDK for Java分段上传 API 复制对象

下面的任务将指导您使用 Java 开发工具包,以完成将 Amazon S3 对象从一个源位置复制到另一个位置 (例如,从一个存储桶到另一个存储桶) 的过程。您可以使用此处演示的代码复制大于 5 GB 的对象。对于小于 5 GB 的对象,请使用单个操作复制,如 使用 AWS SDK for Java 复制对象 中所述。

复制对象

1

通过提供 AWS 凭证创建 AmazonS3Client 类的实例。

2

通过执行 AmazonS3Client.initiateMultipartUpload 方法启动分段复制。创建 InitiateMultipartUploadRequest 的实例。您需要提供存储桶名称和键名称。

3

AmazonS3Client.initiateMultipartUpload 方法返回的响应对象保存上传 ID。您将需要为随后的每个分段上传操作提供此上传 ID。

4

复制所有分段。对于每个分段复制,创建 CopyPartRequest 类的新实例并提供分段信息,包括源存储桶、目标存储桶、对象键、上传 ID、分段的第一个字节、分段的最后一个字节以及分段编号。

5

CopyPartRequest 方法的响应保存在列表中。此响应包括 ETag 值和分段编号。您将需要分段编号来完成分段上传。

6

为每个分段重复任务 4 和 5。

7

执行 AmazonS3Client.completeMultipartUpload 方法以完成复制。

以下 Java 代码示例演示了上述任务。

Copy
// Step 1: Create instance and provide credentials. AmazonS3Client s3Client = new AmazonS3Client(new PropertiesCredentials( LowLevel_LargeObjectCopy.class.getResourceAsStream( "AwsCredentials.properties"))); // Create lists to hold copy responses List<CopyPartResult> copyResponses = new ArrayList<CopyPartResult>(); // Step 2: Initialize InitiateMultipartUploadRequest initiateRequest = new InitiateMultipartUploadRequest(targetBucketName, targetObjectKey); InitiateMultipartUploadResult initResult = s3Client.initiateMultipartUpload(initiateRequest); // Step 3: Save upload Id. String uploadId = initResult.getUploadId(); try { // Get object size. GetObjectMetadataRequest metadataRequest = new GetObjectMetadataRequest(sourceBucketName, sourceObjectKey); ObjectMetadata metadataResult = s3Client.getObjectMetadata(metadataRequest); long objectSize = metadataResult.getContentLength(); // in bytes // Step 4. Copy parts. long partSize = 5 * (long)Math.pow(2.0, 20.0); // 5 MB long bytePosition = 0; for (int i = 1; bytePosition < objectSize; i++) { // Step 5. Save copy response. CopyPartRequest copyRequest = new CopyPartRequest() .withDestinationBucketName(targetBucketName) .withDestinationKey(targetObjectKey) .withSourceBucketName(sourceBucketName) .withSourceKey(sourceObjectKey) .withUploadId(initResult.getUploadId()) .withFirstByte(bytePosition) .withLastByte(bytePosition + partSize -1 >= objectSize ? objectSize - 1 : bytePosition + partSize - 1) .withPartNumber(i); copyResponses.add(s3Client.copyPart(copyRequest)); bytePosition += partSize; } // Step 7. Complete copy operation. CompleteMultipartUploadResult completeUploadResponse = s3Client.completeMultipartUpload(completeRequest); } catch (Exception e) { System.out.println(e.getMessage()); }

下面的 Java 代码示例将对象从一个 Amazon S3 存储桶复制到另一个存储桶。有关如何创建和测试有效示例的说明,请参阅 测试 Java 代码示例

Copy
import java.io.IOException; import java.util.ArrayList; import java.util.List; import com.amazonaws.auth.PropertiesCredentials; import com.amazonaws.services.s3.*; import com.amazonaws.services.s3.model.*; public class LowLevel_LargeObjectCopy { public static void main(String[] args) throws IOException { String sourceBucketName = "*** Source-Bucket-Name ***"; String targetBucketName = "*** Target-Bucket-Name ***"; String sourceObjectKey = "*** Source-Object-Key ***"; String targetObjectKey = "*** Target-Object-Key ***"; AmazonS3Client s3Client = new AmazonS3Client(new PropertiesCredentials( LowLevel_LargeObjectCopy.class.getResourceAsStream( "AwsCredentials.properties"))); // List to store copy part responses. List<CopyPartResult> copyResponses = new ArrayList<CopyPartResult>(); InitiateMultipartUploadRequest initiateRequest = new InitiateMultipartUploadRequest(targetBucketName, targetObjectKey); InitiateMultipartUploadResult initResult = s3Client.initiateMultipartUpload(initiateRequest); try { // Get object size. GetObjectMetadataRequest metadataRequest = new GetObjectMetadataRequest(sourceBucketName, sourceObjectKey); ObjectMetadata metadataResult = s3Client.getObjectMetadata(metadataRequest); long objectSize = metadataResult.getContentLength(); // in bytes // Copy parts. long partSize = 5 * (long)Math.pow(2.0, 20.0); // 5 MB long bytePosition = 0; for (int i = 1; bytePosition < objectSize; i++) { CopyPartRequest copyRequest = new CopyPartRequest() .withDestinationBucketName(targetBucketName) .withDestinationKey(targetObjectKey) .withSourceBucketName(sourceBucketName) .withSourceKey(sourceObjectKey) .withUploadId(initResult.getUploadId()) .withFirstByte(bytePosition) .withLastByte(bytePosition + partSize -1 >= objectSize ? objectSize - 1 : bytePosition + partSize - 1) .withPartNumber(i); copyResponses.add(s3Client.copyPart(copyRequest)); bytePosition += partSize; } CompleteMultipartUploadRequest completeRequest = new CompleteMultipartUploadRequest( targetBucketName, targetObjectKey, initResult.getUploadId(), GetETags(copyResponses)); CompleteMultipartUploadResult completeUploadResponse = s3Client.completeMultipartUpload(completeRequest); } catch (Exception e) { System.out.println(e.getMessage()); } } // Helper function that constructs ETags. static List<PartETag> GetETags(List<CopyPartResult> responses) { List<PartETag> etags = new ArrayList<PartETag>(); for (CopyPartResult response : responses) { etags.add(new PartETag(response.getPartNumber(), response.getETag())); } return etags; } }