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 回答
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
已回答 8 个月前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则

相关内容