Amazon Simple Storage Service
開発者ガイド (API バージョン 2006-03-01)

マルチパートアップロードの進行状況の追跡

高レベル Java マルチパートアップロード API は、Amazon S3 へのオブジェクトのアップロード時に進行状況を追跡するリッスンインターフェイス ProgressListener を備えています。進行状況イベントが定期的に、バイトが転送されたことをリスナーに通知します。

以下の例は、ProgressEvent イベントを登録し、ハンドラを記述する方法を示しています。

import java.io.File; 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 com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.model.PutObjectRequest; import com.amazonaws.services.s3.transfer.TransferManager; import com.amazonaws.services.s3.transfer.TransferManagerBuilder; import com.amazonaws.services.s3.transfer.Upload; 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(); } } }