Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload a file to S3 bucket's folder using ASP.NET SDK

How do I use AWS SDK for ASP.NET to upload a file to a specific folder? - I was able to upload files by specifying the bucket name (request.WithBucketName), but I want to be able to upload a file to a specific folder within the bucket itself.

This is the code that I use to upload a file to a single bucket:

public bool UploadFileToS3(string uploadAsFileName, Stream ImageStream, S3CannedACL filePermission, S3StorageClass storageType, string toWhichBucketName)
{

    try
    {
        client = Amazon.AWSClientFactory.CreateAmazonS3Client(MY_AWS_ACCESS_KEY_ID, MY_AWS_SECRET_KEY);
        PutObjectRequest request = new PutObjectRequest();
        request.WithKey(uploadAsFileName);
        request.WithInputStream(ImageStream);
        request.WithBucketName(toWhichBucketName);
        request.CannedACL = filePermission;
        request.StorageClass = storageType;

        client.PutObject(request);
        client.Dispose();
    }
    catch
    {            
        return false;
    }
    return true;

}

Hope that this code will help you out.

like image 370
Idan Shechter Avatar asked Aug 12 '11 12:08

Idan Shechter


3 Answers

To add a file to a folder in a bucket, you need to update the Key of the PutObjectRequest to include the folder before the file name.

public bool UploadFileToS3(string uploadAsFileName, Stream ImageStream, S3CannedACL filePermission, S3StorageClass storageType, string toWhichBucketName)
{
    try
    {
        using(client = Amazon.AWSClientFactory.CreateAmazonS3Client(MY_AWS_ACCESS_KEY_ID, MY_AWS_SECRET_KEY))
        {
           PutObjectRequest request = new PutObjectRequest();
           request.WithKey( "folder" + "/" + uploadAsFileName );
           request.WithInputStream(ImageStream);
           request.WithBucketName(toWhichBucketName);
           request.CannedACL = filePermission;
           request.StorageClass = storageType;

           client.PutObject(request);
        }
    }
    catch
    {            
        return false;
    }
    return true;
}

This post that talks about uploading files to folder. They are using a TransferUtilityUploadRequest though, but it should work with the PutObjectRequest. Scroll to the bottom for the relevant example.

This post shows how to create a folder without uploading a file to it.

Hope this is helpful

Edit: Updated the code to use a using block instead of calling Dispose per best practices.

like image 174
Brian Dishaw Avatar answered Nov 13 '22 23:11

Brian Dishaw


Look Like Following functionlity

1.Create an AmazonS3 object

2.Create a bucket

3.Add a new file to Amazon S3

4.Get a file from Amazon S3

5.Delete a file from Amazon S3

Amazon

like image 37
Sagar Rawal Avatar answered Nov 13 '22 22:11

Sagar Rawal


super easy way:

using System;
using System.Web;
using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
using System.Configuration;

/// <summary>
/// Summary description for AWShandler
/// </summary>
public static class AWSHandler
{
    public static void sendFileToS3(string fileName, string storeLocation)
    {
        try
        {
            AmazonS3Client client = new AmazonS3Client(RegionEndpoint.EUWest1);
            PutObjectRequest request = new PutObjectRequest();
            request.BucketName = ConfigurationManager.AppSettings["AWSBucket"].ToString();
            request.FilePath = fileName;
            request.Key = storeLocation + fileName;
            request.ContentType = MimeMapping.GetMimeMapping(fileName);
            PutObjectResponse response = client.PutObject(request);
        }
        catch (Exception ex)
        {
            // use a logger and handle it
        }
    }
}

you just need to put your keys in the web/app.config file:

<add key="AWSAccessKey" Value="yourKey" />
<add key="AWSSecretKey" Value="yourSecret" />

These can be obtained from you account page in the AWS console. They must use the names quoted here too, as they are pre-defined by the AWS library.

like image 24
Rich Avatar answered Nov 13 '22 22:11

Rich