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.

上传文件

以下任务将引导您使用低级别的 Java 类来上传文件。

低级别 API 文件上传过程

1

创建 AmazonS3Client 类的实例。

2

通过执行 AmazonS3Client.initiateMultipartUpload 方法开始分段上传。您需要通过创建 InitiateMultipartUploadRequest 类的实例,提供开始分段上传所需的信息 (即存储桶名称和键名称)。

3

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

4

上传分段。对每个上传分段执行 AmazonS3Client.uploadPart 方法。您需要提供分段上传信息,例如上传 ID、存储桶名称和分段编号。您可以通过创建 UploadPartRequest 类的实例提供此信息。

5

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

6

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

7

执行 AmazonS3Client.completeMultipartUpload 方法来完成分段上传。

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

Copy
AmazonS3 s3Client = new AmazonS3Client(new ProfileCredentialsProvider()); // Create a list of UploadPartResponse objects. You get one of these for // each part upload. List<PartETag> partETags = new ArrayList<PartETag>(); // Step 1: Initialize. InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest( existingBucketName, keyName); InitiateMultipartUploadResult initResponse = s3Client.initiateMultipartUpload(initRequest); File file = new File(filePath); long contentLength = file.length(); long partSize = 5 * 1024 * 1024; // Set part size to 5 MB. try { // Step 2: Upload parts. long filePosition = 0; for (int i = 1; filePosition < contentLength; i++) { // Last part can be less than 5 MB. Adjust part size. partSize = Math.min(partSize, (contentLength - filePosition)); // Create request to upload a part. UploadPartRequest uploadRequest = new UploadPartRequest() .withBucketName(existingBucketName).withKey(keyName) .withUploadId(initResponse.getUploadId()).withPartNumber(i) .withFileOffset(filePosition) .withFile(file) .withPartSize(partSize); // Upload part and add response to our list. partETags.add(s3Client.uploadPart(uploadRequest).getPartETag()); filePosition += partSize; } // Step 3: Complete. CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest(existingBucketName, keyName, initResponse.getUploadId(), partETags); s3Client.completeMultipartUpload(compRequest); } catch (Exception e) { s3Client.abortMultipartUpload(new AbortMultipartUploadRequest( existingBucketName, keyName, initResponse.getUploadId())); }

以下 Java 代码示例将文件上传到 Amazon S3 存储桶。有关如何创建和测试有效示例的说明,请参阅 测试 Java 代码示例

Copy
import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3Client; import com.amazonaws.services.s3.model.AbortMultipartUploadRequest; import com.amazonaws.services.s3.model.CompleteMultipartUploadRequest; import com.amazonaws.services.s3.model.InitiateMultipartUploadRequest; import com.amazonaws.services.s3.model.InitiateMultipartUploadResult; import com.amazonaws.services.s3.model.PartETag; import com.amazonaws.services.s3.model.UploadPartRequest; public class UploadObjectMPULowLevelAPI { public static void main(String[] args) throws IOException { String existingBucketName = "*** Provide-Your-Existing-BucketName ***"; String keyName = "*** Provide-Key-Name ***"; String filePath = "*** Provide-File-Path ***"; AmazonS3 s3Client = new AmazonS3Client(new ProfileCredentialsProvider()); // Create a list of UploadPartResponse objects. You get one of these // for each part upload. List<PartETag> partETags = new ArrayList<PartETag>(); // Step 1: Initialize. InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(existingBucketName, keyName); InitiateMultipartUploadResult initResponse = s3Client.initiateMultipartUpload(initRequest); File file = new File(filePath); long contentLength = file.length(); long partSize = 5242880; // Set part size to 5 MB. try { // Step 2: Upload parts. long filePosition = 0; for (int i = 1; filePosition < contentLength; i++) { // Last part can be less than 5 MB. Adjust part size. partSize = Math.min(partSize, (contentLength - filePosition)); // Create request to upload a part. UploadPartRequest uploadRequest = new UploadPartRequest() .withBucketName(existingBucketName).withKey(keyName) .withUploadId(initResponse.getUploadId()).withPartNumber(i) .withFileOffset(filePosition) .withFile(file) .withPartSize(partSize); // Upload part and add response to our list. partETags.add( s3Client.uploadPart(uploadRequest).getPartETag()); filePosition += partSize; } // Step 3: Complete. CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest( existingBucketName, keyName, initResponse.getUploadId(), partETags); s3Client.completeMultipartUpload(compRequest); } catch (Exception e) { s3Client.abortMultipartUpload(new AbortMultipartUploadRequest( existingBucketName, keyName, initResponse.getUploadId())); } } }