Added AWS SDK. Re-wrote S3 implementation to use pre-signed URLs for PUT.

This commit is contained in:
Alan Edwardes 2015-04-19 00:48:30 +01:00
parent 34c27f7ef2
commit ae89ebd8bc
7 changed files with 93 additions and 99 deletions

View file

@ -25,13 +25,17 @@ You should have received a copy of the GNU General Public License
// Credits: https://github.com/alanedwardes
using Newtonsoft.Json;
using ShareX.HelpersLib;
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Linq;
using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
using Amazon.Runtime;
using System.Collections.Specialized;
namespace ShareX.UploadersLib.FileUploaders
{
@ -39,9 +43,9 @@ public sealed class AmazonS3 : FileUploader
{
private AmazonS3Settings S3Settings { get; set; }
public AmazonS3(AmazonS3Settings accessKeys)
public AmazonS3(AmazonS3Settings s3Settings)
{
S3Settings = accessKeys;
S3Settings = s3Settings;
}
private string GetObjectStorageClass()
@ -49,51 +53,26 @@ private string GetObjectStorageClass()
return S3Settings.UseReducedRedundancyStorage ? "REDUCED_REDUNDANCY" : "STANDARD";
}
// Helper class to construct the S3 policy document (below)
private class S3PolicyCondition : Dictionary<string, string>
private RegionEndpoint GetCurrentRegion()
{
public S3PolicyCondition(string key, string value)
try
{
Add(key, value);
return RegionEndpoint.GetBySystemName(S3Settings.Region);
}
catch (ArgumentException)
{
return RegionEndpoint.USWest1;
}
}
private string GetPolicyDocument(string fileName, string objectKey)
{
var policyDocument = new {
expiration = DateTime.UtcNow.AddDays(2).ToString("yyyy-MM-ddTHH:mm:ssZ"), // The policy is valid for 2 days
conditions = new List<S3PolicyCondition> {
new S3PolicyCondition("acl", "public-read"),
new S3PolicyCondition("bucket", S3Settings.Bucket),
new S3PolicyCondition("Content-Type", Helpers.GetMimeType(fileName)),
new S3PolicyCondition("key", objectKey),
new S3PolicyCondition("x-amz-storage-class", GetObjectStorageClass())
}
};
return JsonConvert.SerializeObject(policyDocument);
}
private string GetEndpoint()
{
return string.Format("{0}{1}", S3Settings.Endpoint, S3Settings.Bucket);
return URLHelpers.CombineURL("https://" + GetCurrentRegion().GetEndpointForService("s3").Hostname, S3Settings.Bucket);
}
// http://codeonaboat.wordpress.com/2011/04/22/uploading-a-file-to-amazon-s3-using-an-asp-net-mvc-application-directly-from-the-users-browser/
private string CreateSignature(string secretKey, byte[] policyBytes)
private AWSCredentials GetCurrentCredentials()
{
var encoding = new ASCIIEncoding();
var base64Policy = Convert.ToBase64String(policyBytes);
var secretKeyBytes = encoding.GetBytes(secretKey);
byte[] signatureBytes;
using (var hmacsha1 = new HMACSHA256(secretKeyBytes))
{
var base64PolicyBytes = encoding.GetBytes(base64Policy);
signatureBytes = hmacsha1.ComputeHash(base64PolicyBytes);
}
return Convert.ToBase64String(signatureBytes);
return new BasicAWSCredentials(S3Settings.AccessKeyID, S3Settings.SecretAccessKey);
}
private string GetObjectKey(string fileName)
@ -107,10 +86,10 @@ private string GetObjectURL(string objectName)
objectName = objectName.Trim('/');
objectName = URLHelpers.URLPathEncode(objectName);
var url = string.Empty;
if (S3Settings.UseCustomCNAME)
{
string url;
if (!string.IsNullOrEmpty(S3Settings.CustomDomain))
{
url = URLHelpers.CombineURL(S3Settings.CustomDomain, objectName);
@ -119,15 +98,11 @@ private string GetObjectURL(string objectName)
{
url = URLHelpers.CombineURL(S3Settings.Bucket, objectName);
}
}
else
{
url = URLHelpers.CombineURL(GetEndpoint(), objectName);
return URLHelpers.FixPrefix(url);
}
url = URLHelpers.FixPrefix(url);
return url;
return URLHelpers.CombineURL(GetEndpoint(), objectName);
}
public string GetURL(string fileName)
@ -135,41 +110,57 @@ public string GetURL(string fileName)
return GetObjectURL(GetObjectKey(fileName));
}
private Dictionary<string, string> GetParameters(string fileName, string objectKey)
public string GetMd5Hash(Stream stream)
{
var policyDocument = GetPolicyDocument(fileName, objectKey);
var policyBytes = Encoding.ASCII.GetBytes(policyDocument);
var signature = CreateSignature(S3Settings.SecretAccessKey, policyBytes);
return new Dictionary<string, string> {
{ "key", objectKey },
{ "acl", "public-read" },
{ "content-type", Helpers.GetMimeType(fileName) },
{ "AWSAccessKeyId", S3Settings.AccessKeyID },
{ "policy", Convert.ToBase64String(policyBytes) },
{ "x-amz-algorithm", "AWS4-HMAC-SHA256" },
{ "signature", signature },
{ "x-amz-storage-class", GetObjectStorageClass() }
};
stream.Seek(0, SeekOrigin.Begin);
using (var md5 = MD5.Create())
{
return string.Concat(md5.ComputeHash(stream).Select(b => b.ToString("x2")));
}
}
public override UploadResult Upload(Stream stream, string fileName)
{
if (string.IsNullOrEmpty(S3Settings.AccessKeyID)) throw new Exception("'Access Key' must not be empty.");
if (string.IsNullOrEmpty(S3Settings.SecretAccessKey)) throw new Exception("'Secret Access Key' must not be empty.");
if (string.IsNullOrEmpty(S3Settings.Endpoint)) throw new Exception("'Endpoint' must not be empty.");
if (string.IsNullOrEmpty(S3Settings.Bucket)) throw new Exception("'Bucket' must not be empty.");
var objectKey = GetObjectKey(fileName);
var uploadResult = UploadData(stream, GetEndpoint(), fileName, arguments: GetParameters(fileName, objectKey), responseType: ResponseType.Headers);
if (uploadResult.IsSuccess)
using (var client = new AmazonS3Client(GetCurrentCredentials(), GetCurrentRegion()))
{
uploadResult.URL = GetObjectURL(objectKey);
}
var putRequest = new GetPreSignedUrlRequest
{
BucketName = S3Settings.Bucket,
Key = GetObjectKey(fileName),
Verb = HttpVerb.PUT,
Expires = DateTime.UtcNow.AddMinutes(5),
ContentType = Helpers.GetMimeType(fileName),
Protocol = Protocol.HTTPS,
};
return uploadResult;
var requestHeaders = new NameValueCollection();
requestHeaders["x-amz-acl"] = "public-read";
requestHeaders["x-amz-storage-class"] = GetObjectStorageClass();
putRequest.Headers["x-amz-acl"] = "public-read";
putRequest.Headers["x-amz-storage-class"] = GetObjectStorageClass();
var responseHeaders = SendRequestStreamGetHeaders(client.GetPreSignedURL(putRequest), stream, Helpers.GetMimeType(fileName), requestHeaders, method: HttpMethod.PUT);
var eTag = responseHeaders["ETag"].Replace("\"", "");
var uploadResult = new UploadResult();
if (GetMd5Hash(stream) == eTag)
{
uploadResult.IsSuccess = true;
uploadResult.URL = GetObjectURL(putRequest.Key);
}
else
{
uploadResult.Errors = new List<string> { "Uploaded file is different." };
}
return uploadResult;
}
}
}
@ -177,7 +168,7 @@ public class AmazonS3Settings
{
public string AccessKeyID { get; set; }
public string SecretAccessKey { get; set; }
public string Endpoint { get; set; }
public string Region { get; set; }
public string Bucket { get; set; }
public string ObjectPrefix { get; set; }
public bool UseCustomCNAME { get; set; }

View file

@ -1679,22 +1679,11 @@ private void InitializeComponent()
//
// cbAmazonS3Endpoint
//
this.cbAmazonS3Endpoint.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cbAmazonS3Endpoint.FormattingEnabled = true;
this.cbAmazonS3Endpoint.Items.AddRange(new object[] {
resources.GetString("cbAmazonS3Endpoint.Items"),
resources.GetString("cbAmazonS3Endpoint.Items1"),
resources.GetString("cbAmazonS3Endpoint.Items2"),
resources.GetString("cbAmazonS3Endpoint.Items3"),
resources.GetString("cbAmazonS3Endpoint.Items9"),
resources.GetString("cbAmazonS3Endpoint.Items4"),
resources.GetString("cbAmazonS3Endpoint.Items5"),
resources.GetString("cbAmazonS3Endpoint.Items6"),
resources.GetString("cbAmazonS3Endpoint.Items7"),
resources.GetString("cbAmazonS3Endpoint.Items8")});
resources.ApplyResources(this.cbAmazonS3Endpoint, "cbAmazonS3Endpoint");
this.cbAmazonS3Endpoint.Name = "cbAmazonS3Endpoint";
this.cbAmazonS3Endpoint.SelectionChangeCommitted += new System.EventHandler(this.cbAmazonS3Endpoint_SelectionChangeCommitted);
this.cbAmazonS3Endpoint.TextChanged += new System.EventHandler(this.cbAmazonS3Endpoint_TextChanged);
//
// lblAmazonS3BucketName
//

View file

@ -513,13 +513,16 @@ public void LoadSettings(UploadersConfig uploadersConfig)
txtAmazonS3AccessKey.Text = Config.AmazonS3Settings.AccessKeyID;
txtAmazonS3SecretKey.Text = Config.AmazonS3Settings.SecretAccessKey;
cbAmazonS3Endpoint.Text = Config.AmazonS3Settings.Endpoint;
txtAmazonS3BucketName.Text = Config.AmazonS3Settings.Bucket;
txtAmazonS3ObjectPrefix.Text = Config.AmazonS3Settings.ObjectPrefix;
cbAmazonS3CustomCNAME.Checked = Config.AmazonS3Settings.UseCustomCNAME;
txtAmazonS3CustomDomain.Enabled = Config.AmazonS3Settings.UseCustomCNAME;
txtAmazonS3CustomDomain.Text = Config.AmazonS3Settings.CustomDomain;
cbAmazonS3UseRRS.Checked = Config.AmazonS3Settings.UseReducedRedundancyStorage;
cbAmazonS3Endpoint.Items.AddRange(Amazon.RegionEndpoint.EnumerableAllRegions.ToArray());
cbAmazonS3Endpoint.SelectedItem = Amazon.RegionEndpoint.EnumerableAllRegions.SingleOrDefault(r => r.SystemName == Config.AmazonS3Settings.Region) ?? Amazon.RegionEndpoint.USWest1;
cbAmazonS3Endpoint.DisplayMember = "DisplayName";
UpdateAmazonS3Status();
// ownCloud
@ -1782,13 +1785,12 @@ private void txtAmazonS3SecretKey_TextChanged(object sender, EventArgs e)
private void cbAmazonS3Endpoint_SelectionChangeCommitted(object sender, EventArgs e)
{
Config.AmazonS3Settings.Endpoint = cbAmazonS3Endpoint.Text;
}
private void cbAmazonS3Endpoint_TextChanged(object sender, EventArgs e)
{
Config.AmazonS3Settings.Endpoint = cbAmazonS3Endpoint.Text;
UpdateAmazonS3Status();
var region = cbAmazonS3Endpoint.SelectedItem as Amazon.RegionEndpoint;
if (region != null)
{
Config.AmazonS3Settings.Region = region.SystemName;
UpdateAmazonS3Status();
}
}
private void txtAmazonS3BucketName_TextChanged(object sender, EventArgs e)

View file

@ -68,6 +68,9 @@
<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
</PropertyGroup>
<ItemGroup>
<Reference Include="AWSSDK">
<HintPath>..\packages\AWSSDK.2.3.32.0\lib\net35\AWSSDK.dll</HintPath>
</Reference>
<Reference Include="MegaApiClient, Version=1.0.4.0, Culture=neutral, PublicKeyToken=0480d311efbeb4e2, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\MegaApiClient.1.0.4\lib\MegaApiClient.dll</HintPath>

View file

@ -253,6 +253,15 @@ protected string SendRequestStream(string url, Stream stream, string contentType
}
}
protected NameValueCollection SendRequestStreamGetHeaders(string url, Stream stream, string contentType, NameValueCollection headers = null,
CookieCollection cookies = null, HttpMethod method = HttpMethod.POST)
{
using (HttpWebResponse response = GetResponse(url, stream, null, contentType, headers, cookies, method))
{
return response.Headers;
}
}
private HttpWebResponse SendRequestMultiPart(string url, Dictionary<string, string> arguments, NameValueCollection headers = null,
CookieCollection cookies = null, HttpMethod method = HttpMethod.POST)
{

View file

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="AWSSDK" version="2.3.32.0" targetFramework="net40" />
<package id="MegaApiClient" version="1.0.4" targetFramework="net40" />
<package id="Newtonsoft.Json" version="6.0.8" targetFramework="net40" />
<package id="SSH.NET" version="2014.4.6-beta1" targetFramework="net40" />

View file

@ -46,7 +46,6 @@ public partial class MainForm : HotkeyForm
private bool forceClose, firstUpdateCheck = true;
private UploadInfoManager uim;
private ToolStripDropDownItem tsmiImageFileUploaders, tsmiTrayImageFileUploaders, tsmiTextFileUploaders, tsmiTrayTextFileUploaders;
private System.Threading.Timer updateTimer;
private static readonly object updateTimerLock = new object();
public MainForm()