Integrate Amazon S3 .Net app

Recently I faced the task of use in your application to the cloud storage. Not stalkivalis this before, I thought that this task will take more time. But, to my surprise, already developed enough convenient tools for interaction. An example of their use is described in this topic.

Consider how files are stored on Amazon: we have Basket(Buckets) directly in which you can upload files and create directories. Access is through two key Access Key and Secret Key, which you can view on a separate page of the profile.
/ > So, we have a task to implement in my application the ability to upload files and delete already downloaded. Create a new Windows Forms project. Add using NuGet Amazon SDK
the
PM> Install-Package AWSSDK

Let's add on our form two ListBox'a: one to display the list of baskets, the second to display the list of files in the selected basket. Also, add a button to get a list and upload the file to the specified bin and delete the selected file:


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

And consider the code to get a list of baskets:
the
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: I create a client object and call it the required method. It should be noted that in addition to the name of the basket there is more information about the creation date.
Code to get a list of files and directories in the selected basket:
the
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 end we get the final list S3Objects corresponding to the files. You can also get "raw" response XML has the following format:

the
<?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>Wed, 20 Jun 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 code to upload a file:
the
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 is a downloadable file you can specify which cart to download it, what are the privacy settings. To download the file to the directory of the recycle bin, just add to path the directory name with a slash. If the specified directory is missing, it will be created.
Well code removal:
the
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 content:
Getting Started with the AWS SDK for .NET
GDownload Source Code
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

Briefly on how to make your Qt geoservice plugin

Database replication PostgreSQL-based SymmetricDS

Yandex.Widget + adjustIFrameHeight + MooTools