(bucket: Bucket, filePath: string)
| 184 | }; |
| 185 | |
| 186 | export const getFileFromBucket = async (bucket: Bucket, filePath: string) => { |
| 187 | const { name: bucketName, region: bucketRegion } = bucket; |
| 188 | const s3 = initializeS3Service(bucketRegion); |
| 189 | |
| 190 | logger.debug(`Fetching file '${filePath}' from S3 bucket '${bucketName}'`); |
| 191 | const getCommand = new GetObjectCommand({ Bucket: bucketName, Key: filePath }); |
| 192 | const goGet = await go(() => s3.send(getCommand)); |
| 193 | if (!goGet.success) { |
| 194 | throw new Error(`Failed to fetch file '${filePath}' from S3 bucket '${bucketName}': ${goGet.error}`); |
| 195 | } |
| 196 | if (!goGet.data.Body) { |
| 197 | throw new Error(`The response for file '${filePath}' from S3 bucket '${bucketName}' contained an empty body`); |
| 198 | } |
| 199 | |
| 200 | const goFileContent = await go(() => goGet.data.Body!.transformToString('utf-8')); |
| 201 | if (!goFileContent.success) { |
| 202 | throw new Error(`The response for file '${filePath}' from S3 bucket '${bucketName}' is not parsable`); |
| 203 | } |
| 204 | |
| 205 | return goFileContent.data; |
| 206 | }; |
| 207 | |
| 208 | export const copyFileInBucket = async (bucket: Bucket, fromFilePath: string, toFilePath: string) => { |
| 209 | const { name: bucketName, region: bucketRegion } = bucket; |
no test coverage detected