Suivi d'un chargement partitionné - Amazon Simple Storage Service

Les traductions sont fournies par des outils de traduction automatique. En cas de conflit entre le contenu d'une traduction et celui de la version originale en anglais, la version anglaise prévaudra.

Suivi d'un chargement partitionné

L'API de haut niveau de chargement partitionné fournit une interface d'écoute, ProgressListener, pour suivre la progression du chargement d'un objet sur Amazon S3. Les événements de progression ont lieu de manière périodique et informent l'écouteur que des octets ont été transférés.

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

L'exemple de code Java suivant charge un fichier et utilise le ProgressListener pour suivre la progression du chargement. Pour obtenir des instructions sur la façon de créer et de tester un exemple pratique, consultez Test des exemples de code Java 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

L'exemple C# suivant charge un fichier dans un compartiment S3 à l'aide de la classe TransferUtility et suit la progression du chargement. Pour plus d'informations sur la compatibilité de l'exemple avec une version spécifique du kit AWS SDK for .NET et pour obtenir des instructions pour la création et le test d'un exemple pratique, consultez Exécution des exemples de code .NET 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); } } }