Unable to list objects from a S3 Bucket using AWS Amplify (Flutter)

1k views Asked by At

I am trying to list all the items inside an S3 storage bucket. I have made my bucket public and my storage type is also set to guest but still when I run the Amplify.Storage.list() method it throws an access denied error (403)

Here's the code:

Future<void> listItems() async {
try {
 final ListResult result = await Amplify.Storage.list();
 final List<StorageItem> items = result.items;
 print('Got items: $items');
 } on StorageException catch (e) {
print('Error listing items: $e');
 }
}

What am I missing?

1

There are 1 answers

0
rvaRiverPirate On

For me, the fix was to add the s3:ListBucket action to the policy. Here is my CDK implementation (mostly from a tutorial).

    // grant only read and list access to the bucket for unauthenticated users
const giveReadAccessS3 = new PolicyDocument({
  statements: [
    new PolicyStatement({
      resources: [
        props.storageBucketARN,
        `${props.storageBucketARN}/public/*`,
      ],
      actions: ['s3:GetObject', 's3:ListBucket'],
      effect: Effect.ALLOW,
    }),
  ],
});