From flutter amplify uploaded image to s3 bucket successfully but not able to set global public access for that object

0

Using flutter amplify connected to s3 bucket which is public. To connect I am using identity provider which uses cognito user pool for authentication. Also associated identity provider to IAM role which has admin access to S3. Usecase is that I have to upload an image using logged in user mobile. Once. uploaded it should become available for global user (not app users but to anyone). But when I uploaded with below options it still denying access for public to access using url. What am I missing?

    final result = await Amplify.Storage.uploadFile(
      localFile: AWSFile.fromStream(imageStream, size: imageBytes.length),
      key: keyPath,
      options: StorageUploadFileOptions(accessLevel: StorageAccessLevel.guest, metadata: {'acl': 'public-read'}),
      onProgress: (progress) {
        safePrint('Fraction completed: ${progress.fractionCompleted}');
      },
    ).result;
1 Answer
0

Thanks for your question.

It seems like you are using the Amplify Flutter library to upload a file to an S3 bucket and trying to make it publicly accessible. The code you provided specifies the access level as StorageAccessLevel.guest and sets the ACL (Access Control List) metadata to 'public-read', which should theoretically make the uploaded object publicly accessible.

To make the object publicly accessible, you need to set the ACL to "public-read" at the time of object creation or after the object is created. You can try the following code to update the ACL of the uploaded object to "public-read":

final response = await Amplify.Storage.updateObjectACL({ bucketName: '', key: '', acl: 'public-read' });

However, there are a few considerations to ensure that the object is indeed publicly accessible:

Double-check the S3 bucket policy to make sure it allows public access. You can configure a bucket policy that allows public access to objects.

Ensure that the CORS (Cross-Origin Resource Sharing) configuration of your S3 bucket allows requests from any origin (or from your app's origin).

You can take a look at the official documentation for some examples.

You can also check how to set the file access level here.

AWS
David C
answered 8 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions