Last active
June 9, 2020 03:55
-
-
Save stanlee321/79859a7bc3e86c8dceff82a4709d7c38 to your computer and use it in GitHub Desktop.
Firebase upload python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from google.cloud import storage | |
| import os | |
| class FireBaseSync: | |
| def __init__(self, credentials_path = "Mnse-f823ecc07175.json", | |
| bucket_name = "musense.appspot.com"): | |
| # Set credentials | |
| os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = credentials_path | |
| # Enable Storage | |
| self.client = storage.Client() | |
| # Reference an existing bucket. | |
| self.bucket = self.client.get_bucket(bucket_name) | |
| print("STARTED...") | |
| def upload(self, image_path = "", folder_name_firebase = "", image_name_in_firebase = ""): | |
| """ | |
| Upload image | |
| """ | |
| path_in_firebase = folder_name_firebase + "/" + image_name_in_firebase | |
| # posting to firebase storage | |
| imageBlob = self.bucket.blob(path_in_firebase) | |
| imageBlob.upload_from_filename(image_path) | |
| # Make public | |
| imageBlob.make_public() | |
| return imageBlob.public_url | |
| if __name__ == '__main__': | |
| fire_uploader = FireBaseSync() | |
| image_path = "/home/REGISTRATION2019/FBQ Photo Gallery/FBQ.272/272-lamp.png" | |
| url = fire_uploader.upload(image_path = image_path, | |
| folder_name_firebase = "test", | |
| image_name_in_firebase = "test.png" | |
| ) | |
| print(url) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment