Storage
To use Storage today, you must use the AWS Cloud Development Kit (CDK). To get started, install the CDK library package:
1npm add --save-dev aws-cdk-lib
Next, modify your project's backend.ts
file to create an Amazon S3 bucket and grant Amplify-generated resources access to read and write:
1import * as s3 from 'aws-cdk-lib/aws-s3';2import { defineBackend } from '@aws-amplify/backend';3import { auth } from './auth/resource.js';4import { data } from './data/resource.js';5
6const backend = defineBackend({7 auth,8 data9});10
11// create the bucket and its stack12const bucketStack = backend.createStack('BucketStack');13const bucket = new s3.Bucket(bucketStack, 'Bucket', {14 blockPublicAccess: s3.BlockPublicAccess.BLOCK_ALL15});16
17// allow any authenticated user to read and write to the bucket18const authRole = backend.resources.auth.resources.authenticatedUserIamRole;19bucket.grantReadWrite(authRole);20
21// allow any guest (unauthenticated) user to read from the bucket22const unauthRole = backend.resources.auth.resources.unauthenticatedUserIamRole;23bucket.grantRead(unauthRole);
Finally, run npx amplify sandbox
to create your first Amazon S3 bucket!