|
| 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 | +} |
0 commit comments