amplify-flutter icon indicating copy to clipboard operation
amplify-flutter copied to clipboard

Metadata not being retrieved (add CORS info to docs)

Open Tyga76 opened this issue 1 year ago • 4 comments

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

  • [ ] Analytics
  • [ ] API (REST)
  • [ ] API (GraphQL)
  • [ ] Auth
  • [ ] Authenticator
  • [ ] DataStore
  • [ ] Notifications (Push)
  • [X] Storage

Steps to Reproduce

No response

Screenshots

No response

Platforms

  • [ ] iOS
  • [ ] Android
  • [X] Web
  • [ ] macOS
  • [ ] Windows
  • [ ] Linux

Flutter Version

3.13.9

Amplify Flutter Version

1.0.4

Deployment Method

Amplify CLI

Schema

No response

Tyga76 avatar Nov 09 '23 11:11 Tyga76

@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?

Equartey avatar Nov 16 '23 19:11 Equartey

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

Tyga76 avatar Dec 11 '23 19:12 Tyga76

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 avatar Apr 16 '24 14:04 Jordan-Nelson

@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.

Jordan-Nelson avatar Apr 16 '24 18:04 Jordan-Nelson