2015-06-08 01:34:33 +12:00
|
|
|
|
#region License Information (GPL v3)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
ShareX - A program that allows you to take screenshots and share any file type
|
2016-01-04 04:16:01 +13:00
|
|
|
|
Copyright (c) 2007-2016 ShareX Team
|
2015-06-08 01:34:33 +12: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)
|
|
|
|
|
|
|
|
|
|
// Credits: https://github.com/alanedwardes
|
|
|
|
|
|
|
|
|
|
using Amazon;
|
|
|
|
|
using Amazon.Runtime;
|
|
|
|
|
using Amazon.S3;
|
|
|
|
|
using Amazon.S3.Model;
|
|
|
|
|
using ShareX.HelpersLib;
|
2016-06-28 05:20:55 +12:00
|
|
|
|
using ShareX.UploadersLib.Properties;
|
2015-06-08 01:34:33 +12:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Collections.Specialized;
|
2016-06-28 05:20:55 +12:00
|
|
|
|
using System.Drawing;
|
2015-06-08 01:34:33 +12:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Security.Cryptography;
|
2016-05-01 20:09:11 +12:00
|
|
|
|
using System.Windows.Forms;
|
2015-06-08 01:34:33 +12:00
|
|
|
|
|
|
|
|
|
namespace ShareX.UploadersLib.FileUploaders
|
|
|
|
|
{
|
2016-03-23 06:24:41 +13:00
|
|
|
|
public class AmazonS3FileUploaderService : FileUploaderService
|
|
|
|
|
{
|
|
|
|
|
public override FileDestination EnumValue { get; } = FileDestination.AmazonS3;
|
|
|
|
|
|
2016-06-28 05:20:55 +12:00
|
|
|
|
public override Icon ServiceIcon => Resources.AmazonS3;
|
|
|
|
|
|
2016-03-24 07:47:59 +13:00
|
|
|
|
public override bool CheckConfig(UploadersConfig config)
|
2016-03-23 06:24:41 +13:00
|
|
|
|
{
|
2016-03-24 07:47:59 +13:00
|
|
|
|
return config.AmazonS3Settings != null && !string.IsNullOrEmpty(config.AmazonS3Settings.AccessKeyID) &&
|
|
|
|
|
!string.IsNullOrEmpty(config.AmazonS3Settings.SecretAccessKey) && !string.IsNullOrEmpty(config.AmazonS3Settings.Bucket) &&
|
|
|
|
|
AmazonS3.GetCurrentRegion(config.AmazonS3Settings) != AmazonS3.UnknownEndpoint;
|
2016-03-23 06:24:41 +13:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-04 21:53:11 +12:00
|
|
|
|
public override GenericUploader CreateUploader(UploadersConfig config, TaskReferenceHelper taskInfo)
|
2016-03-23 06:24:41 +13:00
|
|
|
|
{
|
2016-03-24 07:47:59 +13:00
|
|
|
|
return new AmazonS3(config.AmazonS3Settings);
|
2016-03-23 06:24:41 +13:00
|
|
|
|
}
|
2016-05-01 20:09:11 +12:00
|
|
|
|
|
|
|
|
|
public override TabPage GetUploadersConfigTabPage(UploadersConfigForm form) => form.tpAmazonS3;
|
2016-03-23 06:24:41 +13:00
|
|
|
|
}
|
|
|
|
|
|
2015-06-08 01:34:33 +12:00
|
|
|
|
public sealed class AmazonS3 : FileUploader
|
|
|
|
|
{
|
2015-09-17 20:17:21 +12:00
|
|
|
|
public static readonly AmazonS3Region UnknownEndpoint = new AmazonS3Region("Unknown Endpoint");
|
2016-09-13 08:49:01 +12:00
|
|
|
|
public static readonly AmazonS3Region DreamObjectsEndpoint = new AmazonS3Region("DreamObjects", "dreamobjects", "objects-us-west-1.dream.io");
|
2015-06-08 01:34:33 +12:00
|
|
|
|
|
|
|
|
|
private static IList<AmazonS3Region> regionEndpoints = new List<AmazonS3Region>();
|
|
|
|
|
|
|
|
|
|
public static IEnumerable<AmazonS3Region> RegionEndpoints
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (!regionEndpoints.Any())
|
|
|
|
|
{
|
|
|
|
|
regionEndpoints.Add(UnknownEndpoint);
|
|
|
|
|
RegionEndpoint.EnumerableAllRegions.Select(r => new AmazonS3Region(r)).ForEach(regionEndpoints.Add);
|
|
|
|
|
regionEndpoints.Add(DreamObjectsEndpoint);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return regionEndpoints;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-17 20:17:21 +12:00
|
|
|
|
private AmazonS3Settings s3Settings { get; set; }
|
|
|
|
|
|
2015-06-08 01:34:33 +12:00
|
|
|
|
public AmazonS3(AmazonS3Settings s3Settings)
|
|
|
|
|
{
|
|
|
|
|
this.s3Settings = s3Settings;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetObjectStorageClass()
|
|
|
|
|
{
|
|
|
|
|
return s3Settings.UseReducedRedundancyStorage ? "REDUCED_REDUNDANCY" : "STANDARD";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static AmazonS3Region GetCurrentRegion(AmazonS3Settings s3Settings)
|
|
|
|
|
{
|
|
|
|
|
return RegionEndpoints.SingleOrDefault(r => r.Identifier == s3Settings.Endpoint) ?? UnknownEndpoint;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetEndpoint()
|
|
|
|
|
{
|
2016-04-08 04:48:09 +12:00
|
|
|
|
return URLHelpers.ForcePrefix(URLHelpers.CombineURL(GetCurrentRegion(s3Settings).Hostname, s3Settings.Bucket));
|
2015-06-08 01:34:33 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private AWSCredentials GetCurrentCredentials()
|
|
|
|
|
{
|
|
|
|
|
return new BasicAWSCredentials(s3Settings.AccessKeyID, s3Settings.SecretAccessKey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetObjectKey(string fileName)
|
|
|
|
|
{
|
2016-09-17 19:07:02 +12:00
|
|
|
|
string objectPrefix = NameParser.Parse(NameParserType.FolderPath, s3Settings.ObjectPrefix.Trim('/'));
|
2015-06-08 01:34:33 +12:00
|
|
|
|
return URLHelpers.CombineURL(objectPrefix, fileName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetObjectURL(string objectName)
|
|
|
|
|
{
|
|
|
|
|
objectName = objectName.Trim('/');
|
|
|
|
|
objectName = URLHelpers.URLPathEncode(objectName);
|
|
|
|
|
|
|
|
|
|
if (s3Settings.UseCustomCNAME)
|
|
|
|
|
{
|
|
|
|
|
string url;
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(s3Settings.CustomDomain))
|
|
|
|
|
{
|
|
|
|
|
url = URLHelpers.CombineURL(s3Settings.CustomDomain, objectName);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
url = URLHelpers.CombineURL(s3Settings.Bucket, objectName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return URLHelpers.FixPrefix(url);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return URLHelpers.CombineURL(GetEndpoint(), objectName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetURL(string fileName)
|
|
|
|
|
{
|
|
|
|
|
return GetObjectURL(GetObjectKey(fileName));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetMd5Hash(Stream stream)
|
|
|
|
|
{
|
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
2016-09-17 19:07:02 +12:00
|
|
|
|
using (MD5 md5 = MD5.Create())
|
2015-06-08 01:34:33 +12:00
|
|
|
|
{
|
|
|
|
|
return string.Concat(md5.ComputeHash(stream).Select(b => b.ToString("x2")));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override UploadResult Upload(Stream stream, string fileName)
|
|
|
|
|
{
|
2016-04-04 21:13:10 +12:00
|
|
|
|
if (string.IsNullOrEmpty(s3Settings.AccessKeyID)) Errors.Add("'Access Key' must not be empty.");
|
|
|
|
|
if (string.IsNullOrEmpty(s3Settings.SecretAccessKey)) Errors.Add("'Secret Access Key' must not be empty.");
|
|
|
|
|
if (string.IsNullOrEmpty(s3Settings.Bucket)) Errors.Add("'Bucket' must not be empty.");
|
|
|
|
|
if (GetCurrentRegion(s3Settings) == UnknownEndpoint) Errors.Add("Please select an endpoint.");
|
2015-06-08 01:34:33 +12:00
|
|
|
|
|
2016-04-04 21:13:10 +12:00
|
|
|
|
if (IsError)
|
2015-06-08 01:34:33 +12:00
|
|
|
|
{
|
2016-04-04 21:13:10 +12:00
|
|
|
|
return null;
|
2015-06-08 01:34:33 +12:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-17 19:07:02 +12:00
|
|
|
|
AmazonS3Region region = GetCurrentRegion(s3Settings);
|
2015-06-08 01:34:33 +12:00
|
|
|
|
|
2016-09-17 19:07:02 +12:00
|
|
|
|
AmazonS3Config s3ClientConfig = new AmazonS3Config();
|
2015-06-08 01:34:33 +12:00
|
|
|
|
|
|
|
|
|
if (region.AmazonRegion == null)
|
|
|
|
|
{
|
2016-04-08 04:48:09 +12:00
|
|
|
|
s3ClientConfig.ServiceURL = URLHelpers.ForcePrefix(region.Hostname);
|
2015-06-08 01:34:33 +12:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
s3ClientConfig.RegionEndpoint = region.AmazonRegion;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-17 19:07:02 +12:00
|
|
|
|
using (AmazonS3Client client = new AmazonS3Client(GetCurrentCredentials(), s3ClientConfig))
|
2015-06-08 01:34:33 +12:00
|
|
|
|
{
|
2016-09-17 19:07:02 +12:00
|
|
|
|
GetPreSignedUrlRequest putRequest = new GetPreSignedUrlRequest
|
2015-06-08 01:34:33 +12:00
|
|
|
|
{
|
|
|
|
|
BucketName = s3Settings.Bucket,
|
|
|
|
|
Key = GetObjectKey(fileName),
|
|
|
|
|
Verb = HttpVerb.PUT,
|
|
|
|
|
Expires = DateTime.UtcNow.AddMinutes(5),
|
|
|
|
|
ContentType = Helpers.GetMimeType(fileName)
|
|
|
|
|
};
|
|
|
|
|
|
2016-09-17 19:07:02 +12:00
|
|
|
|
NameValueCollection requestHeaders = new NameValueCollection();
|
2015-06-08 01:34:33 +12:00
|
|
|
|
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();
|
|
|
|
|
|
2016-12-23 21:21:37 +13:00
|
|
|
|
NameValueCollection responseHeaders = SendRequestGetHeaders(HttpMethod.PUT, client.GetPreSignedURL(putRequest), stream, Helpers.GetMimeType(fileName), null, requestHeaders);
|
2016-12-23 21:51:13 +13:00
|
|
|
|
if (responseHeaders == null || responseHeaders.Count == 0)
|
2015-06-08 01:34:33 +12:00
|
|
|
|
{
|
2016-04-04 21:13:10 +12:00
|
|
|
|
Errors.Add("Upload to Amazon S3 failed. Check your access credentials.");
|
|
|
|
|
return null;
|
2015-06-08 01:34:33 +12:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-17 19:07:02 +12:00
|
|
|
|
string eTag = responseHeaders.Get("ETag");
|
2015-06-08 01:34:33 +12:00
|
|
|
|
if (eTag == null)
|
|
|
|
|
{
|
2016-04-04 21:13:10 +12:00
|
|
|
|
Errors.Add("Upload to Amazon S3 failed.");
|
|
|
|
|
return null;
|
2015-06-08 01:34:33 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (GetMd5Hash(stream) == eTag.Replace("\"", ""))
|
|
|
|
|
{
|
|
|
|
|
return new UploadResult { IsSuccess = true, URL = GetObjectURL(putRequest.Key) };
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-04 21:13:10 +12:00
|
|
|
|
Errors.Add("Upload to Amazon S3 failed, uploaded data did not match.");
|
|
|
|
|
return null;
|
2015-06-08 01:34:33 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-03-27 05:56:05 +13:00
|
|
|
|
}
|