0

I am using the code below to upload an image to Firebase storage, which is does successfully but without an access token. How would I add an access token? I have looked on stackoverflow but none of the implementations have worked for me

import os from google.cloud import storage

os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = 'C:///file.json'
storage_client = storage.Client()
bucket = storage_client.get_bucket('name.appspot.com')
imageBlob = bucket.blob("/")
imagePath = "C:\\\image.jpg"
imageBlob = bucket.blob("image.jpg")
imageBlob.upload_from_filename(imagePath)
tech123
  • 13
  • 1
  • 7
  • You can add a token through metadata, similar to the way it's done in this node.js example: https://stackoverflow.com/a/43764656 – Frank van Puffelen Apr 26 '21 at 17:17
  • @FrankvanPuffelen I had seen this but I am not really sure how to implement it. What does fb stand for in this context? – tech123 Apr 27 '21 at 09:11
  • I don't think you'll need `fbId` or `fbKeyFile` from the answer, since you're already connected to the correct storage bucket. All you should really have to do is attach a `metadata/metadata/firebaseStorageDownloadTokens` property to your uploaded blob. https://stackoverflow.com/questions/33145047/set-metadata-in-google-cloud-storage-using-gcloud-python – Frank van Puffelen Apr 27 '21 at 14:07
  • @FrankvanPuffelen sorry posted that comment on the wrong question, I haven't got it working yet, will delete comment – tech123 Apr 28 '21 at 15:45
  • @FrankvanPuffelen I have tried the suggestion in the linked post and couldn't get it to work. This is what I added to my code metadata = {'AccessToken': '12345'} imageBlob.metadata = metadata imageBlob.content_disposition = "attachment" imageBlob.patch() – tech123 Apr 28 '21 at 15:52
  • The linked answer uses `metadata: { metadata: { firebaseStorageDownloadTokens`... So it's nested deeper, and you're missing the `firebaseStorageDownloadTokens` that seems to matter the most. From that answer: "The key thing here is that there is a `metadata` object nested within the `metadata` option property. Setting `firebaseStorageDownloadTokens` to a uuid-v4 value will tell Cloud Storage to use that as its public auth token." – Frank van Puffelen Apr 28 '21 at 17:21

0 Answers0