Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Add a new object to an S3 Bucket
public class SomeClass
// Change the AWSProfileName to the profile you want to use in the App.config file.
// See for more details.
// You must also sign up for an Amazon S3 account for this to work
// See for details on creating an Amazon S3 account
// Change the bucketName and keyName fields to values that match your bucketname and keyname
string bucketName = "bucketname";
string keyName = "testpage";
Add this stuff in to your web/app config or else client variable will throw an exception
<add key="AwsAccessKey" value=""/>
<add key="AwsSecretKey" value=""/>
IAmazonS3 client = new AmazonS3Client(RegionEndpoint.USEast1);
void SomeClass()
void WritingAnObject()
// simple object put
PutObjectRequest request = new PutObjectRequest()
ContentBody = "this is a test",
BucketName = bucketName,
Key = keyName,
ContentType = "text/html",
PutObjectResponse response = client.PutObject(request);
request.Metadata.Add("title", "the title");
catch (AmazonS3Exception amazonS3Exception)
if (amazonS3Exception.ErrorCode != null &&
(amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") ||
Console.WriteLine("Please check the provided AWS Credentials.");
Console.WriteLine("If you haven't signed up for Amazon S3, please visit");
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
You can’t perform that action at this time.