10

I need some help I am using python 3.6 to upload a file to firebase storage, but I couldn't get a reasonable result .

import firebase_admin
from firebase_admin import credentials, firestore, storage

cred=credentials.Certificate('C:\\Users\\blackturtle\\Envs\\tube\\ps.json')
firebase_admin.initialize_app(cred, {
    'storageBucket': 'gs://dene-2ac17.appspot.com'
})
db = firestore.client()
bucket = storage.bucket()
blob = bucket.blob('hello.txt')
outfile='C:\\Users\\blackturtle\\Envs\\tube\\hello.txt'
blob.upload_from_filename(outfile)

That code gives this error below

Exception has occurred: google.api_core.exceptions.NotFound
404 POST https://www.googleapis.com/upload/storage/v1/b/gs://dene-2ac17.appspot.com/o?uploadType=multipart: ('Request failed with status code', 404, 'Expected one of', <HTTPStatus.OK: 200>)
  File "C:\Users\blackturtle\Envs\tube\drive.py", line 27, in <module>
    blob.upload_from_filename(outfile)

When I change and use this code below to upload file

import firebase_admin
from firebase_admin import credentials, firestore, storage

cred=credentials.Certificate('C:\\Users\\blackturtle\\Envs\\tube\\ps.json')
firebase_admin.initialize_app(cred, {
    'storageBucket': 'gs://dene-2ac17.appspot.com'
})
db = firestore.client()
bucket = storage.bucket()
blob = bucket.blob('hello.txt')
outfile='C:\\Users\\blackturtle\\Envs\\tube\\hello.txt'
with open(outfile, 'rb') as my_file:
    blob.upload_from_file(my_file)

Got this error

Exception has occurred: google.api_core.exceptions.NotFound
404 POST https://www.googleapis.com/upload/storage/v1/b/gs://dene-2ac17.appspot.com/o?uploadType=resumable: ('Response headers must contain header', 'location')
  File "C:\Users\blackturtle\Envs\tube\drive.py", line 29, in <module>
    blob.upload_from_file(my_file)

Any idea what's going on?

Thanks in advance

0

1 Answer 1

10

Try changing 'gs://dene-2ac17.appspot.com' with 'dene-2ac17.appspot.com' as explained here.

Use a default bucket

You can specify a default bucket name when initializing the Admin SDK. Then you can retrieve an authenticated reference to this bucket. The bucket name must not contain gs:// or any other protocol prefixes. For example, if the bucket URL displayed in the Firebase Console is gs://bucket-name.appspot.com, pass the string bucket-name.appspot.com to the Admin SDK.

6
  • Thanks for advice.I have just tried what you suggested but got the sama error ('Response headers must contain header', 'location') any advice will be appreciated @Cheche
    – digging
    Commented Oct 19, 2018 at 16:18
  • Ouch!, thought it would work. Was that error on your first or second code?,
    – Cheche
    Commented Oct 19, 2018 at 17:25
  • I tried both code block seperately again as you suggested way and they gave the errors as same as above . @Cheche
    – digging
    Commented Oct 19, 2018 at 19:32
  • Ok, sorry @digging - Last two shots: a) try isolating your problem removing db = firestore.client(); b) check your credentials are right.
    – Cheche
    Commented Oct 19, 2018 at 19:57
  • 1
    Thanks for taking your time for problem @Cheche are it is all about credendials problem.I copied credentials from firebase and pasted then it works like charm.Thank you again
    – digging
    Commented Oct 19, 2018 at 20:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.