Skip to content

Commit b7e03f6

Browse files
Merge branch 'main' into stefannikolei/nullable/aws
2 parents 737eb59 + e13e8af commit b7e03f6

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

src/ImageSharp.Web.Providers.AWS/Providers/AWSS3StorageImageProvider.cs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,13 @@ public bool IsValidRequest(HttpContext context)
113113
return null;
114114
}
115115

116-
if (!await KeyExists(s3Client, bucketName, key))
116+
KeyExistsResult keyExists = await KeyExists(s3Client, bucketName, key);
117+
if (!keyExists.Exists)
117118
{
118119
return null;
119120
}
120121

121-
return new AWSS3StorageImageResolver(s3Client, bucketName, key);
122+
return new AWSS3StorageImageResolver(s3Client, bucketName, key, keyExists.Metadata);
122123
}
123124

124125
private bool IsMatch(HttpContext context)
@@ -144,39 +145,40 @@ private bool IsMatch(HttpContext context)
144145
}
145146

146147
// ref https://github.com/aws/aws-sdk-net/blob/master/sdk/src/Services/S3/Custom/_bcl/IO/S3FileInfo.cs#L118
147-
private static async Task<bool> KeyExists(IAmazonS3 s3Client, string bucketName, string key)
148+
private static async Task<KeyExistsResult> KeyExists(IAmazonS3 s3Client, string bucketName, string key)
148149
{
149150
try
150151
{
151-
GetObjectMetadataRequest request = new()
152-
{
153-
BucketName = bucketName,
154-
Key = key
155-
};
152+
GetObjectMetadataRequest request = new() { BucketName = bucketName, Key = key };
156153

157154
// If the object doesn't exist then a "NotFound" will be thrown
158-
await s3Client.GetObjectMetadataAsync(request);
159-
return true;
155+
GetObjectMetadataResponse metadata = await s3Client.GetObjectMetadataAsync(request);
156+
return new KeyExistsResult(metadata);
160157
}
161158
catch (AmazonS3Exception e)
162159
{
163160
if (string.Equals(e.ErrorCode, "NoSuchBucket", StringComparison.Ordinal))
164161
{
165-
return false;
162+
return default;
166163
}
167164

168165
if (string.Equals(e.ErrorCode, "NotFound", StringComparison.Ordinal))
169166
{
170-
return false;
167+
return default;
171168
}
172169

173170
// If the object exists but the client is not authorized to access it, then a "Forbidden" will be thrown.
174171
if (string.Equals(e.ErrorCode, "Forbidden", StringComparison.Ordinal))
175172
{
176-
return false;
173+
return default;
177174
}
178175

179176
throw;
180177
}
181178
}
179+
180+
private readonly record struct KeyExistsResult(GetObjectMetadataResponse Metadata)
181+
{
182+
public bool Exists => this.Metadata is not null;
183+
}
182184
}

src/ImageSharp.Web.Providers.AWS/Resolvers/AWSS3StorageImageResolver.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,27 @@ public class AWSS3StorageImageResolver : IImageResolver
1515
private readonly IAmazonS3 amazonS3;
1616
private readonly string bucketName;
1717
private readonly string imagePath;
18+
private readonly GetObjectMetadataResponse metadataResponse;
1819

1920
/// <summary>
2021
/// Initializes a new instance of the <see cref="AWSS3StorageImageResolver"/> class.
2122
/// </summary>
2223
/// <param name="amazonS3">The Amazon S3 Client</param>
2324
/// <param name="bucketName">The bucket name.</param>
2425
/// <param name="imagePath">The image path.</param>
25-
public AWSS3StorageImageResolver(IAmazonS3 amazonS3, string bucketName, string imagePath)
26+
/// <param name="metadataResponse">Optional metadata response.</param>
27+
public AWSS3StorageImageResolver(IAmazonS3 amazonS3, string bucketName, string imagePath, GetObjectMetadataResponse metadataResponse = null)
2628
{
2729
this.amazonS3 = amazonS3;
2830
this.bucketName = bucketName;
2931
this.imagePath = imagePath;
32+
this.metadataResponse = metadataResponse;
3033
}
3134

3235
/// <inheritdoc />
3336
public async Task<ImageMetadata> GetMetaDataAsync()
3437
{
35-
GetObjectMetadataResponse metadata = await this.amazonS3.GetObjectMetadataAsync(this.bucketName, this.imagePath);
38+
GetObjectMetadataResponse metadata = this.metadataResponse ?? await this.amazonS3.GetObjectMetadataAsync(this.bucketName, this.imagePath);
3639

3740
// Try to parse the max age from the source. If it's not zero then we pass it along
3841
// to set the cache control headers for the response.

0 commit comments

Comments
 (0)