将 ListObjectVersions 与 AWS SDK 或 CLI 配合使用 - Amazon Simple Storage Service

ListObjectVersions 与 AWS SDK 或 CLI 配合使用

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

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

.NET
AWS SDK for .NET
注意

查看 GitHub,了解更多信息。例如,如果您将大小为 128 KB 到 1024 KB 的对象设置为从 S3 标准存储类移到 S3标准-IA 存储类,则大小精确为 1024 KB 和 128 KB 的对象将不会转换到 S3 标准-IA。

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 详细信息,请参阅《AWS SDK for .NET API 参考》中的 ListObjectVersions

CLI
AWS 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 详细信息,请参阅《AWS CLI 命令参考》中的 ListObjectVersions

Go
适用于 Go V2 的 SDK
注意

查看 GitHub,了解更多信息。例如,如果您将大小为 128 KB 到 1024 KB 的对象设置为从 S3 标准存储类移到 S3标准-IA 存储类,则大小精确为 1024 KB 和 128 KB 的对象将不会转换到 S3 标准-IA。

// S3Actions wraps S3 service actions. type S3Actions struct { S3Client *s3.Client S3Manager *manager.Uploader } // ListObjectVersions lists all versions of all objects in a bucket. func (actor S3Actions) ListObjectVersions(ctx context.Context, bucket string) ([]types.ObjectVersion, error) { var err error var output *s3.ListObjectVersionsOutput var versions []types.ObjectVersion input := &s3.ListObjectVersionsInput{Bucket: aws.String(bucket)} versionPaginator := s3.NewListObjectVersionsPaginator(actor.S3Client, input) for versionPaginator.HasMorePages() { output, err = versionPaginator.NextPage(ctx) if err != nil { var noBucket *types.NoSuchBucket if errors.As(err, &noBucket) { log.Printf("Bucket %s does not exist.\n", bucket) err = noBucket } break } else { versions = append(versions, output.Versions...) } } return versions, err }
  • 有关 API 详细信息,请参阅《AWS SDK for Go API 参考》中的 ListObjectVersions

Rust
适用于 Rust 的 SDK
注意

查看 GitHub,了解更多信息。例如,如果您将大小为 128 KB 到 1024 KB 的对象设置为从 S3 标准存储类移到 S3标准-IA 存储类,则大小精确为 1024 KB 和 128 KB 的对象将不会转换到 S3 标准-IA。

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 详细信息,请参阅《AWS SDK for Rust API 参考》中的 ListObjectVersions

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