Monitoraggio di un caricamento in più parti - Amazon Simple Storage Service

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

Monitoraggio di un caricamento in più parti

L'API Java per il caricamento in più parti di alto livello fornisce un'interfaccia di ascolto, ProgressListener, per il monitoraggio del caricamento di un oggetto in Amazon S3. Gli eventi di stato si verificano periodicamente e inviano al listener la notifica dell'avvenuto trasferimento dei dati.

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()); } });
Esempio

Il seguente codice Java carica un file e utilizza ProgressListener per monitorare lo stato del caricamento. Per istruzioni su come creare e testare un esempio di utilizzo, consulta Test degli esempi di codice Java di 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

Il seguente esempio di codice C# consente di caricare un file in un bucket S3 utilizzando la classe TransferUtility e monitorare lo stato di avanzamento del caricamento. Per informazioni sulla compatibilità dell'esempio con una versione specifica dell' AWS SDK for .NET e le istruzioni per la creazione e il test di un esempio di utilizzo, consulta Esecuzione degli esempi di codice .NET di Amazon S3.

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); } } }