Hello guys. A simple question. Could anyone point ...
# sst
i
Hello guys. A simple question. Could anyone point me on S3 configuration in SST app? I created storage stack and trying to upload a file on the front-end side using
aws-amplify
but getting
CORS
error. On the docs I have found this info only:
Copy code
this.auth.attachPermissionsForAuthUsers([
      // Allow access to the API
      api,
      // Policy granting access to a specific folder in the bucket
      new iam.PolicyStatement({
        actions: ["s3:*"],
        effect: iam.Effect.ALLOW,
        resources: [
          bucket.bucketArn + "/private/${<http://cognito-identity.amazonaws.com:sub|cognito-identity.amazonaws.com:sub>}/*",
        ],
      }),
    ]);
r
You can do this kind of thing on the bucket itself
Copy code
bucket.s3Bucket.addCorsRule( {
  allowedMethods: [HttpMethods.GET, HttpMethods.PUT],
  allowedOrigins: ['*'],
  allowedHeaders: ['*'],
});
i
@Ross Coundon Where can I import
HttpMethods
module from?
r
Copy code
import { BlockPublicAccess, BucketAccessControl, EventType, HttpMethods } from 'aws-cdk-lib/aws-s3';
i
@Ross Coundon It is working. Thank you very much. You saved my time)
f
Noticed a few ppl were trying to do this, we thought it makes sense to provide an easier way to configure this on the Bucket in
v1.0.0-beta.22
It can be configured like this:
Copy code
new Bucket(stack, "Bucket", {
  cors: [
    {
      allowedMethods: ["GET"],
      allowedOrigins: ["<https://www.example.com>"],
    }
  ],
});
No need to import
HttpMethods
r
Cool!
f
@Ross Coundon this is a design pattern we try to follow for v1. For all constructs, u can do “everything” via the cdk prop:
Copy code
new Bucket(stack, "Bucket", {
  cdk: {
    bucket: { ...all cdk.s3.Bucket's props... }
  }
});
And if we notice some prop is getting frequently used, we lift it to the top, ie.
Copy code
new Bucket(stack, "Bucket", {
  cors: { ... },
  cdk: {
    bucket: { ...all cdk.s3.Bucket's props... }
  }
});
And when we lift it, we make sure: • no need to import CDK class/enum • type safe • example right in ts doc
r
love it