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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With