ShareX/ShareX.UploadersLib/FileUploaders/AmazonS3.cs

178 lines
6.2 KiB
C#
Raw Normal View History

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
2014-12-31 22:41:32 +13:00
Copyright © 2007-2015 ShareX Developers
2014-03-27 05:56:05 +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)
// Credits: https://github.com/alanedwardes
2014-12-11 12:19:28 +13:00
using ShareX.HelpersLib;
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;
using System.Linq;
using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
using Amazon.Runtime;
using System.Collections.Specialized;
2014-03-27 05:56:05 +13:00
2014-12-11 09:25:20 +13:00
namespace ShareX.UploadersLib.FileUploaders
2014-03-27 05:56:05 +13:00
{
public sealed class AmazonS3 : FileUploader
{
private AmazonS3Settings S3Settings { get; set; }
2014-03-27 05:56:05 +13:00
public AmazonS3(AmazonS3Settings s3Settings)
2014-03-27 05:56:05 +13:00
{
S3Settings = s3Settings;
2014-03-27 05:56:05 +13:00
}
private string GetObjectStorageClass()
{
return S3Settings.UseReducedRedundancyStorage ? "REDUCED_REDUNDANCY" : "STANDARD";
2014-03-27 05:56:05 +13:00
}
private RegionEndpoint GetCurrentRegion()
{
try
{
return RegionEndpoint.GetBySystemName(S3Settings.Region);
}
catch (ArgumentException)
{
return RegionEndpoint.USWest1;
}
2014-03-27 05:56:05 +13:00
}
private string GetEndpoint()
{
return URLHelpers.CombineURL("https://" + GetCurrentRegion().GetEndpointForService("s3").Hostname, S3Settings.Bucket);
2014-03-27 05:56:05 +13:00
}
private AWSCredentials GetCurrentCredentials()
2014-03-27 05:56:05 +13:00
{
return new BasicAWSCredentials(S3Settings.AccessKeyID, S3Settings.SecretAccessKey);
2014-03-27 05:56:05 +13:00
}
2014-04-16 07:30:46 +12:00
private string GetObjectKey(string fileName)
{
var objectPrefix = NameParser.Parse(NameParserType.FolderPath, S3Settings.ObjectPrefix.Trim('/'));
return URLHelpers.CombineURL(objectPrefix, fileName);
2014-04-16 07:30:46 +12:00
}
private string GetObjectURL(string objectName)
{
2014-04-16 07:30:46 +12:00
objectName = objectName.Trim('/');
objectName = URLHelpers.URLPathEncode(objectName);
2014-04-16 07:30:46 +12:00
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);
}
2014-05-06 11:24:00 +12:00
public string GetURL(string fileName)
{
return GetObjectURL(GetObjectKey(fileName));
}
public string GetMd5Hash(Stream stream)
2014-03-27 05:56:05 +13:00
{
stream.Seek(0, SeekOrigin.Begin);
using (var md5 = MD5.Create())
{
return string.Concat(md5.ComputeHash(stream).Select(b => b.ToString("x2")));
}
2014-03-27 05:56:05 +13:00
}
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.Bucket)) throw new Exception("'Bucket' must not be empty.");
2014-03-27 05:56:05 +13:00
using (var client = new AmazonS3Client(GetCurrentCredentials(), GetCurrentRegion()))
2014-03-27 05:56:05 +13:00
{
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,
};
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("\"", "");
2014-03-27 05:56:05 +13:00
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;
}
2014-03-27 05:56:05 +13:00
}
}
public class AmazonS3Settings
{
public string AccessKeyID { get; set; }
public string SecretAccessKey { get; set; }
public string Region { get; set; }
public string Bucket { get; set; }
public string ObjectPrefix { get; set; }
public bool UseCustomCNAME { get; set; }
public string CustomDomain { get; set; }
public bool UseReducedRedundancyStorage { get; set; }
2014-03-27 05:56:05 +13:00
}
}