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

603 views Asked by At

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;
}
2

There are 2 answers

0
Ravi Sevta On BEST ANSWER

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.

1
Colin Moreno Burgess On

I think you should be using Dio for declaring the client object that will be used for posting the request You can find an example code in the following answer