@@ -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}
0 commit comments