🌳
pt0/bktF/deployF/ensureBucketF.mts
1import { HeadBucketCommand, CreateBucketCommand } from '@aws-sdk/client-s3'
5export const ensureBucket = async ({bucket_name}: {bucket_name: string}) => {
6 const s3 = getS3Client({bucket_name})
7 for (let attempt = 0; attempt < 3; attempt++) {
8 try {
9 await s3.send(new HeadBucketCommand({Bucket: bucket_name}))
10 return
11 } catch (e: any) {
12 if (e.name === 'NotFound' || e.Code === 'NoSuchBucket') {
13 console.log(`creating bucket ${bucket_name}...`)
14 await s3.send(new CreateBucketCommand({Bucket: bucket_name}))
15 return
16 }
17 if (attempt < 2) { await sleep(5000); continue }
18 throw e
19 }
20 }