( key: string, customConfig?: S3Config, maxBytes?: number )
| 189 | ): Promise<Buffer> |
| 190 | |
| 191 | export async function downloadFromS3( |
| 192 | key: string, |
| 193 | customConfig?: S3Config, |
| 194 | maxBytes?: number |
| 195 | ): Promise<Buffer> { |
| 196 | const config = customConfig || { bucket: S3_CONFIG.bucket, region: S3_CONFIG.region } |
| 197 | |
| 198 | const command = new GetObjectCommand({ |
| 199 | Bucket: config.bucket, |
| 200 | Key: key, |
| 201 | }) |
| 202 | |
| 203 | const response = await getS3Client().send(command) |
| 204 | if (maxBytes !== undefined && response.ContentLength !== undefined) { |
| 205 | try { |
| 206 | assertKnownSizeWithinLimit(response.ContentLength, maxBytes, 'storage download') |
| 207 | } catch (error) { |
| 208 | const body = response.Body as { destroy?: (error?: Error) => void } | undefined |
| 209 | body?.destroy?.(error instanceof Error ? error : undefined) |
| 210 | throw error |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | const stream = response.Body as NodeJS.ReadableStream |
| 215 | return readNodeStreamToBufferWithLimit(stream, { |
| 216 | maxBytes: maxBytes ?? Number.MAX_SAFE_INTEGER, |
| 217 | label: 'storage download', |
| 218 | }) |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Stream an object out of S3 without buffering it. The caller MUST fully consume or |
no test coverage detected