2014-03-27 05:56:05 +13:00
|
|
|
|
#region License Information (GPL v3)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
ShareX - A program that allows you to take screenshots and share any file type
|
|
|
|
|
Copyright (C) 2008-2014 ShareX Developers
|
|
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
|
|
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#endregion License Information (GPL v3)
|
|
|
|
|
|
2014-03-30 22:16:20 +13:00
|
|
|
|
// Credits: https://github.com/alanedwardes
|
|
|
|
|
|
2014-03-27 05:56:05 +13:00
|
|
|
|
using HelpersLib;
|
|
|
|
|
using Newtonsoft.Json;
|
2014-03-29 01:55:41 +13:00
|
|
|
|
using System;
|
2014-03-27 05:56:05 +13:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Security.Cryptography;
|
2014-03-29 01:55:41 +13:00
|
|
|
|
using System.Text;
|
2014-04-03 12:45:12 +13:00
|
|
|
|
using System.Web;
|
2014-03-27 05:56:05 +13:00
|
|
|
|
|
|
|
|
|
namespace UploadersLib.FileUploaders
|
|
|
|
|
{
|
|
|
|
|
public sealed class AmazonS3 : FileUploader
|
|
|
|
|
{
|
2014-03-28 01:01:05 +13:00
|
|
|
|
private AmazonS3Settings S3Settings { get; set; }
|
2014-03-27 05:56:05 +13:00
|
|
|
|
|
|
|
|
|
public AmazonS3(AmazonS3Settings accessKeys)
|
|
|
|
|
{
|
2014-03-28 01:01:05 +13:00
|
|
|
|
S3Settings = accessKeys;
|
2014-03-27 05:56:05 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetObjectStorageClass()
|
|
|
|
|
{
|
2014-03-28 01:01:05 +13:00
|
|
|
|
return S3Settings.UseReducedRedundancyStorage ? "REDUCED_REDUNDANCY" : "STANDARD";
|
2014-03-27 05:56:05 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetObjectKey(string fileName)
|
|
|
|
|
{
|
2014-04-03 12:21:19 +13:00
|
|
|
|
var parser = new NameParser(NameParserType.FolderPath);
|
2014-03-28 01:01:05 +13:00
|
|
|
|
return string.Format("{0}{1}", parser.Parse(S3Settings.ObjectPrefix), fileName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Helper class to construct the S3 policy document (below)
|
|
|
|
|
private class S3PolicyCondition : Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
public S3PolicyCondition(string key, string value)
|
|
|
|
|
{
|
|
|
|
|
Add(key, value);
|
|
|
|
|
}
|
2014-03-27 05:56:05 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetPolicyDocument(string fileName)
|
|
|
|
|
{
|
2014-03-29 01:55:41 +13:00
|
|
|
|
var policyDocument = new
|
|
|
|
|
{
|
2014-03-28 01:01:05 +13:00
|
|
|
|
expiration = DateTime.UtcNow.AddDays(2).ToString("o"), // 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", GetObjectKey(fileName)),
|
|
|
|
|
new S3PolicyCondition("x-amz-storage-class", GetObjectStorageClass())
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return JsonConvert.SerializeObject(policyDocument);
|
2014-03-27 05:56:05 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetEndpoint()
|
|
|
|
|
{
|
2014-03-28 01:01:05 +13:00
|
|
|
|
return string.Format("{0}{1}", S3Settings.Endpoint, S3Settings.Bucket);
|
2014-03-27 05:56:05 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
{
|
|
|
|
|
var encoding = new ASCIIEncoding();
|
|
|
|
|
var base64Policy = Convert.ToBase64String(policyBytes);
|
|
|
|
|
var secretKeyBytes = encoding.GetBytes(secretKey);
|
2014-03-28 01:01:05 +13:00
|
|
|
|
|
|
|
|
|
byte[] signatureBytes;
|
|
|
|
|
using (var hmacsha1 = new HMACSHA1(secretKeyBytes))
|
|
|
|
|
{
|
|
|
|
|
var base64PolicyBytes = encoding.GetBytes(base64Policy);
|
|
|
|
|
signatureBytes = hmacsha1.ComputeHash(base64PolicyBytes);
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-27 05:56:05 +13:00
|
|
|
|
return Convert.ToBase64String(signatureBytes);
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-03 12:45:12 +13:00
|
|
|
|
private string GetObjectURL(string objectName)
|
|
|
|
|
{
|
|
|
|
|
var urlEncodedObjectName = Helpers.URLPathEncode(objectName);
|
|
|
|
|
if (S3Settings.UseCustomCNAME)
|
|
|
|
|
{
|
|
|
|
|
return string.Format("http://{0}/{1}", S3Settings.Bucket, urlEncodedObjectName);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return string.Format("{0}/{1}", GetEndpoint(), urlEncodedObjectName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-27 05:56:05 +13:00
|
|
|
|
private Dictionary<string, string> GetParameters(string fileName, string objectKey)
|
|
|
|
|
{
|
|
|
|
|
var policyDocument = GetPolicyDocument(fileName);
|
|
|
|
|
var policyBytes = Encoding.ASCII.GetBytes(policyDocument);
|
2014-03-28 01:01:05 +13:00
|
|
|
|
var signature = CreateSignature(S3Settings.SecretAccessKey, policyBytes);
|
2014-03-27 05:56:05 +13:00
|
|
|
|
|
|
|
|
|
var parameters = new Dictionary<string, string>();
|
|
|
|
|
parameters.Add("key", objectKey);
|
|
|
|
|
parameters.Add("acl", "public-read");
|
|
|
|
|
parameters.Add("content-type", Helpers.GetMimeType(fileName));
|
2014-03-28 01:01:05 +13:00
|
|
|
|
parameters.Add("AWSAccessKeyId", S3Settings.AccessKeyID);
|
2014-03-27 05:56:05 +13:00
|
|
|
|
parameters.Add("policy", Convert.ToBase64String(policyBytes));
|
|
|
|
|
parameters.Add("signature", signature);
|
|
|
|
|
parameters.Add("x-amz-storage-class", GetObjectStorageClass());
|
|
|
|
|
return parameters;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override UploadResult Upload(Stream stream, string fileName)
|
|
|
|
|
{
|
2014-03-28 01:01:05 +13:00
|
|
|
|
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 emoty.");
|
|
|
|
|
if (string.IsNullOrEmpty(S3Settings.Bucket)) throw new Exception("'Bucket' must not be empty.");
|
2014-03-27 05:56:05 +13:00
|
|
|
|
|
|
|
|
|
var objectKey = GetObjectKey(fileName);
|
|
|
|
|
|
2014-03-28 01:01:05 +13:00
|
|
|
|
var uploadResult = UploadData(stream, GetEndpoint(), fileName, arguments: GetParameters(fileName, objectKey), responseType: ResponseType.Headers);
|
2014-03-27 05:56:05 +13:00
|
|
|
|
|
|
|
|
|
if (uploadResult.IsSuccess)
|
|
|
|
|
{
|
2014-04-03 12:45:12 +13:00
|
|
|
|
uploadResult.URL = GetObjectURL(objectKey);
|
2014-03-27 05:56:05 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return uploadResult;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class AmazonS3Settings
|
|
|
|
|
{
|
|
|
|
|
public string AccessKeyID { get; set; }
|
|
|
|
|
public string SecretAccessKey { get; set; }
|
|
|
|
|
public bool UseReducedRedundancyStorage { get; set; }
|
|
|
|
|
public bool UseCustomCNAME { get; set; }
|
|
|
|
|
public string ObjectPrefix { get; set; }
|
|
|
|
|
public string Bucket { get; set; }
|
|
|
|
|
public string Endpoint { get; set; }
|
|
|
|
|
}
|
|
|
|
|
}
|