Firebase storage API - upload file with metada
Asked Answered
C

2

2

I am using the rest api to upload files to firebase storage as described in the following answer https://mcmap.net/q/484396/-uploading-files-to-firebase-storage-using-rest-api.

And I would like to set the cache-control metadata but can't figure it out.

I found the next firebase doc - https://firebase.google.com/docs/storage/web/file-metadata but didn't saw any information about the rest API.

=== Update ====

So I did a little bit of reverse engineering, , and I found that when I am using the JS-SDK, the library add a prefix to the file, Any one know what is the meaning of the first long number?

enter image description here

Cleocleobulus answered 23/11, 2021 at 10:27 Comment(0)
C
2

So I found the answer:

Send the blob file with a prefix and suffix as follow:

let prefix = `
--23483860318350978727016216849495
Content-Type: application/json; charset=utf-8
Content-Disposition: form-data; name="metadata"

{"name":"${filePath}", "cacheControl":"max-age=7200","contentType":"${fileType}"}
                
--23483860318350978727016216849495
Content-Type: ${fileType}`

let suffix = '\n--23483860318350978727016216849495--'

let blobWithMetadata = new Blob([prefix, blob, suffix])

and add the following headers:

headers = { 
  'x-goog-upload-protocol': 'multipart',
  'content-type': 'multipart/related; boundary=23483860318350978727016216849495'
}
Cleocleobulus answered 1/12, 2021 at 9:29 Comment(0)
J
0

Another option is to make an additional call but use patch method instead of post and submit the metadata as json in the body.

Junkie answered 17/9, 2022 at 18:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.