Amazon Simple Storage Service
开发人员指南 (API 版本 2006-03-01)
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 Amazon AWS 入门


高级别 Java 分段上传 API 提供了侦听接口 ProgressListener,用于在将对象上传到 Amazon S3 时跟踪进度。进度事件定期向侦听器通知已传输的字节。

以下示例演示了如何订阅 ProgressEvent 事件和编写处理程序:

// Copyright 2018, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: MIT-0 (For details, see import; import com.amazonaws.AmazonServiceException; import com.amazonaws.SdkClientException; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.event.ProgressEvent; import com.amazonaws.event.ProgressListener; import; import; import; import; import; import; public class HighLevelTrackMultipartUpload { public static void main(String[] args) throws Exception { String clientRegion = "*** Client region ***"; String bucketName = "*** Bucket name ***"; String keyName = "*** Object key ***"; String filePath = "*** Path to file to upload ***"; try { AmazonS3 s3Client = AmazonS3ClientBuilder.standard() .withRegion(clientRegion) .withCredentials(new ProfileCredentialsProvider()) .build(); TransferManager tm = TransferManagerBuilder.standard() .withS3Client(s3Client) .build(); PutObjectRequest request = new PutObjectRequest(bucketName, keyName, new File(filePath)); // To receive notifications when bytes are transferred, add a // ProgressListener to your request. request.setGeneralProgressListener(new ProgressListener() { public void progressChanged(ProgressEvent progressEvent) { System.out.println("Transferred bytes: " + progressEvent.getBytesTransferred()); } }); // TransferManager processes all transfers asynchronously, // so this call returns immediately. Upload upload = tm.upload(request); // Optionally, you can wait for the upload to finish before continuing. upload.waitForCompletion(); } catch(AmazonServiceException e) { // The call was transmitted successfully, but Amazon S3 couldn't process // it, so it returned an error response. e.printStackTrace(); } catch(SdkClientException e) { // Amazon S3 couldn't be contacted for a response, or the client // couldn't parse the response from Amazon S3. e.printStackTrace(); } } }