Elenca la versione degli oggetti in un bucket Amazon S3 utilizzando un SDK AWS - 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à.

Elenca la versione degli oggetti in un bucket Amazon S3 utilizzando un SDK AWS

Gli esempi di codice seguenti mostrano come elencare le versioni degli oggetti in un bucket S3.

Gli esempi di operazioni sono estratti di codice da programmi più grandi e devono essere eseguiti nel contesto. È possibile visualizzare questa operazione nel contesto nel seguente esempio di codice:

.NET
AWS SDK for .NET
Nota

C'è altro su. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

using System; using System.Threading.Tasks; using Amazon.S3; using Amazon.S3.Model; /// <summary> /// This example lists the versions of the objects in a version enabled /// Amazon Simple Storage Service (Amazon S3) bucket. /// </summary> public class ListObjectVersions { public static async Task Main() { string bucketName = "doc-example-bucket"; // If the AWS Region where your bucket is defined is different from // the AWS Region where the Amazon S3 bucket is defined, pass the constant // for the AWS Region to the client constructor like this: // var client = new AmazonS3Client(RegionEndpoint.USWest2); IAmazonS3 client = new AmazonS3Client(); await GetObjectListWithAllVersionsAsync(client, bucketName); } /// <summary> /// This method lists all versions of the objects within an Amazon S3 /// version enabled bucket. /// </summary> /// <param name="client">The initialized client object used to call /// ListVersionsAsync.</param> /// <param name="bucketName">The name of the version enabled Amazon S3 bucket /// for which you want to list the versions of the contained objects.</param> public static async Task GetObjectListWithAllVersionsAsync(IAmazonS3 client, string bucketName) { try { // When you instantiate the ListVersionRequest, you can // optionally specify a key name prefix in the request // if you want a list of object versions of a specific object. // For this example we set a small limit in MaxKeys to return // a small list of versions. ListVersionsRequest request = new ListVersionsRequest() { BucketName = bucketName, MaxKeys = 2, }; do { ListVersionsResponse response = await client.ListVersionsAsync(request); // Process response. foreach (S3ObjectVersion entry in response.Versions) { Console.WriteLine($"key: {entry.Key} size: {entry.Size}"); } // If response is truncated, set the marker to get the next // set of keys. if (response.IsTruncated) { request.KeyMarker = response.NextKeyMarker; request.VersionIdMarker = response.NextVersionIdMarker; } else { request = null; } } while (request != null); } catch (AmazonS3Exception ex) { Console.WriteLine($"Error: '{ex.Message}'"); } } }
CLI
AWS CLI

Il comando seguente recupera le informazioni sulla versione di un oggetto in un bucket denominato: my-bucket

aws s3api list-object-versions --bucket my-bucket --prefix index.html

Output:

{ "DeleteMarkers": [ { "Owner": { "DisplayName": "my-username", "ID": "7009a8971cd660687538875e7c86c5b672fe116bd438f46db45460ddcd036c32" }, "IsLatest": true, "VersionId": "B2VsEK5saUNNHKcOAJj7hIE86RozToyq", "Key": "index.html", "LastModified": "2015-11-10T00:57:03.000Z" }, { "Owner": { "DisplayName": "my-username", "ID": "7009a8971cd660687538875e7c86c5b672fe116bd438f46db45460ddcd036c32" }, "IsLatest": false, "VersionId": ".FLQEZscLIcfxSq.jsFJ.szUkmng2Yw6", "Key": "index.html", "LastModified": "2015-11-09T23:32:20.000Z" } ], "Versions": [ { "LastModified": "2015-11-10T00:20:11.000Z", "VersionId": "Rb_l2T8UHDkFEwCgJjhlgPOZC0qJ.vpD", "ETag": "\"0622528de826c0df5db1258a23b80be5\"", "StorageClass": "STANDARD", "Key": "index.html", "Owner": { "DisplayName": "my-username", "ID": "7009a8971cd660687538875e7c86c5b672fe116bd438f46db45460ddcd036c32" }, "IsLatest": false, "Size": 38 }, { "LastModified": "2015-11-09T23:26:41.000Z", "VersionId": "rasWWGpgk9E4s0LyTJgusGeRQKLVIAFf", "ETag": "\"06225825b8028de826c0df5db1a23be5\"", "StorageClass": "STANDARD", "Key": "index.html", "Owner": { "DisplayName": "my-username", "ID": "7009a8971cd660687538875e7c86c5b672fe116bd438f46db45460ddcd036c32" }, "IsLatest": false, "Size": 38 }, { "LastModified": "2015-11-09T22:50:50.000Z", "VersionId": "null", "ETag": "\"d1f45267a863c8392e07d24dd592f1b9\"", "StorageClass": "STANDARD", "Key": "index.html", "Owner": { "DisplayName": "my-username", "ID": "7009a8971cd660687538875e7c86c5b672fe116bd438f46db45460ddcd036c32" }, "IsLatest": false, "Size": 533823 } ] }
Rust
SDK per Rust
Nota

C'è altro su GitHub. Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

async fn show_versions(client: &Client, bucket: &str) -> Result<(), Error> { let resp = client.list_object_versions().bucket(bucket).send().await?; for version in resp.versions() { println!("{}", version.key().unwrap_or_default()); println!(" version ID: {}", version.version_id().unwrap_or_default()); println!(); } Ok(()) }
  • Per i dettagli sulle API, consulta la ListObjectVersionsguida di riferimento all'API AWS SDK for Rust.

Per un elenco completo delle guide per sviluppatori AWS SDK e degli esempi di codice, consulta. Utilizzo del servizio con un SDK AWS Questo argomento include anche informazioni su come iniziare e dettagli sulle versioni precedenti dell'SDK.