将 ListObjectVersions 与 Amazon SDK 或命令行工具结合使用 - Amazon Simple Storage Service
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

ListObjectVersions 与 Amazon SDK 或命令行工具结合使用

以下代码示例演示如何使用 ListObjectVersions

操作示例是大型程序的代码摘录,必须在上下文中运行。在以下代码示例中,您可以查看此操作的上下文:

.NET
Amazon SDK for .NET
注意

在 GitHub 上查看更多内容。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

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}'"); } } }
  • 有关 API 详细信息,请参阅《Amazon SDK for .NET API 参考》中的 ListObjectVersions

CLI
Amazon CLI

以下命令检索名为 my-bucket 的存储桶中对象的版本信息:

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

输出:

{ "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 } ] }
  • 有关 API 详细信息,请参阅《Amazon CLI 命令参考》中的 ListObjectVersions

Rust
适用于 Rust 的 SDK
注意

在 GitHub 上查看更多内容。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

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(()) }
  • 有关 API 详细信息,请参阅《Amazon SDK for Rust API 参考》中的 ListObjectVersions

有关 Amazon SDK 开发人员指南和代码示例的完整列表,请参阅 将此服务与 Amazon SDK 结合使用。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。