-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
Copy pathObjectTag.cs
115 lines (100 loc) · 4.34 KB
/
ObjectTag.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
namespace ObjectTagExample
{
// snippet-start:[S3.dotnetv3.ObjectTagExample]
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
/// <summary>
/// This example shows how to work with tags in Amazon Simple Storage
/// Service (Amazon S3) objects.
/// </summary>
public class ObjectTag
{
public static async Task Main()
{
string bucketName = "amzn-s3-demo-bucket";
string keyName = "newobject.txt";
string filePath = @"*** file path ***";
// Specify your bucket region (an example region is shown).
RegionEndpoint bucketRegion = RegionEndpoint.USWest2;
var client = new AmazonS3Client(bucketRegion);
await PutObjectsWithTagsAsync(client, bucketName, keyName, filePath);
}
/// <summary>
/// This method uploads an object with tags. It then shows the tag
/// values, changes the tags, and shows the new tags.
/// </summary>
/// <param name="client">The Initialized Amazon S3 client object used
/// to call the methods to create and change an objects tags.</param>
/// <param name="bucketName">A string representing the name of the
/// bucket where the object will be stored.</param>
/// <param name="keyName">A string representing the key name of the
/// object to be tagged.</param>
/// <param name="filePath">The directory location and file name of the
/// object to be uploaded to the Amazon S3 bucket.</param>
public static async Task PutObjectsWithTagsAsync(IAmazonS3 client, string bucketName, string keyName, string filePath)
{
try
{
// Create an object with tags.
var putRequest = new PutObjectRequest
{
BucketName = bucketName,
Key = keyName,
FilePath = filePath,
TagSet = new List<Tag>
{
new Tag { Key = "Keyx1", Value = "Value1" },
new Tag { Key = "Keyx2", Value = "Value2" },
},
};
PutObjectResponse response = await client.PutObjectAsync(putRequest);
// Now retrieve the new object's tags.
GetObjectTaggingRequest getTagsRequest = new GetObjectTaggingRequest()
{
BucketName = bucketName,
Key = keyName,
};
GetObjectTaggingResponse objectTags = await client.GetObjectTaggingAsync(getTagsRequest);
// Display the tag values.
objectTags.Tagging
.ForEach(t => Console.WriteLine($"Key: {t.Key}, Value: {t.Value}"));
Tagging newTagSet = new Tagging()
{
TagSet = new List<Tag>
{
new Tag { Key = "Key3", Value = "Value3" },
new Tag { Key = "Key4", Value = "Value4" },
},
};
PutObjectTaggingRequest putObjTagsRequest = new PutObjectTaggingRequest()
{
BucketName = bucketName,
Key = keyName,
Tagging = newTagSet,
};
PutObjectTaggingResponse response2 = await client.PutObjectTaggingAsync(putObjTagsRequest);
// Retrieve the tags again and show the values.
GetObjectTaggingRequest getTagsRequest2 = new GetObjectTaggingRequest()
{
BucketName = bucketName,
Key = keyName,
};
GetObjectTaggingResponse objectTags2 = await client.GetObjectTaggingAsync(getTagsRequest2);
objectTags2.Tagging
.ForEach(t => Console.WriteLine($"Key: {t.Key}, Value: {t.Value}"));
}
catch (AmazonS3Exception ex)
{
Console.WriteLine(
$"Error: '{ex.Message}'");
}
}
}
// snippet-end:[S3.dotnetv3.ObjectTagExample]
}