aws-amplify / amplify-android

The fastest and easiest way to use AWS from your Android app.
https://docs.amplify.aws/lib/q/platform/android/
Apache License 2.0
239 stars 112 forks source link

Retrieve metadata #2805

Open mbahmani90 opened 2 months ago

mbahmani90 commented 2 months ago

Before opening, please confirm:

Language and Async Model

Kotlin, Kotlin - Coroutines

Amplify Categories

Storage

Gradle script dependencies

```groovy // Put output below this line implementation 'com.amplifyframework:aws-storage-s3:2.16.0' ```

Environment information

``` # Put output below this line ```

Please include any relevant guides or documentation you're referencing

No response

Describe the bug

Hi,

Hope you are fine.

I want to add some values to a file metadate. According to https://docs.amplify.aws/gen1/android/build-a-backend/storage/upload/ it can define a custom option and put metadata values there. When I check S3 files in the console I can file the metadata which has been stored.

    File exampleFile = new File(getApplicationContext().getFilesDir(), "example");
    try {
        BufferedWriter writer = new BufferedWriter(new FileWriter(exampleFile));
        writer.append("Example file contents");
        writer.close();
    } catch (Exception exception) {
        Log.e("MyAmplifyApp", "Upload failed", exception);
    }

    // Create metadata
    Map<String, String> userMetadata = new HashMap<>();
    userMetadata.put("myKey", "myVal");

    // Configure upload options with metadata
    StorageUploadFileOptions options = StorageUploadFileOptions.builder()
        .metadata(userMetadata)
        .build();

    // Perform the upload
    Amplify.Storage.uploadFile(
        StoragePath.fromString("public/example"),
        exampleFile,
        options,
        result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + result.getPath()),
        error -> Log.e("MyAmplifyApp", "Upload failed", error)
    );

However, I couldn't find a way to read metadata of downloaded file https://docs.amplify.aws/gen1/android/build-a-backend/storage/download/.

Can anyone explain how it is possible to retrieve metadata of a downloaded file?

Thanks

Reproduction steps (if applicable)

No response

Code Snippet

// Put your code below this line.

Log output

``` // Put your logs below this line ```

amplifyconfiguration.json

No response

GraphQL Schema

```graphql // Put your schema below this line ```

Additional information and screenshots

No response

tylerjroach commented 2 months ago

@mbahmani90 It does not appear to be possible at this time. I've labeled this ticket as a feature request, and I will also check to see if other Amplify platforms already support this.

As a workaround, I think the best way to get metadata on an object would be to use the escape hatch and use our S3 APIs directly.

Here's an example snippet that makes a HEAD request, fetching only the metadata, not the entire object.

val plugin = Amplify.Storage.getPlugin("awsS3StoragePlugin") as AWSS3StoragePlugin
val client: S3Client = plugin.escapeHatch

val headResponse = client.headObject {
    bucket = "<bucket-name>"
    key = "public/example.txt"
}

val metadata = headResponse.metadata
mbahmani90 commented 2 months ago

Thanks for your support.

I will check your advice soon.

mbahmani90 commented 2 months ago

Could you please explain more how it is possible to use headObject:

bucket is not found!

image

tylerjroach commented 2 months ago

Hi @mbahmani90, replace "<bucket-name>" with the name of your bucket.

tylerjroach commented 2 months ago

Sorry, I see what you mean. Can you show me your imports?

In the test code I used to test this, I have:

import aws.sdk.kotlin.services.s3.S3Client
import aws.sdk.kotlin.services.s3.headObject
mbahmani90 commented 2 months ago

Tylor sorry for the delay in response.

Thank you very much it works.

Only a silly question. Is it secure to use bucket = "" in the code?

You are a great guy

tylerjroach commented 2 months ago

Bucket is ok to include in code. The bucket is already stored in the amplifyconfiguration.json that is shipped with your application.