-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
ListObjectVersions.cs
86 lines (77 loc) · 3.25 KB
/
ListObjectVersions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
namespace ListObjectVersionsExample
{
// snippet-start:[S3.dotnetv3.ListObjectVersionsExample]
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 = "amzn-s3-demo-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}'");
}
}
}
// snippet-end:[S3.dotnetv3.ListObjectVersionsExample]
}