Retrieve metadata
Before opening, please confirm:
- [X] I have searched for duplicate or closed issues and discussions.
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
@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
Thanks for your support.
I will check your advice soon.
Could you please explain more how it is possible to use headObject:
bucket is not found!
Hi @mbahmani90, replace "<bucket-name>" with the name of your bucket.
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
Tylor sorry for the delay in response.
Thank you very much it works.
Only a silly question. Is it secure to use bucket = "
You are a great guy
Bucket is ok to include in code. The bucket is already stored in the amplifyconfiguration.json that is shipped with your application.