📜 ⬆️ ⬇️

We integrate Amazon S3 into .Net application

Recently, I faced the challenge of using cloud storage in my application. Not faced with a similar, I thought that this task will take more time. But, to my surprise, quite convenient tools for interaction have already been developed. An example with their use is described in this topic.

Consider how files are stored on Amazon: we have Recycle Bins (Buckets), directly into which you can upload files and create directories in them. For access, two keys Access Key and Secret Key are used , which can be viewed on a separate profile page.
So, we have a task to implement the ability to upload files and delete already loaded ones in your application. Let's create a new Windows Forms project. Add using the NuGet Amazon SDK
PM> Install-Package AWSSDK 

Add two ListBoxes to our form: one to display the list of baskets, the second to display the list of files in the selected basket. We will also add buttons for getting lists and uploading a file to the specified basket and deleting the selected file:


Add rows with keys
 string accessKey = "LASKDJFHGQPWOEIRUTY"; string secretKey = "laksdjfhgqpwoeirutyzmxncbv"; 

And consider the code for a list of baskets:
 private void btnGetBucket_Click(object sender, EventArgs e) { using (var client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKey, secretKey)) { var list = client.ListBuckets(); foreach (var bucket in list.Buckets) { lbBuckets.Items.Add(bucket.BucketName); } } } 

As you can see, there is nothing complicated here: we create a client object and call the necessary method in it. It is worth noting that in addition to the name of the basket there is still information on the date of creation.
Code to get the list of files and directories in the selected basket:
 private void btnGetList_Click(object sender, EventArgs e) { using (var client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKey, secretKey)) { if(lbBuckets.SelectedIndex < 0) return; var list = client.ListObjects( new ListObjectsRequest() { BucketName = lbBuckets.SelectedItem.ToString() }); foreach (var file in list.S3Objects) { lbFiles.Items.Add(file.Key); } } } 

At the output we get a ready list of S3Objects corresponding to the files. You can also get a “raw” response in the form of XML, which has the following format:

 <?xml version="1.0" encoding="utf-16"?> <ListObjectsResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://s3.amazonaws.com/doc/2006-03-01/"> <RequestId>Id</RequestId> <AmazonId2>Id</AmazonId2> <Name>Name</Name> <Prefix /> <S3Objects> <Key>FileName</Key> <BucketName>BucketName</BucketName> <LastModified>, 20  2012 13:00:33 GMT</LastModified> <ETag>"tag"</ETag> <Size>0</Size> <Owner> <Id>OwnerId</Id> <DisplayName>OwnerName</DisplayName> </Owner> <StorageClass>STANDARD</StorageClass> </S3Objects> <MaxKeys>1000</MaxKeys> <IsTruncated>false</IsTruncated> </ListObjectsResponse> 

Well, the file sending code:
 private void btnUpload_Click(object sender, EventArgs e) { var dlg = new OpenFileDialog(); if(dlg.ShowDialog() == DialogResult.OK) { var stream = new FileStream(dlg.FileName, FileMode.Open); using (var client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKey, secretKey)) { var request = new PutObjectRequest(); if (lbBuckets.SelectedIndex < 0) return; request .WithBucketName(lbBuckets.SelectedItem.ToString()) .WithCannedACL(S3CannedACL.PublicRead) .WithKey(Path.GetFileName(dlg.FileName)) .InputStream = stream; client.PutObject(request); } } } 

Here you can specify the downloadable file in which basket to load it, which privacy settings. To upload a file to the basket directory, just add the directory name through the slash to the path. If the specified directory is missing, it will be created.
Well, the removal code:
 private void btnDelete_Click(object sender, EventArgs e) { using (var client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKey, secretKey)) { if (lbFiles.SelectedIndex < 0) return; new DeleteObjectRequest() .WithBucketName(lbBuckets.SelectedItem.ToString()) .WithKey(lbFiles.SelectedItem.ToString()); } } 

')
Additional materials:
Getting Started with the AWS SDK for .NET
GDownload Source Code

Source: https://habr.com/ru/post/146223/


All Articles