Upload the file to the S3 Recycle Bin folder using the ASP.NET SDK

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

This is the code that I use to upload a file to one container:

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 this code helps you.

+7
source share
3 answers

To add a file to a folder in a bucket, you need to update the PutObjectRequest key 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 is a message that says downloading files to a folder. However, they use TransferUtilityUploadRequest, but should work with PutObjectRequest. Scroll down the page in the corresponding example.

This post shows how to create a folder without downloading a file.

Hope this will be helpful

Edit: Updated code to use used block instead of calling Dispose for best practices.

+17
source

See how functionlity function

1.create an AmazonS3 object

2.Create a bucket

3. Add a new file to Amazon S3

4. Get file from Amazon S3

5.Remove file from Amazon S3

Amazon

+2
source

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" /> 

They can be obtained from your account on the AWS console. They should also use the names given here, as they are predefined by the AWS library.

+2
source

All Articles