How to pass ACL properties to flutter amplify while uploading file to S3?

0

Issue

Or How to upload an image to s3 with public access by Flutter Amplify?

In my current flutter project, I can’t pass ACL:public-read property while uploading files to S3 using amplify.

And because of this, whenever I’m uploading a new file to s3, I need to make it public manually.
So I just want to upload a new file with public read access for everyone.

I found some solutions for the Javascript project but not in the Flutter project.

Below is a method, I’m using to upload.

Future<String> uploadFile(String fileName, File local) async {
try {
  Map<String, String> metadata = <String, String>{};
  metadata['name'] = 'filename';
  metadata['desc'] = 'A file';

  S3UploadFileOptions options = S3UploadFileOptions(accessLevel: StorageAccessLevel.guest, metadata: metadata);
  UploadFileResult result = await Amplify.Storage.uploadFile(key: fileName, local: local, options: options);
  return result.key;
} catch (e) {
  print('UploadFile Err: ' + e.toString());
}

return null;
}

Solution

So far Flutter Amplify is not giving any option to upload images with public access.
It always uploads with private read access.

So I updated a few things in my project as described below.

Before Amplify integration I was uploading images to S3 and storing that URL to my server, and wherever I have to display, I’m just fetching URL from my server and loading images.

But now I’m storing key(that is used to upload images to S3 by Amplify) to my server.

And to display the image I’m getting the image URL from Amplify using that key(which is stored in my server).
Amplify adds a token to the image URL with a default validity of 7 days

Future<String> getUrl(String key) async {
    try {
      S3GetUrlOptions options = S3GetUrlOptions(accessLevel: StorageAccessLevel.guest, expires: 10000);
      GetUrlResult result = await Amplify.Storage.getUrl(key: key, options: options);
      String url = result.url;
      return url;
    } catch (e) {
      print('GetUrl Err: ' + e.toString());
    }

    return null;
  }

So it can be displayed by ImageView.

Answered By – Ravi Sevta

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More