Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retrieve metadata #2805

Open
1 task done
mbahmani90 opened this issue May 5, 2024 · 7 comments
Open
1 task done

Retrieve metadata #2805

mbahmani90 opened this issue May 5, 2024 · 7 comments
Labels
feature-request Request a new feature storage Related to the Storage category/plugins

Comments

@mbahmani90
Copy link

Before opening, please confirm:

Language and Async Model

Kotlin, Kotlin - Coroutines

Amplify Categories

Storage

Gradle script dependencies

// 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

// Put your schema below this line

Additional information and screenshots

No response

@github-actions github-actions bot added the pending-triage Issue is pending triage label May 5, 2024
@tylerjroach tylerjroach added the feature-request Request a new feature label May 6, 2024
@github-actions github-actions bot removed the pending-triage Issue is pending triage label May 6, 2024
@tylerjroach
Copy link
Member

@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
Copy link
Author

Thanks for your support.

I will check your advice soon.

@mbahmani90
Copy link
Author

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

bucket is not found!

image

@tylerjroach
Copy link
Member

tylerjroach commented May 6, 2024

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

@tylerjroach
Copy link
Member

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
Copy link
Author

mbahmani90 commented May 6, 2024

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
Copy link
Member

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

@joon-won joon-won added the storage Related to the Storage category/plugins label May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request Request a new feature storage Related to the Storage category/plugins
Projects
None yet
Development

No branches or pull requests

3 participants