Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Uploading Files to AWS

DZone's Guide to

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!

· Cloud Zone
Free Resource

Are you joining the containers revolution? Start leveraging container management using Platform9's ultimate guide to Kubernetes deployment.

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.

Using Containers? Read our Kubernetes Comparison eBook to learn the positives and negatives of Kubernetes, Mesos, Docker Swarm and EC2 Container Services.

Topics:
aws ,tutorial ,cloud

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

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}