Amplify-js: How to get Metadata with Storage.get

Created on 20 Mar 2019  路  2Comments  路  Source: aws-amplify/amplify-js

I set up Storage file's metadata. When I check property of uploaded file, metadata is there.

How can I get metadata using Storage.get / Storage.vault.get ?

When I try simply get an object it returns only URL without metadata.

When I'm using it with { download: true } option Metadata property is there, but is always empty:

{
  Body: Uint8Array(3544474) [137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 16, 0, 0, 0, 11, 0, 8, 0, 0, 0, 0, 178, 17, 179, 255, 0, 0, 32, 0, 73, 68, 65, 84, 120, 1, 236, 193, 139, 98, 220, 70, 98, 0, 193, 238, 209, 255, 127, 178, 167, 51, 0, 246, 73, 82, 86, 114, 177, 206, 139, 21, 171, 228, 219, 183, 111, 223, 190, 125, 251, 246, 237, 219, 183, 111, 223, 190, 125, 251, 246, 246, 228, 219, 183, 111, 47, 195, 248, 3, 24, 96, 66, 鈥
  ContentType: "binary/octet-stream"
  ETag: ""2bed7285cc6cc585a01e2762763696c6""
  LastModified: Tue Mar 19 2019 10:58:43 GMT+0100 (czas 艣rodkowoeuropejski standardowy) {}
  Metadata: {}
  __proto__: Object
}

Is there a way to get metadata using amplify ?

Storage question

Most helpful comment

It may be a CORS permissioning issue that you are seeing. Metadata is returned in the http header, and you have to specify the name of any header you want returned in the CORS policy of the bucket. This is an XML file per bucket, that you would need to add one or more <ExposeHeader> lines to.

You can edit the XML file in the AWS web console by going to S3, clicking on the bucket, then selecting the 'Permissions' tab and clicking 'CORS Configuration'

image

You can also set bucket CORS programmatically if prefer to look that up.

For example, if your metadata header was x-amz-meta-foo you would add

<ExposeHeader>x-amz-meta-foo</ExposeHeader>

To the CORS file. Then when calling Storage.get(key, {download: true}) the returned object should have a Metadata object with a foo property. (note that the property name does not start with 'x-amz-meta-' when coming from the Storage class.

More information here: https://github.com/aws/aws-sdk-js/issues/232

Looks like this has been around awhile as is a limitation with how browsers work, and suggests to me at least that custom S3 metadata is to be sparingly.

All 2 comments

It may be a CORS permissioning issue that you are seeing. Metadata is returned in the http header, and you have to specify the name of any header you want returned in the CORS policy of the bucket. This is an XML file per bucket, that you would need to add one or more <ExposeHeader> lines to.

You can edit the XML file in the AWS web console by going to S3, clicking on the bucket, then selecting the 'Permissions' tab and clicking 'CORS Configuration'

image

You can also set bucket CORS programmatically if prefer to look that up.

For example, if your metadata header was x-amz-meta-foo you would add

<ExposeHeader>x-amz-meta-foo</ExposeHeader>

To the CORS file. Then when calling Storage.get(key, {download: true}) the returned object should have a Metadata object with a foo property. (note that the property name does not start with 'x-amz-meta-' when coming from the Storage class.

More information here: https://github.com/aws/aws-sdk-js/issues/232

Looks like this has been around awhile as is a limitation with how browsers work, and suggests to me at least that custom S3 metadata is to be sparingly.

Thank you @credator It is working now as expected.

Was this page helpful?
0 / 5 - 0 ratings