DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Uploading Files to AWS

Uploading Files to AWS

In this post we take a quick look at how to upload files to AWS S3 using C#. It's pretty simple, so come check it out!

Simon Foster user avatar by
Simon Foster
·
Jul. 04, 17 · Tutorial
Like (3)
Save
Tweet
Share
6.62K Views

Join the DZone community and get the full member experience.

Join For Free

I am a fan of Azure, but today, I have been looking at AWS — specifically, how to upload and download files.

AWS S3 stores files in buckets. I already had an AWS S3 account set up with a bucket. I am going to assume you have a bucket set up and concentrate on the code to get files in and out.

The first step is to use nuget to install the AWS packages. In nuget, the packages you want are called AWSSDK.Core and AWSSDK.S3.

The using statements you want to use are called Amazon.S3 and Amazon.S3.Transfer. Not sure why this doesn’t match nuget; this difference caught me out a couple of times.

Now to the code that uploads files:

AmazonS3Client AWSclient = new AmazonS3Client(accessKeyID, secretAccessKeyID, Amazon.RegionEndpoint.EUWest1);
TransferUtility fileTransferUtility = new TransferUtility(AWSclient);
using(FileStream streamWriter = new FileStream(path, FileMode.Open)) {
 TransferUtilityUploadRequest fileTransferUtilityRequest = new TransferUtilityUploadRequest {
  BucketName = “flawlessimages”,
   InputStream = streamWriter,
   Key = fileName
 };
 fileTransferUtility.Upload(fileTransferUtilityRequest);
}

Let's break it down and look at what it does.

AmazonS3Client AWSclient = new AmazonS3Client(accessKeyID, secretAccessKeyID, Amazon.RegionEndpoint.EUWest1);

This creates an instance of AmazonS3Client. We are passing the Access Key and Secret Access Key, both of which can be found from your Amazon S3 account in the My Security Credentials section. Amazon.RegionEndpoint.EUWest1 specifies the Amazon data center that your bucket is located in.

TransferUtility fileTransferUtility = new TransferUtility(AWSclient);

This creates an instance of TransfterUtility using the AmazonS3Client instance we created in the previous step.

using (FileStream streamWriter = new FileStream(path, FileMode.Open))    {

This opens up a filestream from a file path and specifies that the file should be opened.

TransferUtilityUploadRequest fileTransferUtilityRequest = new TransferUtilityUploadRequest {
 BucketName = “flawlessimages”,
  InputStream = streamWriter,
  Key = fileName
};
fileTransferUtility.Upload(fileTransferUtilityRequest);
}

This last step specifies which bucket to upload to, what input stream to upload, and the key to use. The key is just the AWS way of referring to files, more commonly referred to as the filename.

This is all you need to do to upload a file to your Bucket. The file will be located at https://s3-eu-west-1.amazonaws.com/[bucketname]/[filename]. However, by default, it will not be downloadable until you set read permission to everyone. Once you do that, anyone who has the link will be able to download your file.

This is the same permission level as any file you have on your webserver. However, AWS has a better way.

using(s3Client = new AmazonS3Client(accessKeyID, secretAccessKeyID, Amazon.RegionEndpoint.USEast1)) {
 GetPreSignedUrlRequest request1 = new GetPreSignedUrlRequest {
  BucketName = bucketName,
   Key = filename,
   Expires = DateTime.Now.AddMinutes(5)
 };
 urlString = s3Client.GetPreSignedURL(request1);
}

Here we are generating a URL to download the file, but we are specifying that it is only valid for five minutes. This means that if you share the URL it will only work for five minutes, and after that, AWS will give an access denied message.

This is much better security than you have on a typical web server, and easy to implement. Every time a user clicks on a download link, you generate a new pre-signed URL and send the download to the browser. And as long as this process doesn’t take longer than five minutes, the user will never know.

AWS

Published at DZone with permission of Simon Foster, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Handling Automatic ID Generation in PostgreSQL With Node.js and Sequelize
  • The Top 3 Challenges Facing Engineering Leaders Today—And How to Overcome Them
  • Getting Started With JMS-ActiveMQ: Explained in a Simple Way
  • When AI Strengthens Good Old Chatbots: A Brief History of Conversational AI

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: