Monitorar um multipart upload - Amazon Simple Storage Service

Monitorar um multipart upload

A API multipart upload de alto nível upload fornece uma interface de escuta, ProgressListener, para acompanhar o progresso ao fazer upload de um objeto para o Amazon S3. Os eventos de progresso ocorrem periodicamente e notificam o listener que os bytes foram transferidos.

Java
TransferManager tm = new TransferManager(new ProfileCredentialsProvider()); PutObjectRequest request = new PutObjectRequest( existingBucketName, keyName, new File(filePath)); // Subscribe to the event and provide event handler. request.setProgressListener(new ProgressListener() { public void progressChanged(ProgressEvent event) { System.out.println("Transferred bytes: " + event.getBytesTransfered()); } });
exemplo

O código Java a seguir faz upload de um arquivo e usa ProgressListener para rastrear o progresso do upload. Para obter instruções sobre como criar e testar um exemplo funcional, consulte Testar exemplos de código Java no Amazon S3.

import java.io.File; import com.amazonaws.AmazonClientException; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.event.ProgressEvent; import com.amazonaws.event.ProgressListener; import com.amazonaws.services.s3.model.PutObjectRequest; import com.amazonaws.services.s3.transfer.TransferManager; import com.amazonaws.services.s3.transfer.Upload; public class TrackMPUProgressUsingHighLevelAPI { public static void main(String[] args) throws Exception { String existingBucketName = "*** Provide bucket name ***"; String keyName = "*** Provide object key ***"; String filePath = "*** file to upload ***"; TransferManager tm = new TransferManager(new ProfileCredentialsProvider()); // For more advanced uploads, you can create a request object // and supply additional request parameters (ex: progress listeners, // canned ACLs, etc.) PutObjectRequest request = new PutObjectRequest( existingBucketName, keyName, new File(filePath)); // You can ask the upload for its progress, or you can // add a ProgressListener to your request to receive notifications // when bytes are transferred. request.setGeneralProgressListener(new ProgressListener() { @Override public void progressChanged(ProgressEvent progressEvent) { System.out.println("Transferred bytes: " + progressEvent.getBytesTransferred()); } }); // TransferManager processes all transfers asynchronously, // so this call will return immediately. Upload upload = tm.upload(request); try { // You can block and wait for the upload to finish upload.waitForCompletion(); } catch (AmazonClientException amazonClientException) { System.out.println("Unable to upload file, upload aborted."); amazonClientException.printStackTrace(); } } }
.NET

O seguinte exemplo do C# faz upload de um arquivo em um bucket do S3 usando a classe TransferUtility e monitora o andamento do upload. Para obter informações sobre a compatibilidade do exemplo com uma versão específica do AWS SDK for .NET e instruções para criar e testar um exemplo funcional, consulte Executar os exemplos de código do Amazon S3 .NET.

using Amazon; using Amazon.S3; using Amazon.S3.Transfer; using System; using System.Threading.Tasks; namespace Amazon.DocSamples.S3 { class TrackMPUUsingHighLevelAPITest { private const string bucketName = "*** provide the bucket name ***"; private const string keyName = "*** provide the name for the uploaded object ***"; private const string filePath = " *** provide the full path name of the file to upload **"; // Specify your bucket region (an example region is shown). private static readonly RegionEndpoint bucketRegion = RegionEndpoint.USWest2; private static IAmazonS3 s3Client; public static void Main() { s3Client = new AmazonS3Client(bucketRegion); TrackMPUAsync().Wait(); } private static async Task TrackMPUAsync() { try { var fileTransferUtility = new TransferUtility(s3Client); // Use TransferUtilityUploadRequest to configure options. // In this example we subscribe to an event. var uploadRequest = new TransferUtilityUploadRequest { BucketName = bucketName, FilePath = filePath, Key = keyName }; uploadRequest.UploadProgressEvent += new EventHandler<UploadProgressArgs> (uploadRequest_UploadPartProgressEvent); await fileTransferUtility.UploadAsync(uploadRequest); Console.WriteLine("Upload completed"); } catch (AmazonS3Exception e) { Console.WriteLine("Error encountered on server. Message:'{0}' when writing an object", e.Message); } catch (Exception e) { Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message); } } static void uploadRequest_UploadPartProgressEvent(object sender, UploadProgressArgs e) { // Process event. Console.WriteLine("{0}/{1}", e.TransferredBytes, e.TotalBytes); } } }