Skip to content

Instantly share code, notes, and snippets.

@hayashih
Created November 24, 2010 09:12
Show Gist options
  • Save hayashih/713370 to your computer and use it in GitHub Desktop.
Save hayashih/713370 to your computer and use it in GitHub Desktop.
Set and Get S3 AccessControlList. Require AWS SDK for .NET http://aws.amazon.com/sdkfornet/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Amazon.S3;
using Amazon.S3.Model;
using System.Collections.Specialized;
using System.Configuration;
namespace AWS_S3_Test
{
class Program
{
static string accessKeyID = "Your AccessKey ID";
static string secretAccessKeyID = "Your SecretAccessKey ID";
static string awsAccountNumbers = "Your Account Numbers";
static string bucketName = "S3BucketName";
static AmazonS3 s3Client = null;
static void Main(string[] args)
{
using (s3Client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKeyID, secretAccessKeyID))
{
GetS3ACL();
SetS3ACL();
}
}
static void GetS3ACL()
{
try
{
String filePath = "images/test.jpg";
GetACLRequest getACLRequest = new GetACLRequest();
getACLRequest
.WithBucketName(bucketName)
.WithKey(filePath);
using (GetACLResponse response = s3Client.GetACL(getACLRequest))
{
String xml = response.ResponseXml;
Console.WriteLine(xml);
}
}
catch (AmazonS3Exception amazonS3Exception)
{
if (amazonS3Exception.ErrorCode != null &&
(amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") ||
amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
{
Console.WriteLine("Please check the provided AWS Credentials.");
Console.WriteLine("If you haven't signed up for Amazon S3, please visit http://aws.amazon.com/s3");
}
else
{
Console.WriteLine("An error occurred with the message '{0}' when writing an object", amazonS3Exception.Message);
}
}
}
static void SetS3ACL()
{
try
{
S3AccessControlList acl = new S3AccessControlList();
S3Grantee grantee = new S3Grantee();
grantee.CanonicalUser = new Amazon.S3.Model.Tuple<string, string>()
{
First = "User1 CanonicalUserId"
,
Second = "User1 Display Name"
};
acl.AddGrant(grantee, S3Permission.FULL_CONTROL);
S3Grantee grantee2 = new S3Grantee();
grantee2.CanonicalUser = new Amazon.S3.Model.Tuple<string, string>()
{
First = "User2 CanonicalUserId"
,
Second = "User2 Display Name"
};
acl.AddGrant(grantee2, S3Permission.FULL_CONTROL);
acl.Owner = new Owner()
.WithDisplayName("Owner Display Name")
.WithId("Owner CanonicalUserId");
SetACLRequest setACLResuest = new SetACLRequest();
String filePath = "images/test.jpg";
setACLResuest
.WithKey(filePath)
.WithBucketName(bucketName)
.WithACL(acl);
using (SetACLResponse response = s3Client.SetACL(setACLResuest))
{
String resultXml = response.ResponseXml;
}
}
catch (AmazonS3Exception amazonS3Exception)
{
if (amazonS3Exception.ErrorCode != null &&
(amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") ||
amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
{
Console.WriteLine("Please check the provided AWS Credentials.");
Console.WriteLine("If you haven't signed up for Amazon S3, please visit http://aws.amazon.com/s3");
}
else
{
Console.WriteLine("An error occurred with the message '{0}' when writing an object", amazonS3Exception.Message);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment