mirror of
https://github.com/ShareX/ShareX.git
synced 2024-10-02 18:26:27 +13:00
Minor refactoring - now using AmazonS3 class to turn settings key for region into the actual S3 region.
This commit is contained in:
parent
ae89ebd8bc
commit
d0ee29460b
2 changed files with 26 additions and 32 deletions
|
@ -41,43 +41,36 @@ namespace ShareX.UploadersLib.FileUploaders
|
|||
{
|
||||
public sealed class AmazonS3 : FileUploader
|
||||
{
|
||||
private AmazonS3Settings S3Settings { get; set; }
|
||||
private AmazonS3Settings s3Settings { get; set; }
|
||||
|
||||
public AmazonS3(AmazonS3Settings s3Settings)
|
||||
{
|
||||
S3Settings = s3Settings;
|
||||
this.s3Settings = s3Settings;
|
||||
}
|
||||
|
||||
private string GetObjectStorageClass()
|
||||
{
|
||||
return S3Settings.UseReducedRedundancyStorage ? "REDUCED_REDUNDANCY" : "STANDARD";
|
||||
}
|
||||
|
||||
private RegionEndpoint GetCurrentRegion()
|
||||
return s3Settings.UseReducedRedundancyStorage ? "REDUCED_REDUNDANCY" : "STANDARD";
|
||||
}
|
||||
|
||||
public static RegionEndpoint GetCurrentRegion(AmazonS3Settings s3Settings)
|
||||
{
|
||||
try
|
||||
{
|
||||
return RegionEndpoint.GetBySystemName(S3Settings.Region);
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
return RegionEndpoint.USWest1;
|
||||
}
|
||||
return RegionEndpoint.GetBySystemName(s3Settings.Region);
|
||||
}
|
||||
|
||||
private string GetEndpoint()
|
||||
{
|
||||
return URLHelpers.CombineURL("https://" + GetCurrentRegion().GetEndpointForService("s3").Hostname, S3Settings.Bucket);
|
||||
{
|
||||
return URLHelpers.CombineURL("https://" + GetCurrentRegion(s3Settings).GetEndpointForService("s3").Hostname, s3Settings.Bucket);
|
||||
}
|
||||
|
||||
private AWSCredentials GetCurrentCredentials()
|
||||
{
|
||||
return new BasicAWSCredentials(S3Settings.AccessKeyID, S3Settings.SecretAccessKey);
|
||||
return new BasicAWSCredentials(s3Settings.AccessKeyID, s3Settings.SecretAccessKey);
|
||||
}
|
||||
|
||||
private string GetObjectKey(string fileName)
|
||||
{
|
||||
var objectPrefix = NameParser.Parse(NameParserType.FolderPath, S3Settings.ObjectPrefix.Trim('/'));
|
||||
var objectPrefix = NameParser.Parse(NameParserType.FolderPath, s3Settings.ObjectPrefix.Trim('/'));
|
||||
return URLHelpers.CombineURL(objectPrefix, fileName);
|
||||
}
|
||||
|
||||
|
@ -86,17 +79,17 @@ private string GetObjectURL(string objectName)
|
|||
objectName = objectName.Trim('/');
|
||||
objectName = URLHelpers.URLPathEncode(objectName);
|
||||
|
||||
if (S3Settings.UseCustomCNAME)
|
||||
if (s3Settings.UseCustomCNAME)
|
||||
{
|
||||
string url;
|
||||
|
||||
if (!string.IsNullOrEmpty(S3Settings.CustomDomain))
|
||||
if (!string.IsNullOrEmpty(s3Settings.CustomDomain))
|
||||
{
|
||||
url = URLHelpers.CombineURL(S3Settings.CustomDomain, objectName);
|
||||
url = URLHelpers.CombineURL(s3Settings.CustomDomain, objectName);
|
||||
}
|
||||
else
|
||||
{
|
||||
url = URLHelpers.CombineURL(S3Settings.Bucket, objectName);
|
||||
url = URLHelpers.CombineURL(s3Settings.Bucket, objectName);
|
||||
}
|
||||
|
||||
return URLHelpers.FixPrefix(url);
|
||||
|
@ -121,15 +114,15 @@ public string GetMd5Hash(Stream stream)
|
|||
|
||||
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.");
|
||||
|
||||
using (var client = new AmazonS3Client(GetCurrentCredentials(), GetCurrentRegion()))
|
||||
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.");
|
||||
|
||||
using (var client = new AmazonS3Client(GetCurrentCredentials(), GetCurrentRegion(s3Settings)))
|
||||
{
|
||||
var putRequest = new GetPreSignedUrlRequest
|
||||
{
|
||||
BucketName = S3Settings.Bucket,
|
||||
BucketName = s3Settings.Bucket,
|
||||
Key = GetObjectKey(fileName),
|
||||
Verb = HttpVerb.PUT,
|
||||
Expires = DateTime.UtcNow.AddMinutes(5),
|
||||
|
|
|
@ -34,7 +34,8 @@ You should have received a copy of the GNU General Public License
|
|||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Windows.Forms;
|
||||
using System.Windows.Forms;
|
||||
using Amazon;
|
||||
|
||||
namespace ShareX.UploadersLib
|
||||
{
|
||||
|
@ -520,8 +521,8 @@ public void LoadSettings(UploadersConfig uploadersConfig)
|
|||
txtAmazonS3CustomDomain.Text = Config.AmazonS3Settings.CustomDomain;
|
||||
cbAmazonS3UseRRS.Checked = Config.AmazonS3Settings.UseReducedRedundancyStorage;
|
||||
|
||||
cbAmazonS3Endpoint.Items.AddRange(Amazon.RegionEndpoint.EnumerableAllRegions.ToArray());
|
||||
cbAmazonS3Endpoint.SelectedItem = Amazon.RegionEndpoint.EnumerableAllRegions.SingleOrDefault(r => r.SystemName == Config.AmazonS3Settings.Region) ?? Amazon.RegionEndpoint.USWest1;
|
||||
cbAmazonS3Endpoint.Items.AddRange(RegionEndpoint.EnumerableAllRegions.ToArray());
|
||||
cbAmazonS3Endpoint.SelectedItem = AmazonS3.GetCurrentRegion(Config.AmazonS3Settings);
|
||||
cbAmazonS3Endpoint.DisplayMember = "DisplayName";
|
||||
UpdateAmazonS3Status();
|
||||
|
||||
|
@ -1785,7 +1786,7 @@ private void txtAmazonS3SecretKey_TextChanged(object sender, EventArgs e)
|
|||
|
||||
private void cbAmazonS3Endpoint_SelectionChangeCommitted(object sender, EventArgs e)
|
||||
{
|
||||
var region = cbAmazonS3Endpoint.SelectedItem as Amazon.RegionEndpoint;
|
||||
var region = cbAmazonS3Endpoint.SelectedItem as RegionEndpoint;
|
||||
if (region != null)
|
||||
{
|
||||
Config.AmazonS3Settings.Region = region.SystemName;
|
||||
|
|
Loading…
Reference in a new issue