Upload to Firebase Storage Image from Path

Viewed 24

I have currently a List of Strings (str) which hold the local path to the image for example it is: '/home/rene/development/scrapping/images/_0_b87e0.jpg'

This is one of the strings inside of the List listing.photos.

The problem I'm having is that I want to upload it to Firebase storage, I have this code:

for image in listing.photos:
            imageFile = Image.open(image)
            outfile = imageFile.save(imageFile)
            of = open(outfile, 'rb')
            bucket = storage.bucket()
            blob = bucket.blob('')
            blob.upload_from_file(of)
            blob.make_public()
            photos.append(blob.public_url)

I get the error ValueError: unknown file extension

How can I upload the images using the path to Firebase?

Also if possible how can I add a route to the images for example I want to add to have the x amount of images in Firebase Storage with the route /lisitingImages/(A unique Id maybe a ref Id)/

Any Ideas on how to upload with just the path to it?

Kind Regards

1 Answers

If any one faces the same Issue I found the way, basically the problem was this line:

blob = bucket.blob('')

As the Blob requires the information of the file you will be uploading or at least it did for me, and instead of using the full path (computers full path), I just got the path of the folder in the project instead of using:

'/home/rene/development/scrapping/images/_0_b87e0.jpg'

I used 'images/_0_b87e0.jpg'

And separate this into path: 'images/' and filename '_0_b87e0.jpg', and finally got the following code:

for image in listing.photos:
            fullpath = find_between(image, 'scrapping/', '.jpg') + '.jpg'

            filename = fullpath[7:]
            path = fullpath[0:6]

            imagePath = path + '/' + filename
            bucket = storage.bucket()
            blob = bucket.blob('ListingImages/' + ref.id + '/' + filename)
            blob.upload_from_filename(imagePath)
            blob.make_public()
            photos.append(blob.public_url)

FYI: the function find_between, is just a function I created to get the substring between 2 strings.

Hope this helps

Related