@@ -108,12 +108,13 @@ public async Task<IImageResolver> GetAsync(HttpContext context)
108
108
return null ;
109
109
}
110
110
111
- if ( ! await KeyExists ( s3Client , bucketName , key ) )
111
+ KeyExistsResult keyExists = await KeyExists ( s3Client , bucketName , key ) ;
112
+ if ( ! keyExists . Exists )
112
113
{
113
114
return null ;
114
115
}
115
116
116
- return new AWSS3StorageImageResolver ( s3Client , bucketName , key ) ;
117
+ return new AWSS3StorageImageResolver ( s3Client , bucketName , key , keyExists . Metadata ) ;
117
118
}
118
119
119
120
private bool IsMatch ( HttpContext context )
@@ -133,39 +134,40 @@ private bool IsMatch(HttpContext context)
133
134
}
134
135
135
136
// 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 )
137
138
{
138
139
try
139
140
{
140
- GetObjectMetadataRequest request = new ( )
141
- {
142
- BucketName = bucketName ,
143
- Key = key
144
- } ;
141
+ GetObjectMetadataRequest request = new ( ) { BucketName = bucketName , Key = key } ;
145
142
146
143
// 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 ) ;
149
146
}
150
147
catch ( AmazonS3Exception e )
151
148
{
152
149
if ( string . Equals ( e . ErrorCode , "NoSuchBucket" , StringComparison . Ordinal ) )
153
150
{
154
- return false ;
151
+ return default ;
155
152
}
156
153
157
154
if ( string . Equals ( e . ErrorCode , "NotFound" , StringComparison . Ordinal ) )
158
155
{
159
- return false ;
156
+ return default ;
160
157
}
161
158
162
159
// If the object exists but the client is not authorized to access it, then a "Forbidden" will be thrown.
163
160
if ( string . Equals ( e . ErrorCode , "Forbidden" , StringComparison . Ordinal ) )
164
161
{
165
- return false ;
162
+ return default ;
166
163
}
167
164
168
165
throw ;
169
166
}
170
167
}
168
+
169
+ private readonly record struct KeyExistsResult ( GetObjectMetadataResponse Metadata )
170
+ {
171
+ public bool Exists => this . Metadata is not null ;
172
+ }
171
173
}
0 commit comments