Projects often require images to be stored over a period of time, and people use their web servers to store. We wondered, why not store images on a webshare instead? That would prevent unnecessary hits on our servers when people wanted to access those images. It would also help us to:

    • Prevent server injection, and,
    • Ensure security in terms of user file segregation

    I thought doing this would be rather easy. It wasn’t the first time it was being done and I was confident I would get all the information I needed online. As I got started though, I discovered certain issues with amazon file upload, which a lot of others doing something similar might have come across as well:

    • Not much of help is available online for this. Amazon also has sample applications like “Pet Board” which uploads files on Amazon but I found the sample application is quiet complex and tightly coupled with Amazon’s own database. Additionally configuration and running will cost you 1 USD
    • The application is not in Visual Studio 2012 and will not help you to identify the Client class to communicate with Amazon service
    • Most of the links point to download .NET SDK which is not required unless you are creating application just to upload files on amazon

    So, as a fruit of my frustrations, this blogpost shares how I went about configuring an Amazon S3 Account along with sample code to upload files directly on Amazon S3.

    Configuring Amazon S3 Account

    Amazon allow users to sign-up for free and use 5GB of space. To create new account, you can log on to http://aws.amazon.com/s3/ and signup.

    Note: Please do not hesitate to share your card detail. In my experience, the money does not get deducted unless you exceed the limit and that too very nominally.

    Creating Bucket

    Once the account is created you are asked to create your bucket. Bucket is the place where all your data gets stored. Please create your own bucket and you can eventually create folders inside the bucket.

    Once bucket is created, your console will look something like this

    file upload to amazon

    Creating Access-Key and Secret-Key

    To upload an image you require to get your Access Key and Secret ID which is required for security handshake while uploading or connecting to the bucket.

    Steps:

    • Under your name, click on Security Credentials.

    amazon file upload

    • Click on ‘Continue to Security Credential’ and you will see Access Keys ID and Secret Access Key

    amazon file upload

    • Click on “Create new access key” and then you can download the key file which will contain the Key ID and Access Key.

    amazon file upload

    Now you are ready to communicate and get an Amazon file upload completed on S3 from the ASP.NET application.

    2. Configuring Amazon Client in ASP.NET

    Adding Amazon reference in solution

    • Right click the reference and select “Manage NuGet Package” and search for “AWS”
    • Add Reference to “AWS SDK for .NET” in the solution

    Adding settings in web.config file

    • Add below setting inside <appSettings> Section.
        <add key="AWSAccessKey" value="Your Access ID"/>
    
        <add key="AWSSecretKey" value="Your Access Key/>
    
        <add key="AWSRegion" value="Region of Storage" />

    Code to upload file on Amazon

    After uploading file on server, it was time to upload on Amazon –  I created a Static class for Amazon file upload on Amazon which would accept the file name and path of the file stored on the web server.

    Here is the code for Amazon file uploads.

    using System;
    using System.Linq;
    using System.Web;
    using Amazon.S3;
    using Amazon.S3.Model;
    using System.Drawing.Imaging;
    using Amazon;
    using System.IO;
    using System.Drawing;
    namespace UploadPhoto
    {
        public static class UploadOnAmazon
        {
            public static void UploadFileOnAmazon(string filepath,string filename)
            {
                // Save the new graphic file to the server
                    IAmazonS3 client;
               using (client = AWSClientFactory.CreateAmazonS3Client())
                {
                    PutObjectRequest request = new PutObjectRequest();
                    request.BucketName = "YourBucketName";
                    request.CannedACL = S3CannedACL.PublicRead;
                    request.Key = "images/" + filename;
                    request.FilePath = filepath;
                    PutObjectResponse response = client.PutObject(request);
                }
            }
        }
    }

     

    Originally contributed by Vivek Singh, WukiLabs Alumni

    About Author

    You may also like

    No Comment

    You can post first response comment.

    Leave A Comment

    Please enter your name. Please enter an valid email address. Please enter a message.