Click here to Skip to main content
12,819,019 members (25,290 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


44 bookmarked
Posted 24 Apr 2011

Beginning with Amazon S3

, 24 Apr 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
This article is about how to develop an application with Amazon S3 using C#.


Amazon S3 (Amazon Simple Storage Service) is a service that allows to store files online. This article is about how a beginner can develop applications with Amazon S3 using C#.

Amazon S3 can help us store data as files using a folder structure, similar to an online hard disk. We can create files, folders, upload a file, delete a file/folder, etc. We will talk about how this can be done using C# from a developer's point of view.

The first thing, you need is the Amazon Web Service SDK. Secondly, you need an Amazon Web Service account with an access key and private key to connect to Amazon S3.


Create an AmazonS3 object

Now let us start with Amazon S3. We need an Amazon S3 account, access key, and private key, to create an AmazonS3 object. This object controls all the actions to interact with the Amazon S3 server.

const string AWS_ACCESS_KEY = "put_your_AWS_access_key_here";
const string AWS_SECRET_KEY = "put_your_AWS_secret_key _here";
AmazonS3 client = new AmazonS3(AWS_ACCESS_KEY, AWS_SECRET_KEY)

Normally, these keys are stored in the “web.config” and we access them by code:

// In application config
    <add key="AWSAccessKey" value="put_your_AWS_access_key_here"/>
    <add key="AWSSecretKey" value="put_your_AWS_secret_key _here"/>

The code:

// Function to get it
public static AmazonS3 GetS3Client()
    NameValueCollection appConfig = ConfigurationManager.AppSettings;

    AmazonS3 s3Client = AWSClientFactory.CreateAmazonS3Client(
    return s3Client;

Create a bucket

If you want to store data in S3, you need to create a bucket. It is similar to a root folder in Windows. In Amazon S3, the maximum number of buckets is 100 and the names of buckets are unique globally.

string BUCKET_NAME = "stormspiritbucket1";
ListBucketsResponse response = client.ListBuckets();
bool found = false;
foreach (S3Bucket bucket in response.Buckets)
    if (bucket.BucketName == BUCKET_NAME)
        found = true;
if (found == false)
    client.PutBucket(new PutBucketRequest().WithBucketName(BUCKET_NAME));

After that, in the Amazon S3 console, you will see this bucket:

Add a new file to Amazon S3

The first thing to do to store a file is adding the file to the Amazon S3 server. There are many ways to do it, and I will show two of the most popular ways.

  1. Create a file in Amazon S3:
  2. To create a file in Amazon S3, we need the following information:

    • FileKey: It can be understood as the file name, but it is more specific. It must be unique and should include the folder path, like a physical address in Windows.
    • ContentBody: The content of the new file.
    String S3_KEY = "Demo Create File.txt";
    PutObjectRequest request = new PutObjectRequest();
    request.WithContentBody("This is body of S3 object.");

    The new file will be created as a text file.

    You want to create a folder in S3? No problem, we use the FileKey with the character '/' at the end to show that you want to create a folder.

    String S3_KEY = "Demo Create folder/";
    PutObjectRequest request = new PutObjectRequest();

    And this is the result:

    If you want to create a file in the folder, you need change the file key to include the folder name:

    String S3_KEY = "Demo Create folder/" + "Demo Create File.txt";
    PutObjectRequest request = new PutObjectRequest();
    request.WithContentBody("This is body of S3 object.");

    And this is the result:

    However, a problem when you create a file with the key "TestFolder/File.txt" is the folder 'TestFolder' does not exist. When you do this, in the Amazon S3 console, you will see a folder 'TestFolder' and this folder will contain the file 'File.txt'. But the S3 database will not contain the folder 'TestFolder', and if you delete the file 'File.txt', the folder 'TestFolder' will be deleted too. You cannot get any item from S3 with the key 'TestFolder/'.

  3. Upload files to Amazon S3:
  4. This method is to upload a file from a local computer to Amazon S3. Of course, we need a file key to upload a file. We can upload a local file using a file path, like this:

    //S3_KEY is name of file we want to upload
    PutObjectRequest request = new PutObjectRequest();

    And we can upload a file from a stream too:

    //S3_KEY is name of file we want to upload
    PutObjectRequest request = new PutObjectRequest();

Get a file from Amazon S3

When you want to get a file from S3, use the method GetObject:

using (s3Client)
    MemoryStream file = new MemoryStream();
        GetObjectResponse r = s3Client.GetObject(new GetObjectRequest()
            BucketName = bucketName,
            Key = fileKey
            long transferred = 0L;
            BufferedStream stream2 = new BufferedStream(r.ResponseStream);
            byte[] buffer = new byte[0x2000];
            int count = 0;
            while ((count = stream2.Read(buffer, 0, buffer.Length)) > 0)
                file.Write(buffer, 0, count);
        return file;
    catch (AmazonS3Exception)
        //Show exception

If you want to list all items in S3 or in a specific folder, use the method listObject:

using (s3Client)
        ListObjectsRequest Lor = new ListObjectsRequest()
            BucketName = bucketName,
            // with Prefix is a folder Key, it will list only child of that folder
            Prefix = folderPath, 
            //with Delimiter is '/', it will not get folder.
            Delimiter = "/"
        ListObjectsResponse response1 = s3Client.ListObjects(Lor);
        foreach (S3Object s3Object in response1.S3Objects)
            //Do anything you want
    catch (AmazonS3Exception ex)
        //Show Exception

Delete a file from Amazon S3

When you want to delete an item in S3, you need the exact key for the item.

DeleteObjectRequest request = new DeleteObjectRequest()
    BucketName = bucketName,
    Key = FileKey
S3Response response = s3Client.DeleteObject(request);

Unfortunately, when deleting a folder, Amazon S3 does not support deleting its children. Therefore, this is similar to the case of creating a file in a non-existing folder. It may be a real problem, so you need to handle it with your own code. The best solution I know in this case is to list all the ‘children’ of your folder and delete them one by one.

Copy and paste

Amazon S3 supports copy and paste of files from a source to a destination:

CopyObjectRequest request = new CopyObjectRequest()
    SourceBucket = bucketName,
    SourceKey = fileKey,
    DestinationBucket = bucketName,
    DestinationKey = destinationPath
CopyObjectResponse response = s3Client.CopyObject(request);

Share file

If you have a file in Amazon S3 that you want to share with others, there are two ways to doing it:

  1. Change file permission:
  2. You know that the default permission when you create a file in S3 is private, which means only you can view and edit the file. Therefore, you need to change the permission of the file to public to share the file with others. Amazon S3 supports these permissions: AuthenticatedRead, BucketOwnerFullControl, BucketOwnerRead, NoACL, Private (default), PublicRead (share with others), PublicReadWrite.

    S3Response response1 = s3Client.SetACL(new SetACLRequest()
        CannedACL = permission,
        BucketName = bucketName,
        Key = keyName
  3. Make a public URL:
  4. Amazon S3 supports creating a public URL (PreSignedURL). With this link, a public user access a file for reading for a specified period. After the period, the link will be disposes. This is the most popular way to share files in S3.

    string preSignedURL = s3Client.GetPreSignedURL(new GetPreSignedUrlRequest()
        BucketName = bucketName,
        Key = keyName,
        Expires = System.DateTime.Now.AddMinutes(period)


Amazon Simple Storage Service has a lot of useful features and is very useful for anyone who wants to develop an application that need to be store data online. It has the structure and UI similar to Windows apps, and with the Amazon Web Service SDK, beginners get an easy start.


  • 21 Apr, 2011: Initial post.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

StormSpirit Team
Student FPT University
Vietnam Vietnam
StormSpirit Team:
HiepNT - 00403
HuyHN - 00320
DucNM - 00303
ThangTS - 00098
Supervisor: Professor Nguyen Hong Ky.

You may also be interested in...

Comments and Discussions

QuestionHow to do content level search in AmazonS3 Pin
Abu RAHIM28-Feb-17 3:35
memberAbu RAHIM28-Feb-17 3:35 
QuestionPath To File format Pin
NortonC7-Apr-16 6:18
memberNortonC7-Apr-16 6:18 
QuestionThe type or namespace name 'AmazonS3' could not be found (are you missing a using directive or an assembly reference?) Pin
Member 1109252720-Nov-15 0:07
memberMember 1109252720-Nov-15 0:07 
AnswerRe: The type or namespace name 'AmazonS3' could not be found (are you missing a using directive or an assembly reference?) Pin
shrikant shukla201014-Dec-16 0:49
membershrikant shukla201014-Dec-16 0:49 
QuestionCompiler error Pin
coolsam200014-Oct-15 10:32
membercoolsam200014-Oct-15 10:32 
AnswerRe: Compiler error Pin
Member 1296599624-Jan-17 7:21
memberMember 1296599624-Jan-17 7:21 
GeneralMy vote of 5 Pin
bhalaniabhishek10-Dec-14 19:27
memberbhalaniabhishek10-Dec-14 19:27 
QuestionWonderful article...limit of 1000 records??? Pin
Miles Newey3-Sep-14 23:52
memberMiles Newey3-Sep-14 23:52 
QuestionThanks for the article Pin
Heucles Del Bianco Pelegia Junior Júnior28-Aug-14 6:18
memberHeucles Del Bianco Pelegia Junior Júnior28-Aug-14 6:18 
QuestionHow to check if a folder exists in a specific bucket ? Pin
manoj0409199015-Jul-13 18:15
membermanoj0409199015-Jul-13 18:15 
GeneralMy vote of 4 Pin
Lazaro Lima4-Feb-13 13:16
memberLazaro Lima4-Feb-13 13:16 
QuestionAWS S3, VB.NET Pin
KevinBrady5724-Nov-12 7:21
memberKevinBrady5724-Nov-12 7:21 
Very good article. Walks through the basics nicely.

I have translated much of the code you generated into VB.Net.

I am able to get the DeleteFile and CreateBucket subs to work fine. I also create a function to list all the buckets. It also works fine.

I cannot seem to get fileupload to work. It keeps returning an error "ERROR: System.IO.FileNotFoundException: The specified file does not exist at Amazon.S3.AmazonS3Client.invokePutObject(PutObjectRequest request, AsyncCallback, callback, Object state, Boolean synchronized)"

The subroutine that I retooled from your code is:
Private Sub UpLoadFile(ByVal iFlName As String)
    Dim request As New PutObjectRequest

    With request
        .BucketName = BUCKET_NAME
        .FilePath = "C:\Test"
        .Key = iFlName
    End With


    Catch ex As Exception
        MsgBox("Error occured during file upload: " & iFlName & vbCrLf & _
               "ERROR: " & ex.ToString)

    End Try

End Sub

AWS3Client is declared at the form level and has been verified to be function (by using this same object to create a bucket and delete a file).
GeneralMy vote of 5 Pin
Kanasz Robert27-Sep-12 9:28
mvpKanasz Robert27-Sep-12 9:28 
QuestionGood Article Pin
elshiekh511-Sep-12 4:09
memberelshiekh511-Sep-12 4:09 
GeneralAmazon s3 Pin
James8493916-Jul-11 10:50
memberJames8493916-Jul-11 10:50 
GeneralMy vote of 5 Pin
Kim Togo25-Apr-11 22:37
memberKim Togo25-Apr-11 22:37 
GeneralAmazon S3 freeware client Pin
cloudberryman24-Apr-11 23:14
membercloudberryman24-Apr-11 23:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 24 Apr 2011
Article Copyright 2011 by StormSpirit Team
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid