Skip to content

Commit 8592fb2

Browse files
committed
Adding two S3 examples that work with ACL.
1 parent 1aaa243 commit 8592fb2

File tree

6 files changed

+377
-0
lines changed

6 files changed

+377
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.26124.0
5+
MinimumVisualStudioVersion = 15.0.26124.0
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ManageACLsExample", "ManageACLsExample\ManageACLsExample.csproj", "{D7F8ADB4-BF61-4A1E-8B71-310B9D1E7F3A}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Debug|x64 = Debug|x64
12+
Debug|x86 = Debug|x86
13+
Release|Any CPU = Release|Any CPU
14+
Release|x64 = Release|x64
15+
Release|x86 = Release|x86
16+
EndGlobalSection
17+
GlobalSection(SolutionProperties) = preSolution
18+
HideSolutionNode = FALSE
19+
EndGlobalSection
20+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
21+
{D7F8ADB4-BF61-4A1E-8B71-310B9D1E7F3A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
22+
{D7F8ADB4-BF61-4A1E-8B71-310B9D1E7F3A}.Debug|Any CPU.Build.0 = Debug|Any CPU
23+
{D7F8ADB4-BF61-4A1E-8B71-310B9D1E7F3A}.Debug|x64.ActiveCfg = Debug|Any CPU
24+
{D7F8ADB4-BF61-4A1E-8B71-310B9D1E7F3A}.Debug|x64.Build.0 = Debug|Any CPU
25+
{D7F8ADB4-BF61-4A1E-8B71-310B9D1E7F3A}.Debug|x86.ActiveCfg = Debug|Any CPU
26+
{D7F8ADB4-BF61-4A1E-8B71-310B9D1E7F3A}.Debug|x86.Build.0 = Debug|Any CPU
27+
{D7F8ADB4-BF61-4A1E-8B71-310B9D1E7F3A}.Release|Any CPU.ActiveCfg = Release|Any CPU
28+
{D7F8ADB4-BF61-4A1E-8B71-310B9D1E7F3A}.Release|Any CPU.Build.0 = Release|Any CPU
29+
{D7F8ADB4-BF61-4A1E-8B71-310B9D1E7F3A}.Release|x64.ActiveCfg = Release|Any CPU
30+
{D7F8ADB4-BF61-4A1E-8B71-310B9D1E7F3A}.Release|x64.Build.0 = Release|Any CPU
31+
{D7F8ADB4-BF61-4A1E-8B71-310B9D1E7F3A}.Release|x86.ActiveCfg = Release|Any CPU
32+
{D7F8ADB4-BF61-4A1E-8B71-310B9D1E7F3A}.Release|x86.Build.0 = Release|Any CPU
33+
EndGlobalSection
34+
EndGlobal
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
namespace ManageACLsExample
5+
{
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Threading.Tasks;
9+
using Amazon;
10+
using Amazon.S3;
11+
using Amazon.S3.Model;
12+
13+
/// <summary>
14+
/// This example shows how to manage Amazon Simple Storage Service
15+
/// (Amazon S3) Access Control Lists (ACLs) to control Amazon S3 bucket
16+
/// access. The example was created with the AWS SDK for .NET version 3.7
17+
/// and .NET Core 5.0.
18+
/// </summary>
19+
public class ManageACLs
20+
{
21+
public static async Task Main()
22+
{
23+
string bucketName = "doc-example-bucket1";
24+
string newBucketName = "doc-example-bucket2";
25+
string keyName = "sample-object.txt";
26+
string emailAddress = "[email protected]";
27+
28+
// If the AWS region where your bucket is located is different from
29+
// the region defined for the default user, pass the S3 bucket's
30+
// name to the client constructor. It should look like this:
31+
// RegionEndpoint bucketRegion = RegionEndpoint.USWest2;
32+
IAmazonS3 client = new AmazonS3Client();
33+
34+
await TestBucketObjectACLsAsync(client, bucketName, newBucketName, keyName, emailAddress);
35+
}
36+
37+
/// <summary>
38+
/// Creates a new S3 bucket with a canned ACL, then retrieves the ACL
39+
/// infrmation and then adds a new ACL to one of the objects in the
40+
/// S3 bucket.
41+
/// </summary>
42+
/// <param name="client">The initialized S3 client object used to call
43+
/// methods to create a bucket, get an ACL, and add a different ACL to
44+
/// one of the objects.</param>
45+
/// <param name="bucketName">A string representing the original S3
46+
/// bucket name.</param>
47+
/// <param name="newBucketName">A string representing the name of the
48+
/// new bucket that will be created.</param>
49+
/// <param name="keyName">A string representing the key name of an S3
50+
/// object for which we will change the ACL.</param>
51+
/// <param name="emailAddress">A string representing the email address
52+
/// belonging to the person to whom access to the S3 bucket will be
53+
/// granted.</param>
54+
public static async Task TestBucketObjectACLsAsync(IAmazonS3 client, string bucketName, string newBucketName, string keyName, string emailAddress)
55+
{
56+
try
57+
{
58+
// Create a new S3 bucket and specify canned ACL.
59+
var success = await CreateBucketWithCannedACLAsync(client, newBucketName);
60+
61+
// Get the ACL on a bucket.
62+
await GetBucketACLAsync(client, bucketName);
63+
64+
// Add (replace) the ACL on an object in a bucket.
65+
await AddACLToExistingObjectAsync(client, bucketName, keyName, emailAddress);
66+
}
67+
catch (AmazonS3Exception amazonS3Exception)
68+
{
69+
Console.WriteLine($"Exception: {amazonS3Exception.Message}");
70+
}
71+
}
72+
73+
/// <summary>
74+
/// Creates a new S3 bucket with a canned ACL attached.
75+
/// </summary>
76+
/// <param name="client">The initialized client object used to call
77+
/// PutBucketAsync.</param>
78+
/// <param name="newBucketName">A string representing the name of the
79+
/// new S3 bucket.</param>
80+
/// <returns>Returns a boolean value indicating success or failure.</returns>
81+
public static async Task<bool> CreateBucketWithCannedACLAsync(IAmazonS3 client, string newBucketName)
82+
{
83+
var request = new PutBucketRequest()
84+
{
85+
BucketName = newBucketName,
86+
BucketRegion = S3Region.EUW1,
87+
88+
// Add a canned ACL.
89+
CannedACL = S3CannedACL.LogDeliveryWrite,
90+
};
91+
92+
var response = await client.PutBucketAsync(request);
93+
return response.HttpStatusCode == System.Net.HttpStatusCode.OK;
94+
}
95+
96+
/// <summary>
97+
/// Retrieves the ACL associated with the S3 bucket name in the
98+
/// bucketName parameter.
99+
/// </summary>
100+
/// <param name="client">The initialized client object used to call
101+
/// PutBucketAsync.</param>
102+
/// <param name="bucketName">The S3 bucket for which we want to get the
103+
/// ACL list.</param>
104+
/// <returns>Returns an S3AccessCntrolList returned from the call to
105+
/// GetACLAsync.</returns>
106+
public static async Task<S3AccessControlList> GetBucketACLAsync(IAmazonS3 client, string bucketName)
107+
{
108+
GetACLResponse response = await client.GetACLAsync(new GetACLRequest
109+
{
110+
BucketName = bucketName,
111+
});
112+
113+
return response.AccessControlList;
114+
}
115+
116+
/// <summary>
117+
/// Adds a new ACL to an existing object in the S3 bucket.
118+
/// </summary>
119+
/// <param name="client">The initialized client object used to call
120+
/// PutBucketAsync.</param>
121+
/// <param name="bucketName">A string representing the name of the S3
122+
/// bucket where the object we want to which we want to apply a new ACL.</param>
123+
/// <param name="keyName">A string representing the name of the object
124+
/// to which we want to apply the new ACL.</param>
125+
/// <param name="emailAddress">The email address of the person to whom
126+
/// we will be applying to whom access will be granted.</param>
127+
public static async Task AddACLToExistingObjectAsync(IAmazonS3 client, string bucketName, string keyName, string emailAddress)
128+
{
129+
// Retrieve the ACL for an object.
130+
GetACLResponse aclResponse = await client.GetACLAsync(new GetACLRequest
131+
{
132+
BucketName = bucketName,
133+
Key = keyName,
134+
});
135+
136+
S3AccessControlList acl = aclResponse.AccessControlList;
137+
138+
// Retrieve the owner.
139+
Owner owner = acl.Owner;
140+
141+
// Clear existing grants.
142+
acl.Grants.Clear();
143+
144+
// Add a grant to reset the owner's full permission
145+
// (the previous clear statement removed all permissions).
146+
var fullControlGrant = new S3Grant
147+
{
148+
Grantee = new S3Grantee { CanonicalUser = acl.Owner.Id },
149+
};
150+
acl.AddGrant(fullControlGrant.Grantee, S3Permission.FULL_CONTROL);
151+
152+
// Specify email to identify grantee for granting permissions.
153+
var grantUsingEmail = new S3Grant
154+
{
155+
Grantee = new S3Grantee { EmailAddress = emailAddress },
156+
Permission = S3Permission.WRITE_ACP,
157+
};
158+
159+
// Specify log delivery group as grantee.
160+
var grantLogDeliveryGroup = new S3Grant
161+
{
162+
Grantee = new S3Grantee { URI = "http://acs.amazonaws.com/groups/s3/LogDelivery" },
163+
Permission = S3Permission.WRITE,
164+
};
165+
166+
// Create a new ACL.
167+
var newAcl = new S3AccessControlList
168+
{
169+
Grants = new List<S3Grant> { grantUsingEmail, grantLogDeliveryGroup },
170+
Owner = owner,
171+
};
172+
173+
// Set the new ACL. We're throwing away the response here.
174+
_ = await client.PutACLAsync(new PutACLRequest
175+
{
176+
BucketName = bucketName,
177+
Key = keyName,
178+
AccessControlList = newAcl,
179+
});
180+
}
181+
}
182+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net5.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="AWSSDK.Core" Version="3.7.0.37" />
10+
<PackageReference Include="AWSSDK.S3" Version="3.7.1.7" />
11+
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
12+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
13+
<PrivateAssets>all</PrivateAssets>
14+
</PackageReference>
15+
</ItemGroup>
16+
17+
</Project>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.26124.0
5+
MinimumVisualStudioVersion = 15.0.26124.0
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ManageObjectACLExample", "ManageObjectACLExample\ManageObjectACLExample.csproj", "{82EF5A4C-2964-45E9-BDB6-E755C1F3B9EB}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Debug|x64 = Debug|x64
12+
Debug|x86 = Debug|x86
13+
Release|Any CPU = Release|Any CPU
14+
Release|x64 = Release|x64
15+
Release|x86 = Release|x86
16+
EndGlobalSection
17+
GlobalSection(SolutionProperties) = preSolution
18+
HideSolutionNode = FALSE
19+
EndGlobalSection
20+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
21+
{82EF5A4C-2964-45E9-BDB6-E755C1F3B9EB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
22+
{82EF5A4C-2964-45E9-BDB6-E755C1F3B9EB}.Debug|Any CPU.Build.0 = Debug|Any CPU
23+
{82EF5A4C-2964-45E9-BDB6-E755C1F3B9EB}.Debug|x64.ActiveCfg = Debug|Any CPU
24+
{82EF5A4C-2964-45E9-BDB6-E755C1F3B9EB}.Debug|x64.Build.0 = Debug|Any CPU
25+
{82EF5A4C-2964-45E9-BDB6-E755C1F3B9EB}.Debug|x86.ActiveCfg = Debug|Any CPU
26+
{82EF5A4C-2964-45E9-BDB6-E755C1F3B9EB}.Debug|x86.Build.0 = Debug|Any CPU
27+
{82EF5A4C-2964-45E9-BDB6-E755C1F3B9EB}.Release|Any CPU.ActiveCfg = Release|Any CPU
28+
{82EF5A4C-2964-45E9-BDB6-E755C1F3B9EB}.Release|Any CPU.Build.0 = Release|Any CPU
29+
{82EF5A4C-2964-45E9-BDB6-E755C1F3B9EB}.Release|x64.ActiveCfg = Release|Any CPU
30+
{82EF5A4C-2964-45E9-BDB6-E755C1F3B9EB}.Release|x64.Build.0 = Release|Any CPU
31+
{82EF5A4C-2964-45E9-BDB6-E755C1F3B9EB}.Release|x86.ActiveCfg = Release|Any CPU
32+
{82EF5A4C-2964-45E9-BDB6-E755C1F3B9EB}.Release|x86.Build.0 = Release|Any CPU
33+
EndGlobalSection
34+
EndGlobal
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX - License - Identifier: Apache - 2.0
3+
4+
namespace ManageObjectACLExample
5+
{
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Threading.Tasks;
9+
using Amazon.S3;
10+
using Amazon.S3.Model;
11+
12+
/// <summary>
13+
/// This example shows how to work with the Access Control List (ACL) of an
14+
/// object in an Amazon Simple Storage Service (Amazon S3) bucket. The
15+
/// example was created with the AWS SDK for .NET version 3.7 and .NET
16+
/// Core 5.0.
17+
/// </summary>
18+
public class ManageObjectACL
19+
{
20+
public static async Task Main()
21+
{
22+
string bucketName = "doc-example-bucket";
23+
string keyName = "example-bucket.txt";
24+
string emailAddress = "[email protected]";
25+
26+
// If the AWS Region of the default user is different from the AWS
27+
// Region where the Amazon S3 bucket is located, pass the AWS Region
28+
// to the S3 client constructor. Like this:
29+
// RegionEndpoint bucketRegion = RegionEndpoint.USWest2;
30+
IAmazonS3 client = new AmazonS3Client();
31+
await TestObjectACLTestAsync(client, bucketName, keyName, emailAddress);
32+
}
33+
34+
/// <summary>
35+
/// This method first retrieves and then clears the ACL for an object.
36+
/// </summary>
37+
/// <param name="client">The initialized S3 client object which will be
38+
/// used to get and change the ACL for the S3 object.</param>
39+
/// <param name="bucketName">A string representing the name of the S3
40+
/// bucket where the object whose ACL will be modified is stored.</param>
41+
/// <param name="keyName">The key name of the S3 object whose ACL will
42+
/// be modified.</param>
43+
/// <param name="emailAddress">The email address to use in defining the
44+
/// grant for the new ACL.</param>
45+
public static async Task TestObjectACLTestAsync(IAmazonS3 client, string bucketName, string keyName, string emailAddress)
46+
{
47+
try
48+
{
49+
// Retrieve the ACL for the object.
50+
GetACLResponse aclResponse = await client.GetACLAsync(new GetACLRequest
51+
{
52+
BucketName = bucketName,
53+
Key = keyName,
54+
});
55+
56+
S3AccessControlList acl = aclResponse.AccessControlList;
57+
58+
// Retrieve the owner (we use this to re-add permissions after we clear the ACL).
59+
Owner owner = acl.Owner;
60+
61+
// Clear existing grants.
62+
acl.Grants.Clear();
63+
64+
// Add a grant to reset the owner's full permission (the previous clear statement removed all permissions).
65+
S3Grant fullControlGrant = new ()
66+
{
67+
Grantee = new S3Grantee { CanonicalUser = owner.Id },
68+
Permission = S3Permission.FULL_CONTROL,
69+
};
70+
71+
// Describe the grant for the permission using an email address.
72+
S3Grant grantUsingEmail = new ()
73+
{
74+
Grantee = new S3Grantee { EmailAddress = emailAddress },
75+
Permission = S3Permission.WRITE_ACP,
76+
};
77+
acl.Grants.AddRange(new List<S3Grant> { fullControlGrant, grantUsingEmail });
78+
79+
// Set a new ACL.
80+
PutACLResponse response = await client.PutACLAsync(new PutACLRequest
81+
{
82+
BucketName = bucketName,
83+
Key = keyName,
84+
AccessControlList = acl,
85+
});
86+
}
87+
catch (AmazonS3Exception amazonS3Exception)
88+
{
89+
Console.WriteLine($"Error: {amazonS3Exception.Message}");
90+
}
91+
}
92+
}
93+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net5.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="AWSSDK.Core" Version="3.7.0.37" />
10+
<PackageReference Include="AWSSDK.S3" Version="3.7.1.7" />
11+
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
12+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
13+
<PrivateAssets>all</PrivateAssets>
14+
</PackageReference>
15+
</ItemGroup>
16+
17+
</Project>

0 commit comments

Comments
 (0)