( key: string, customConfig?: S3Config )
| 241 | * Returns null when the object is missing. |
| 242 | */ |
| 243 | export async function headS3Object( |
| 244 | key: string, |
| 245 | customConfig?: S3Config |
| 246 | ): Promise<{ size: number; contentType?: string } | null> { |
| 247 | const config = customConfig || { bucket: S3_CONFIG.bucket, region: S3_CONFIG.region } |
| 248 | |
| 249 | try { |
| 250 | const response = await getS3Client().send( |
| 251 | new HeadObjectCommand({ Bucket: config.bucket, Key: key }) |
| 252 | ) |
| 253 | return { |
| 254 | size: response.ContentLength ?? 0, |
| 255 | contentType: response.ContentType, |
| 256 | } |
| 257 | } catch (error) { |
| 258 | const code = (error as { name?: string; $metadata?: { httpStatusCode?: number } } | null)?.name |
| 259 | const status = (error as { $metadata?: { httpStatusCode?: number } } | null)?.$metadata |
| 260 | ?.httpStatusCode |
| 261 | if (code === 'NotFound' || code === 'NoSuchKey' || status === 404) { |
| 262 | return null |
| 263 | } |
| 264 | throw error |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Delete a file from S3 |
no test coverage detected