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