2017-03-11 06:10:16 +13:00
|
|
|
|
#region License Information (GPL v3)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
ShareX - A program that allows you to take screenshots and share any file type
|
|
|
|
|
Copyright (c) 2007-2017 ShareX Team
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
using ShareX.HelpersLib;
|
|
|
|
|
using ShareX.UploadersLib.Properties;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Collections.Specialized;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
|
|
|
namespace ShareX.UploadersLib.FileUploaders
|
|
|
|
|
{
|
|
|
|
|
public class AmazonS3NewFileUploaderService : FileUploaderService
|
|
|
|
|
{
|
|
|
|
|
public override FileDestination EnumValue { get; } = FileDestination.AmazonS3;
|
|
|
|
|
|
|
|
|
|
public override Icon ServiceIcon => Resources.AmazonS3;
|
|
|
|
|
|
|
|
|
|
public override bool CheckConfig(UploadersConfig config)
|
|
|
|
|
{
|
|
|
|
|
return config.AmazonS3Settings != null && !string.IsNullOrEmpty(config.AmazonS3Settings.AccessKeyID) &&
|
2017-03-21 00:26:44 +13:00
|
|
|
|
!string.IsNullOrEmpty(config.AmazonS3Settings.SecretAccessKey) && !string.IsNullOrEmpty(config.AmazonS3Settings.Endpoint) &&
|
2017-03-20 12:05:23 +13:00
|
|
|
|
!string.IsNullOrEmpty(config.AmazonS3Settings.Bucket);
|
2017-03-11 06:10:16 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override GenericUploader CreateUploader(UploadersConfig config, TaskReferenceHelper taskInfo)
|
|
|
|
|
{
|
|
|
|
|
return new AmazonS3(config.AmazonS3Settings);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override TabPage GetUploadersConfigTabPage(UploadersConfigForm form) => form.tpAmazonS3;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public sealed class AmazonS3 : FileUploader
|
|
|
|
|
{
|
2017-03-20 20:38:47 +13:00
|
|
|
|
private const string DefaultRegion = "us-east-1";
|
|
|
|
|
|
2017-03-21 19:31:47 +13:00
|
|
|
|
public static List<AmazonS3Endpoint> Endpoints { get; } = new List<AmazonS3Endpoint>()
|
2017-03-17 13:15:19 +13:00
|
|
|
|
{
|
2017-03-21 19:31:47 +13:00
|
|
|
|
new AmazonS3Endpoint("Asia Pacific (Tokyo)", "s3-ap-northeast-1.amazonaws.com", "ap-northeast-1"),
|
|
|
|
|
new AmazonS3Endpoint("Asia Pacific (Seoul)", "s3.ap-northeast-2.amazonaws.com", "ap-northeast-2"),
|
|
|
|
|
new AmazonS3Endpoint("Asia Pacific (Mumbai)", "s3.ap-south-1.amazonaws.com", "ap-south-1"),
|
|
|
|
|
new AmazonS3Endpoint("Asia Pacific (Singapore)", "s3-ap-southeast-1.amazonaws.com", "ap-southeast-1"),
|
|
|
|
|
new AmazonS3Endpoint("Asia Pacific (Sydney)", "s3-ap-southeast-2.amazonaws.com", "ap-southeast-2"),
|
|
|
|
|
new AmazonS3Endpoint("Canada (Central)", "s3.ca-central-1.amazonaws.com", "ca-central-1"),
|
|
|
|
|
new AmazonS3Endpoint("EU Central (Frankfurt)", "s3.eu-central-1.amazonaws.com", "eu-central-1"),
|
|
|
|
|
new AmazonS3Endpoint("EU West (Ireland)", "s3-eu-west-1.amazonaws.com", "eu-west-1"),
|
|
|
|
|
new AmazonS3Endpoint("EU West (London)", "s3.eu-west-2.amazonaws.com", "eu-west-2"),
|
|
|
|
|
new AmazonS3Endpoint("South America (Sao Paulo)", "s3-sa-east-1.amazonaws.com", "sa-east-1"),
|
|
|
|
|
new AmazonS3Endpoint("US East (Virginia)", "s3.amazonaws.com", "us-east-1"),
|
|
|
|
|
new AmazonS3Endpoint("US East (Ohio)", "s3.us-east-2.amazonaws.com", "us-east-2"),
|
|
|
|
|
new AmazonS3Endpoint("US West (N. California)", "s3-us-west-1.amazonaws.com", "us-west-1"),
|
|
|
|
|
new AmazonS3Endpoint("US West (Oregon)", "s3-us-west-2.amazonaws.com", "us-west-2"),
|
|
|
|
|
new AmazonS3Endpoint("China (Beijing)", "s3.cn-north-1.amazonaws.com.cn", "cn-north-1"),
|
|
|
|
|
new AmazonS3Endpoint("US GovCloud West (Oregon)", "s3-us-gov-west-1.amazonaws.com", "us-gov-west-1"),
|
|
|
|
|
new AmazonS3Endpoint("DreamObjects", "objects-us-west-1.dream.io")
|
2017-03-17 13:15:19 +13:00
|
|
|
|
};
|
|
|
|
|
|
2017-03-11 06:10:16 +13:00
|
|
|
|
private AmazonS3Settings Settings { get; set; }
|
|
|
|
|
|
|
|
|
|
public AmazonS3(AmazonS3Settings settings)
|
|
|
|
|
{
|
|
|
|
|
Settings = settings;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override UploadResult Upload(Stream stream, string fileName)
|
|
|
|
|
{
|
2017-04-24 01:37:52 +12:00
|
|
|
|
bool forcePathStyle = Settings.UsePathStyle;
|
|
|
|
|
|
|
|
|
|
if (!forcePathStyle && Settings.Bucket.Contains("."))
|
|
|
|
|
{
|
|
|
|
|
forcePathStyle = true;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-21 00:26:44 +13:00
|
|
|
|
string endpoint = URLHelpers.RemovePrefixes(Settings.Endpoint);
|
2017-04-24 01:37:52 +12:00
|
|
|
|
string host = forcePathStyle ? endpoint : $"{Settings.Bucket}.{endpoint}";
|
2017-03-11 06:10:16 +13:00
|
|
|
|
string algorithm = "AWS4-HMAC-SHA256";
|
|
|
|
|
string credentialDate = DateTime.UtcNow.ToString("yyyyMMdd", CultureInfo.InvariantCulture);
|
2017-03-21 00:26:44 +13:00
|
|
|
|
string region = GetRegion();
|
|
|
|
|
string scope = URLHelpers.CombineURL(credentialDate, region, "s3", "aws4_request");
|
|
|
|
|
string credential = URLHelpers.CombineURL(Settings.AccessKeyID, scope);
|
2017-03-11 06:10:16 +13:00
|
|
|
|
string longDate = DateTime.UtcNow.ToString("yyyyMMddTHHmmssZ", CultureInfo.InvariantCulture);
|
|
|
|
|
string expiresTotalSeconds = ((long)TimeSpan.FromHours(1).TotalSeconds).ToString();
|
|
|
|
|
string contentType = Helpers.GetMimeType(fileName);
|
|
|
|
|
|
|
|
|
|
NameValueCollection headers = new NameValueCollection();
|
|
|
|
|
headers["content-type"] = contentType;
|
|
|
|
|
headers["host"] = host;
|
|
|
|
|
headers["x-amz-acl"] = "public-read";
|
|
|
|
|
headers["x-amz-storage-class"] = Settings.UseReducedRedundancyStorage ? "REDUCED_REDUNDANCY" : "STANDARD";
|
|
|
|
|
|
2017-03-11 09:48:21 +13:00
|
|
|
|
string signedHeaders = GetSignedHeaders(headers);
|
|
|
|
|
|
|
|
|
|
Dictionary<string, string> args = new Dictionary<string, string>();
|
|
|
|
|
args.Add("X-Amz-Algorithm", algorithm);
|
|
|
|
|
args.Add("X-Amz-Credential", credential);
|
|
|
|
|
args.Add("X-Amz-Date", longDate);
|
|
|
|
|
args.Add("X-Amz-Expires", expiresTotalSeconds);
|
|
|
|
|
args.Add("X-Amz-SignedHeaders", signedHeaders);
|
|
|
|
|
|
2017-03-11 06:10:16 +13:00
|
|
|
|
string uploadPath = GetUploadPath(fileName);
|
2017-04-25 00:17:33 +12:00
|
|
|
|
|
|
|
|
|
string canonicalURI = uploadPath;
|
|
|
|
|
if (forcePathStyle) canonicalURI = URLHelpers.CombineURL(Settings.Bucket, canonicalURI);
|
|
|
|
|
canonicalURI = URLHelpers.AddSlash(canonicalURI, SlashType.Prefix);
|
2017-03-11 06:10:16 +13:00
|
|
|
|
canonicalURI = URLHelpers.URLPathEncode(canonicalURI);
|
|
|
|
|
|
2017-03-20 12:53:32 +13:00
|
|
|
|
string canonicalQueryString = URLHelpers.CreateQuery(args);
|
2017-03-11 06:10:16 +13:00
|
|
|
|
string canonicalHeaders = CreateCanonicalHeaders(headers);
|
|
|
|
|
|
|
|
|
|
string canonicalRequest = "PUT" + "\n" +
|
|
|
|
|
canonicalURI + "\n" +
|
|
|
|
|
canonicalQueryString + "\n" +
|
|
|
|
|
canonicalHeaders + "\n" +
|
|
|
|
|
signedHeaders + "\n" +
|
|
|
|
|
"UNSIGNED-PAYLOAD";
|
|
|
|
|
|
|
|
|
|
string stringToSign = algorithm + "\n" +
|
|
|
|
|
longDate + "\n" +
|
|
|
|
|
scope + "\n" +
|
2017-03-20 12:53:32 +13:00
|
|
|
|
Helpers.BytesToHex(Helpers.ComputeSHA256(canonicalRequest));
|
2017-03-11 06:10:16 +13:00
|
|
|
|
|
2017-03-20 12:53:32 +13:00
|
|
|
|
byte[] dateKey = Helpers.ComputeHMACSHA256(credentialDate, "AWS4" + Settings.SecretAccessKey);
|
2017-03-21 00:26:44 +13:00
|
|
|
|
byte[] dateRegionKey = Helpers.ComputeHMACSHA256(region, dateKey);
|
2017-03-20 12:53:32 +13:00
|
|
|
|
byte[] dateRegionServiceKey = Helpers.ComputeHMACSHA256("s3", dateRegionKey);
|
|
|
|
|
byte[] signingKey = Helpers.ComputeHMACSHA256("aws4_request", dateRegionServiceKey);
|
|
|
|
|
string signature = Helpers.BytesToHex(Helpers.ComputeHMACSHA256(stringToSign, signingKey));
|
2017-03-11 06:10:16 +13:00
|
|
|
|
|
|
|
|
|
args.Add("X-Amz-Signature", signature);
|
|
|
|
|
|
|
|
|
|
headers.Remove("content-type");
|
|
|
|
|
headers.Remove("host");
|
|
|
|
|
|
2017-03-20 12:53:32 +13:00
|
|
|
|
string url = URLHelpers.CombineURL(host, canonicalURI);
|
|
|
|
|
url = URLHelpers.CreateQuery(url, args);
|
2017-03-11 09:48:21 +13:00
|
|
|
|
url = URLHelpers.ForcePrefix(url, "https://");
|
2017-03-11 06:10:16 +13:00
|
|
|
|
|
|
|
|
|
NameValueCollection responseHeaders = SendRequestGetHeaders(HttpMethod.PUT, url, stream, contentType, null, headers);
|
|
|
|
|
|
|
|
|
|
if (responseHeaders == null || responseHeaders.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
Errors.Add("Upload to Amazon S3 failed. Check your access credentials.");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 09:48:21 +13:00
|
|
|
|
if (responseHeaders["ETag"] == null)
|
2017-03-11 06:10:16 +13:00
|
|
|
|
{
|
|
|
|
|
Errors.Add("Upload to Amazon S3 failed.");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new UploadResult
|
|
|
|
|
{
|
|
|
|
|
IsSuccess = true,
|
2017-04-25 00:17:33 +12:00
|
|
|
|
URL = GenerateURL(uploadPath)
|
2017-03-11 06:10:16 +13:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-21 00:26:44 +13:00
|
|
|
|
private string GetRegion()
|
2017-03-20 12:05:23 +13:00
|
|
|
|
{
|
2017-03-21 00:26:44 +13:00
|
|
|
|
if (!string.IsNullOrEmpty(Settings.Region))
|
2017-03-20 12:05:23 +13:00
|
|
|
|
{
|
2017-03-21 00:26:44 +13:00
|
|
|
|
return Settings.Region;
|
2017-03-20 12:05:23 +13:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-21 00:26:44 +13:00
|
|
|
|
string url = Settings.Endpoint;
|
2017-03-20 20:38:47 +13:00
|
|
|
|
|
|
|
|
|
int delimIndex = url.IndexOf("//", StringComparison.Ordinal);
|
|
|
|
|
if (delimIndex >= 0)
|
|
|
|
|
{
|
|
|
|
|
url = url.Substring(delimIndex + 2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (url.EndsWith("/", StringComparison.Ordinal))
|
|
|
|
|
{
|
|
|
|
|
url = url.Substring(0, url.Length - 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int awsIndex = url.IndexOf(".amazonaws.com", StringComparison.Ordinal);
|
|
|
|
|
if (awsIndex < 0)
|
|
|
|
|
{
|
|
|
|
|
return DefaultRegion;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string serviceAndRegion = url.Substring(0, awsIndex);
|
|
|
|
|
if (serviceAndRegion.StartsWith("s3-", StringComparison.Ordinal))
|
|
|
|
|
{
|
|
|
|
|
serviceAndRegion = "s3." + serviceAndRegion.Substring(3);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int separatorIndex = serviceAndRegion.LastIndexOf('.');
|
|
|
|
|
if (separatorIndex == -1)
|
|
|
|
|
{
|
|
|
|
|
return DefaultRegion;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return serviceAndRegion.Substring(separatorIndex + 1);
|
2017-03-20 12:05:23 +13:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-25 02:01:35 +12:00
|
|
|
|
private string GetUploadPath(string fileName)
|
2017-03-11 06:10:16 +13:00
|
|
|
|
{
|
|
|
|
|
string path = NameParser.Parse(NameParserType.FolderPath, Settings.ObjectPrefix.Trim('/'));
|
|
|
|
|
return URLHelpers.CombineURL(path, fileName);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-25 00:17:33 +12:00
|
|
|
|
public string GenerateURL(string uploadPath)
|
2017-03-11 09:48:21 +13:00
|
|
|
|
{
|
2017-03-21 00:26:44 +13:00
|
|
|
|
if (!string.IsNullOrEmpty(Settings.Endpoint) && !string.IsNullOrEmpty(Settings.Bucket))
|
2017-03-17 13:21:45 +13:00
|
|
|
|
{
|
2017-04-29 17:09:07 +12:00
|
|
|
|
uploadPath = URLHelpers.URLPathEncode(uploadPath);
|
|
|
|
|
|
2017-03-18 13:43:38 +13:00
|
|
|
|
string url;
|
|
|
|
|
|
|
|
|
|
if (Settings.UseCustomCNAME && !string.IsNullOrEmpty(Settings.CustomDomain))
|
|
|
|
|
{
|
|
|
|
|
url = URLHelpers.CombineURL(Settings.CustomDomain, uploadPath);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-03-21 00:26:44 +13:00
|
|
|
|
url = URLHelpers.CombineURL(Settings.Endpoint, Settings.Bucket, uploadPath);
|
2017-03-18 13:43:38 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return URLHelpers.FixPrefix(url, "https://");
|
2017-03-17 13:21:45 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "";
|
2017-03-11 09:48:21 +13:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-25 02:01:35 +12:00
|
|
|
|
public string GetPreviewURL()
|
|
|
|
|
{
|
|
|
|
|
string uploadPath = GetUploadPath("example.png");
|
|
|
|
|
return GenerateURL(uploadPath);
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 06:10:16 +13:00
|
|
|
|
private string CreateCanonicalHeaders(NameValueCollection headers)
|
|
|
|
|
{
|
|
|
|
|
string result = "";
|
|
|
|
|
|
|
|
|
|
foreach (string key in headers)
|
|
|
|
|
{
|
2017-03-11 09:48:21 +13:00
|
|
|
|
result += key.ToLowerInvariant() + ":" + headers[key].Trim() + "\n";
|
2017-03-11 06:10:16 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetSignedHeaders(NameValueCollection headers)
|
|
|
|
|
{
|
|
|
|
|
return string.Join(";", headers.AllKeys.Select(x => x.ToLowerInvariant()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|