Skip to content

Commit

Permalink
cleaned up S3 integration, fixes #8
Browse files Browse the repository at this point in the history
  • Loading branch information
ctolkien committed Sep 9, 2016
1 parent b889f84 commit 89d7f2f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 17 deletions.
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,20 @@ The result of any compress operation can be stored directly on to Amazon S3 stor
If you're going to be storing images for most requests onto S3, then you can pass in an `AmazonS3Configuration` object to the constructor.

```csharp
using (var png = new TinyPngClient("yourSecretApiKey", new AmazonS3Configuration("awsAccessKeyId", "awsSecretAccessKey", "defaultRegion")))
using (var png = new TinyPngClient("yourSecretApiKey",
new AmazonS3Configuration("awsAccessKeyId", "awsSecretAccessKey", "bucket", "region")))
{
var compressedCat = await png.Compress("cat.jpg");
var s3Uri = await png.SaveCompressedImageToAmazonS3(compressedCat, "bucket/file-name.png");
var s3Uri = await png.SaveCompressedImageToAmazonS3(compressedCat, "file-name.png");

//if you'd like to override the particular bucket or region
//an image is being stored to from what is specified in the AmazonS3Configuration:
var s3UriInNewSpot = await png.SaveCompressedImageToAmazonS3(
compressedCat,
"file-name.png",
bucketOverride: "different-bucket",
regionOverride: "different-region");

}

```
Expand All @@ -123,7 +133,12 @@ You can also pass a `AmazonS3Configuration` object directly into calls to `SaveC
using (var png = new TinyPngClient("yourSecretApiKey"))
{
var compressedCat = await png.Compress("cat.jpg");
var s3Uri = await png.SaveCompressedImageToAmazonS3(compressedCat, new AmazonS3Configuration("awsAccessKeyId", "awsSecretAccessKey", "defaultRegion"), "bucket/file-name.png");
var s3Uri = await png.SaveCompressedImageToAmazonS3(compressedCat,
new AmazonS3Configuration(
"awsAccessKeyId",
"awsSecretAccessKey",
"bucket",
"region"), "file-name.png");
}

```
Expand Down
23 changes: 18 additions & 5 deletions src/TinyPNG/AmazonS3Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,39 @@ public class AmazonS3Configuration
[JsonProperty("service")]
public const string Service = "s3";

public AmazonS3Configuration(string awsAccessKeyId, string awsSecretAccessKey, string defaultRegion)
public AmazonS3Configuration(string awsAccessKeyId,
string awsSecretAccessKey,
string defaultBucket,
string defaultRegion)
{
AwsAccessKeyId = awsAccessKeyId;
AwsSecretAccessKey = awsSecretAccessKey;
Bucket = defaultBucket;
Region = defaultRegion;
}

[JsonProperty("aws_access_key_id")]
public string AwsAccessKeyId { get; }
[JsonProperty("aws_secret_access_key")]
public string AwsSecretAccessKey { get; }

[JsonProperty("region")]
public string Region { get; set; }
[JsonProperty("path")]
[JsonIgnore]
public string Bucket { get; set; }
[JsonIgnore]
public string Path { get; set; }

[JsonProperty("path")]
public string BucketPath
{
get
{
return $"{Bucket}/{Path}";
}
}

public AmazonS3Configuration Clone()
{
return new AmazonS3Configuration(AwsAccessKeyId, AwsSecretAccessKey, Region);
return new AmazonS3Configuration(AwsAccessKeyId, AwsSecretAccessKey, Bucket, Region);
}

}
Expand Down
15 changes: 9 additions & 6 deletions src/TinyPNG/TinyPng.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ public async Task<TinyPngResizeResponse> Resize(TinyPngCompressResponse result,
/// </summary>
/// <param name="result">The previously compressed image</param>
/// <param name="amazonSettings">The settings for the amazon connection</param>
/// <param name="pathBucket">The path and bucket to store in: bucket/file.png format</param>
/// <param name="path">The path and bucket to store in: bucket/file.png format</param>
/// <returns></returns>
public async Task<Uri> SaveCompressedImageToAmazonS3(TinyPngCompressResponse result, AmazonS3Configuration amazonSettings, string pathBucket)
public async Task<Uri> SaveCompressedImageToAmazonS3(TinyPngCompressResponse result, AmazonS3Configuration amazonSettings, string path)
{
if (result == null)
throw new ArgumentNullException(nameof(result));
Expand All @@ -184,7 +184,7 @@ public async Task<Uri> SaveCompressedImageToAmazonS3(TinyPngCompressResponse res
throw new ArgumentNullException(nameof(amazonSettings));


amazonSettings.Path = pathBucket;
amazonSettings.Path = path;

var amazonSettingsAsJson = JsonConvert.SerializeObject(new { store = amazonSettings }, jsonSettings);

Expand All @@ -209,7 +209,7 @@ public async Task<Uri> SaveCompressedImageToAmazonS3(TinyPngCompressResponse res
/// <param name="pathBucket">The path and bucket to store in: bucket/file.png format</param>
/// <param name="regionOverride">Optional: To override the previosly configured region</param>
/// <returns></returns>
public async Task<Uri> SaveCompressedImageToAmazonS3(TinyPngCompressResponse result, string pathBucket, string regionOverride = "")
public async Task<Uri> SaveCompressedImageToAmazonS3(TinyPngCompressResponse result, string path, string bucketOverride = "", string regionOverride = "")
{
if (result == null)
throw new ArgumentNullException(nameof(result));
Expand All @@ -218,12 +218,15 @@ public async Task<Uri> SaveCompressedImageToAmazonS3(TinyPngCompressResponse res
throw new InvalidOperationException("AmazonS3Configuration has not been configured");

var amazonSettings = AmazonS3Configuration.Clone();
amazonSettings.Path = pathBucket;
amazonSettings.Path = path;

if (!string.IsNullOrEmpty(regionOverride))
amazonSettings.Region = regionOverride;

return await SaveCompressedImageToAmazonS3(result, amazonSettings, pathBucket);
if (!string.IsNullOrEmpty(bucketOverride))
amazonSettings.Bucket = bucketOverride;

return await SaveCompressedImageToAmazonS3(result, amazonSettings, path);
}

#region IDisposable Support
Expand Down
6 changes: 3 additions & 3 deletions tests/TinyPng.Tests/TinyPngTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@ public async Task CompressAndStoreToS3ShouldThrowIfS3HasNotBeenConfigured()
private const string ApiKey = "lolwat";
private const string ApiAccessKey = "lolwat";

[Fact(Skip ="Integration")]
[Fact]
public async Task CompressAndStoreToS3()
{
var pngx = new TinyPngClient(apiKey);

var result = await pngx.Compress(Cat);

var sendToAmazon = (await pngx.SaveCompressedImageToAmazonS3(result,
new AmazonS3Configuration(ApiKey, ApiAccessKey, "ap-southeast-2"),
"tinypng-test-bucket/path.jpg")).ToString();
new AmazonS3Configuration(ApiKey, ApiAccessKey, "tinypng-test-bucket", "ap-southeast-2"),
"path.jpg")).ToString();

Assert.Equal("https://s3-ap-southeast-2.amazonaws.com/tinypng-test-bucket/path.jpg", sendToAmazon);

Expand Down

0 comments on commit 89d7f2f

Please sign in to comment.