Skip to content

Commit e13e8af

Browse files
Merge pull request #317 from mdupras/mdupras/cached-metadata-query
Updated the `KeyExists` from aws to keep the response metadata in the image resolver
2 parents fb8b334 + 2acb001 commit e13e8af

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
@@ -108,12 +108,13 @@ public async Task<IImageResolver> GetAsync(HttpContext context)
108108
return null;
109109
}
110110

111-
if (!await KeyExists(s3Client, bucketName, key))
111+
KeyExistsResult keyExists = await KeyExists(s3Client, bucketName, key);
112+
if (!keyExists.Exists)
112113
{
113114
return null;
114115
}
115116

116-
return new AWSS3StorageImageResolver(s3Client, bucketName, key);
117+
return new AWSS3StorageImageResolver(s3Client, bucketName, key, keyExists.Metadata);
117118
}
118119

119120
private bool IsMatch(HttpContext context)
@@ -133,39 +134,40 @@ private bool IsMatch(HttpContext context)
133134
}
134135

135136
// ref https://github.com/aws/aws-sdk-net/blob/master/sdk/src/Services/S3/Custom/_bcl/IO/S3FileInfo.cs#L118
136-
private static async Task<bool> KeyExists(IAmazonS3 s3Client, string bucketName, string key)
137+
private static async Task<KeyExistsResult> KeyExists(IAmazonS3 s3Client, string bucketName, string key)
137138
{
138139
try
139140
{
140-
GetObjectMetadataRequest request = new()
141-
{
142-
BucketName = bucketName,
143-
Key = key
144-
};
141+
GetObjectMetadataRequest request = new() { BucketName = bucketName, Key = key };
145142

146143
// If the object doesn't exist then a "NotFound" will be thrown
147-
await s3Client.GetObjectMetadataAsync(request);
148-
return true;
144+
GetObjectMetadataResponse metadata = await s3Client.GetObjectMetadataAsync(request);
145+
return new KeyExistsResult(metadata);
149146
}
150147
catch (AmazonS3Exception e)
151148
{
152149
if (string.Equals(e.ErrorCode, "NoSuchBucket", StringComparison.Ordinal))
153150
{
154-
return false;
151+
return default;
155152
}
156153

157154
if (string.Equals(e.ErrorCode, "NotFound", StringComparison.Ordinal))
158155
{
159-
return false;
156+
return default;
160157
}
161158

162159
// If the object exists but the client is not authorized to access it, then a "Forbidden" will be thrown.
163160
if (string.Equals(e.ErrorCode, "Forbidden", StringComparison.Ordinal))
164161
{
165-
return false;
162+
return default;
166163
}
167164

168165
throw;
169166
}
170167
}
168+
169+
private readonly record struct KeyExistsResult(GetObjectMetadataResponse Metadata)
170+
{
171+
public bool Exists => this.Metadata is not null;
172+
}
171173
}

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

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

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

3336
/// <inheritdoc />
3437
public async Task<ImageMetadata> GetMetaDataAsync()
3538
{
36-
GetObjectMetadataResponse metadata = await this.amazonS3.GetObjectMetadataAsync(this.bucketName, this.imagePath);
39+
GetObjectMetadataResponse metadata = this.metadataResponse ?? await this.amazonS3.GetObjectMetadataAsync(this.bucketName, this.imagePath);
3740

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

0 commit comments

Comments
 (0)