6
6
UploadPartCommand ,
7
7
CompleteMultipartUploadCommand ,
8
8
HeadObjectCommand ,
9
- CreateBucketCommand
9
+ CreateBucketCommand ,
10
+ PutPublicAccessBlockCommand ,
11
+ PutBucketCorsCommand
10
12
} from "@aws-sdk/client-s3"
11
13
import { getSignedUrl } from "@aws-sdk/s3-request-presigner"
12
14
import dotenv from "dotenv"
@@ -146,7 +148,8 @@ export const createBucket = functions
146
148
Bucket : data . bucketName ,
147
149
CreateBucketConfiguration : {
148
150
LocationConstraint : String ( process . env . AWS_REGION )
149
- }
151
+ } ,
152
+ ObjectOwnership : "BucketOwnerPreferred"
150
153
} )
151
154
152
155
try {
@@ -156,6 +159,37 @@ export const createBucket = functions
156
159
// Check response.
157
160
if ( response . $metadata . httpStatusCode === 200 && ! ! response . Location )
158
161
printLog ( `The AWS S3 bucket ${ data . bucketName } has been created successfully` , LogLevel . LOG )
162
+
163
+ const publicBlockCommand = new PutPublicAccessBlockCommand ( {
164
+ Bucket : data . bucketName ,
165
+ PublicAccessBlockConfiguration : {
166
+ BlockPublicAcls : false ,
167
+ BlockPublicPolicy : false ,
168
+ }
169
+ } )
170
+
171
+ // Allow objects to be public
172
+ const publicBlockResponse = await S3 . send ( publicBlockCommand )
173
+ // Check response.
174
+ if ( publicBlockResponse . $metadata . httpStatusCode === 200 )
175
+ printLog ( `The AWS S3 bucket ${ data . bucketName } has been set with the PublicAccessBlock disabled.` , LogLevel . LOG )
176
+
177
+ // Set CORS
178
+ const corsCommand = new PutBucketCorsCommand ( {
179
+ Bucket : data . bucketName ,
180
+ CORSConfiguration : {
181
+ CORSRules : [
182
+ {
183
+ AllowedMethods : [ "GET" ] ,
184
+ AllowedOrigins : [ "*" ] ,
185
+ }
186
+ ]
187
+ }
188
+ } )
189
+ const corsResponse = await S3 . send ( corsCommand )
190
+ // Check response.
191
+ if ( corsResponse . $metadata . httpStatusCode === 200 )
192
+ printLog ( `The AWS S3 bucket ${ data . bucketName } has been set with the CORS configuration.` , LogLevel . LOG )
159
193
} catch ( error : any ) {
160
194
/** * {@link https://docs.aws.amazon.com/simspaceweaver/latest/userguide/troubleshooting_bucket-name-too-long.html | InvalidBucketName } */
161
195
if ( error . $metadata . httpStatusCode === 400 && error . Code === `InvalidBucketName` )
@@ -308,7 +342,7 @@ export const startMultiPartUpload = functions
308
342
const S3 = await getS3Client ( )
309
343
310
344
// Prepare S3 command.
311
- const command = new CreateMultipartUploadCommand ( { Bucket : bucketName , Key : objectKey } )
345
+ const command = new CreateMultipartUploadCommand ( { Bucket : bucketName , Key : objectKey , ACL : "private" } )
312
346
313
347
try {
314
348
// Execute S3 command.
0 commit comments