Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions,
see Getting Started with Amazon Web Services in China
(PDF).
List the version of objects in an Amazon S3 bucket using an Amazon SDK
The following code examples show how to list object versions in an S3 bucket.
Action examples are code excerpts from larger programs and must be run in context. You can see this action in
context in the following code example:
- .NET
-
- Amazon SDK for .NET
-
using System;
using System.Threading.Tasks;
using Amazon.S3;
using Amazon.S3.Model;
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()
{
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}'");
}
}
}
- Rust
-
- SDK for Rust
-
This documentation is for an SDK in preview release. The SDK is subject to change and should not be used in production.
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().unwrap_or_default() {
println!("{}", version.key().unwrap_or_default());
println!(" version ID: {}", version.version_id().unwrap_or_default());
println!();
}
Ok(())
}
For a complete list of Amazon SDK developer guides and code examples, see
Using this service with an Amazon SDK.
This topic also includes information about getting started and details about previous SDK versions.