aws-amplify / amplify-flutter

A declarative library with an easy-to-use interface for building Flutter applications on AWS.
https://docs.amplify.aws
Apache License 2.0
1.32k stars 248 forks source link

Metadata not being retrieved (add CORS info to docs) #4099

Open Tyga76 opened 12 months ago

Tyga76 commented 12 months ago

Description

I have successfully uploaded documents to my S3 bucket. I have added user-defined metadata that I can see on the object properties in the S3 console. However when I use Amplify.Storage.getProperties, it does not pull the metadata.

`

Future<List<StorageItem>?> listBucketByPath(String path) async {
    try {
      String? nextToken;
      bool hasNextPage;
      List<StorageItem>? files = [];
      do {
        final result = await Amplify.Storage.list(
          path: path,
          options: StorageListOptions(
            accessLevel: StorageAccessLevel.guest,
            pageSize: 5,
            nextToken: nextToken,
            pluginOptions: const S3ListPluginOptions(
              excludeSubPaths: false,
            ),
          ),
        ).result;
        _logger.e('Listed items: ${result.items}');
        for (final item in result.items) {
          final metadata = await Amplify.Storage.getProperties(
                  key: item.key,
                  options: const StorageGetPropertiesOptions(
                      accessLevel: StorageAccessLevel.guest))
              .result;
           _logger.e(metadata);
        }
        result.items.isNotEmpty ? files.addAll(result.items) : files.addAll([]);
        nextToken = result.nextToken;
        hasNextPage = result.hasNextPage;
      } while (hasNextPage);

      return files;
    } on StorageException catch (e) {
      _logger.e('Error listing files: ${e.message}');
      rethrow;
    }
  }

`

Categories

Steps to Reproduce

No response

Screenshots

No response

Platforms

Flutter Version

3.13.9

Amplify Flutter Version

1.0.4

Deployment Method

Amplify CLI

Schema

No response

Equartey commented 11 months ago

@Tyga76 sorry to hear you've ran into this issue.

If you're able to upgrade, have you tried a newer version of Amplify-Flutter? Do you still see the issue?

If so, can you also share the code you are using to upload the file?

Tyga76 commented 11 months ago

@Tyga76 sorry to hear you've run into this issue.

If you're able to upgrade, have you tried a newer version of Amplify-Flutter? Do you still see the issue?

If so, can you also share the code you are using to upload the file?

Hi, sorry for the delay in response..my notifications aren't working.. `` I tried updating to the latest version for amplify and Storage plugins. Same issue, unfortunately.

my code to upload 👍

Future<void> uploadFile(FileModel file, String path) async {
    // Select a file from the device

    if (file.fileName.isBlank!) {
      _logger.e('No file selected');
      return;
    }

    // Upload file with its filename as the key

    try {
      final result = await Amplify.Storage.uploadFile(
        localFile: AWSFile.fromData(file.fileData,
            name: file.fileName, contentType: file.fileMIME),
        key: '$path/${file.fileName}',
        onProgress: (progress) {
          _logger.e('Fraction completed: ${progress.fractionCompleted}');
        },
        options: StorageUploadFileOptions(
          accessLevel: StorageAccessLevel.guest,
          metadata: {'project': 'poc', 'contentType': file.fileMIME},
          pluginOptions: const S3UploadFilePluginOptions(
            getProperties: true,
          ),
        ),
      ).result;
      keyName = result.uploadedItem.key;
      _logger.e('Successfully uploaded file: ${result.uploadedItem.key}');
    } on StorageException catch (e) {
      _logger.e('Error uploading file: $e');
      rethrow;
    }
  }
Jordan-Nelson commented 6 months ago

Hello @Tyga76 - Apologies for the delay. This appears to be a gap in the documentation for Amplify Flutter. Metadata is returned through http headers, and when using Web these headers have to be added to the CORS policy for your S3 bucket. You can following the docs here to see how to do that. The docs are for react, but in this case apply to flutter as well.

Jordan-Nelson commented 6 months ago

@Tyga76 I see you noted that your deployment method was the Amplify CLI. Can you confirm if the S3 bucket was created with the Amplify CLI? The CORS policy should be created for you if you use the Amplify CLI.