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
|
2018-01-02 03:59:14 +13:00
|
|
|
|
Copyright (c) 2007-2018 ShareX Team
|
2017-03-11 06:10:16 +13:00
|
|
|
|
|
|
|
|
|
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;
|
2018-06-15 22:12:23 +12:00
|
|
|
|
using System.Net;
|
2017-03-11 06:10:16 +13:00
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
|
|
|
namespace ShareX.UploadersLib.FileUploaders
|
|
|
|
|
{
|
2017-10-01 18:05:53 +13:00
|
|
|
|
public enum AmazonS3StorageClass // Localized
|
2017-09-29 04:25:41 +13:00
|
|
|
|
{
|
|
|
|
|
STANDARD,
|
|
|
|
|
STANDARD_IA
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 06:10:16 +13:00
|
|
|
|
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-12-20 13:31:09 +13:00
|
|
|
|
// http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region
|
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 (Mumbai)", "s3.ap-south-1.amazonaws.com", "ap-south-1"),
|
2017-12-10 01:35:07 +13:00
|
|
|
|
new AmazonS3Endpoint("Asia Pacific (Seoul)", "s3.ap-northeast-2.amazonaws.com", "ap-northeast-2"),
|
2017-12-20 13:31:09 +13:00
|
|
|
|
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("Asia Pacific (Tokyo)", "s3.ap-northeast-1.amazonaws.com", "ap-northeast-1"),
|
2017-03-21 19:31:47 +13:00
|
|
|
|
new AmazonS3Endpoint("Canada (Central)", "s3.ca-central-1.amazonaws.com", "ca-central-1"),
|
2017-12-20 13:18:28 +13:00
|
|
|
|
new AmazonS3Endpoint("China (Beijing)", "s3.cn-north-1.amazonaws.com.cn", "cn-north-1"),
|
|
|
|
|
new AmazonS3Endpoint("China (Ningxia)", "s3.cn-northwest-1.amazonaws.com.cn", "cn-northwest-1"),
|
2017-12-10 01:35:07 +13:00
|
|
|
|
new AmazonS3Endpoint("EU (Frankfurt)", "s3.eu-central-1.amazonaws.com", "eu-central-1"),
|
2017-12-20 13:31:09 +13:00
|
|
|
|
new AmazonS3Endpoint("EU (Ireland)", "s3.eu-west-1.amazonaws.com", "eu-west-1"),
|
2017-12-10 01:35:07 +13:00
|
|
|
|
new AmazonS3Endpoint("EU (London)", "s3.eu-west-2.amazonaws.com", "eu-west-2"),
|
2017-12-20 13:18:28 +13:00
|
|
|
|
new AmazonS3Endpoint("EU (Paris)", "s3.eu-west-3.amazonaws.com", "eu-west-3"),
|
2017-12-20 13:31:09 +13:00
|
|
|
|
new AmazonS3Endpoint("South America (São Paulo)", "s3.sa-east-1.amazonaws.com", "sa-east-1"),
|
2017-12-10 01:35:07 +13:00
|
|
|
|
new AmazonS3Endpoint("US East (N. Virginia)", "s3.amazonaws.com", "us-east-1"),
|
2017-03-21 19:31:47 +13:00
|
|
|
|
new AmazonS3Endpoint("US East (Ohio)", "s3.us-east-2.amazonaws.com", "us-east-2"),
|
2017-12-20 13:31:09 +13:00
|
|
|
|
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"),
|
2018-10-29 18:20:59 +13:00
|
|
|
|
new AmazonS3Endpoint("DreamObjects", "objects-us-east-1.dream.io"),
|
2017-12-23 23:44:56 +13:00
|
|
|
|
new AmazonS3Endpoint("DigitalOcean (Amsterdam)", "ams3.digitaloceanspaces.com", "ams3"),
|
2018-01-29 10:14:02 +13:00
|
|
|
|
new AmazonS3Endpoint("DigitalOcean (New York)", "nyc3.digitaloceanspaces.com", "nyc3"),
|
2018-11-10 16:05:35 +13:00
|
|
|
|
new AmazonS3Endpoint("DigitalOcean (San Francisco)", "sfo2.digitaloceanspaces.com", "sfo2"),
|
2018-06-05 06:38:41 +12:00
|
|
|
|
new AmazonS3Endpoint("DigitalOcean (Singapore)", "sgp1.digitaloceanspaces.com", "sgp1"),
|
|
|
|
|
new AmazonS3Endpoint("Wasabi", "s3.wasabisys.com")
|
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-12-23 22:10:41 +13:00
|
|
|
|
bool isPathStyleRequest = Settings.UsePathStyle;
|
2017-04-24 01:37:52 +12:00
|
|
|
|
|
2017-12-23 22:10:41 +13:00
|
|
|
|
if (!isPathStyleRequest && Settings.Bucket.Contains("."))
|
2017-04-24 01:37:52 +12:00
|
|
|
|
{
|
2017-12-23 22:10:41 +13:00
|
|
|
|
isPathStyleRequest = true;
|
2017-04-24 01:37:52 +12:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-21 00:26:44 +13:00
|
|
|
|
string endpoint = URLHelpers.RemovePrefixes(Settings.Endpoint);
|
2017-12-23 22:10:41 +13:00
|
|
|
|
string host = isPathStyleRequest ? 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-12-23 22:10:41 +13:00
|
|
|
|
string timeStamp = DateTime.UtcNow.ToString("yyyyMMddTHHmmssZ", CultureInfo.InvariantCulture);
|
2018-10-18 05:06:06 +13:00
|
|
|
|
string contentType = UploadHelpers.GetMimeType(fileName);
|
2017-12-23 22:10:41 +13:00
|
|
|
|
string hashedPayload = "UNSIGNED-PAYLOAD";
|
2017-03-11 06:10:16 +13:00
|
|
|
|
|
2018-02-02 08:55:23 +13:00
|
|
|
|
if ((Settings.RemoveExtensionImage && Helpers.IsImageFile(fileName)) ||
|
|
|
|
|
(Settings.RemoveExtensionText && Helpers.IsTextFile(fileName)) ||
|
|
|
|
|
(Settings.RemoveExtensionVideo && Helpers.IsVideoFile(fileName)))
|
|
|
|
|
{
|
|
|
|
|
fileName = Path.GetFileNameWithoutExtension(fileName);
|
|
|
|
|
}
|
|
|
|
|
string uploadPath = GetUploadPath(fileName);
|
|
|
|
|
|
|
|
|
|
NameValueCollection headers = new NameValueCollection
|
|
|
|
|
{
|
|
|
|
|
["Host"] = host,
|
|
|
|
|
["Content-Length"] = stream.Length.ToString(),
|
|
|
|
|
["Content-Type"] = contentType,
|
|
|
|
|
["x-amz-date"] = timeStamp,
|
|
|
|
|
["x-amz-content-sha256"] = hashedPayload,
|
|
|
|
|
["x-amz-storage-class"] = Settings.StorageClass.ToString()
|
|
|
|
|
};
|
2017-12-23 22:10:41 +13:00
|
|
|
|
if (Settings.SetPublicACL) headers["x-amz-acl"] = "public-read";
|
2017-04-25 00:17:33 +12:00
|
|
|
|
|
|
|
|
|
string canonicalURI = uploadPath;
|
2017-12-23 22:10:41 +13:00
|
|
|
|
if (isPathStyleRequest) canonicalURI = URLHelpers.CombineURL(Settings.Bucket, canonicalURI);
|
2017-04-25 00:17:33 +12:00
|
|
|
|
canonicalURI = URLHelpers.AddSlash(canonicalURI, SlashType.Prefix);
|
2018-06-22 04:25:11 +12:00
|
|
|
|
canonicalURI = URLHelpers.URLEncode(canonicalURI, true);
|
2017-12-23 22:10:41 +13:00
|
|
|
|
string canonicalQueryString = "";
|
2017-03-11 06:10:16 +13:00
|
|
|
|
string canonicalHeaders = CreateCanonicalHeaders(headers);
|
2017-12-23 22:10:41 +13:00
|
|
|
|
string signedHeaders = GetSignedHeaders(headers);
|
2017-03-11 06:10:16 +13:00
|
|
|
|
|
|
|
|
|
string canonicalRequest = "PUT" + "\n" +
|
|
|
|
|
canonicalURI + "\n" +
|
|
|
|
|
canonicalQueryString + "\n" +
|
|
|
|
|
canonicalHeaders + "\n" +
|
|
|
|
|
signedHeaders + "\n" +
|
2017-12-23 22:10:41 +13:00
|
|
|
|
hashedPayload;
|
2017-03-11 06:10:16 +13:00
|
|
|
|
|
|
|
|
|
string stringToSign = algorithm + "\n" +
|
2017-12-23 22:10:41 +13:00
|
|
|
|
timeStamp + "\n" +
|
2017-03-11 06:10:16 +13:00
|
|
|
|
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);
|
2017-12-23 22:10:41 +13:00
|
|
|
|
|
2017-03-20 12:53:32 +13:00
|
|
|
|
string signature = Helpers.BytesToHex(Helpers.ComputeHMACSHA256(stringToSign, signingKey));
|
2017-03-11 06:10:16 +13:00
|
|
|
|
|
2017-12-23 22:10:41 +13:00
|
|
|
|
headers["Authorization"] = algorithm + " " +
|
|
|
|
|
"Credential=" + credential + "," +
|
|
|
|
|
"SignedHeaders=" + signedHeaders + "," +
|
|
|
|
|
"Signature=" + signature;
|
2017-03-11 06:10:16 +13:00
|
|
|
|
|
2018-01-30 06:45:31 +13:00
|
|
|
|
headers.Remove("Host");
|
|
|
|
|
headers.Remove("Content-Type");
|
2017-03-11 06:10:16 +13:00
|
|
|
|
|
2017-03-20 12:53:32 +13:00
|
|
|
|
string url = URLHelpers.CombineURL(host, canonicalURI);
|
2017-03-11 09:48:21 +13:00
|
|
|
|
url = URLHelpers.ForcePrefix(url, "https://");
|
2017-03-11 06:10:16 +13:00
|
|
|
|
|
2018-06-15 22:12:23 +12:00
|
|
|
|
using (HttpWebResponse response = GetResponse(HttpMethod.PUT, url, stream, contentType, null, headers))
|
2017-03-11 06:10:16 +13:00
|
|
|
|
{
|
2018-06-15 22:12:23 +12:00
|
|
|
|
if (response != null)
|
|
|
|
|
{
|
|
|
|
|
NameValueCollection responseHeaders = response.Headers;
|
|
|
|
|
|
|
|
|
|
if (responseHeaders != null && responseHeaders["ETag"] != null)
|
|
|
|
|
{
|
|
|
|
|
return new UploadResult
|
|
|
|
|
{
|
|
|
|
|
IsSuccess = true,
|
|
|
|
|
URL = GenerateURL(uploadPath)
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-11 06:10:16 +13:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-15 22:12:23 +12:00
|
|
|
|
Errors.Add("Upload to Amazon S3 failed.");
|
|
|
|
|
return null;
|
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
|
|
|
|
{
|
2018-06-22 04:25:11 +12:00
|
|
|
|
uploadPath = URLHelpers.URLEncode(uploadPath, true);
|
2017-04-29 17:09:07 +12:00
|
|
|
|
|
2017-03-18 13:43:38 +13:00
|
|
|
|
string url;
|
|
|
|
|
|
|
|
|
|
if (Settings.UseCustomCNAME && !string.IsNullOrEmpty(Settings.CustomDomain))
|
|
|
|
|
{
|
2018-10-08 19:10:59 +13:00
|
|
|
|
CustomUploaderParser parser = new CustomUploaderParser();
|
|
|
|
|
string parsedDomain = parser.Parse(Settings.CustomDomain);
|
2018-01-02 08:13:27 +13:00
|
|
|
|
url = URLHelpers.CombineURL(parsedDomain, uploadPath);
|
2017-03-18 13:43:38 +13:00
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
{
|
2017-12-23 22:10:41 +13:00
|
|
|
|
return headers.AllKeys.OrderBy(key => key).Select(key => key.ToLowerInvariant() + ":" + headers[key].Trim() + "\n").
|
|
|
|
|
Aggregate((result, next) => result + next);
|
2017-03-11 06:10:16 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetSignedHeaders(NameValueCollection headers)
|
|
|
|
|
{
|
2017-12-23 22:10:41 +13:00
|
|
|
|
return string.Join(";", headers.AllKeys.OrderBy(key => key).Select(key => key.ToLowerInvariant()));
|
2017-03-11 06:10:16 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-10 16:05:35 +13:00
|
|
|
|
}
|