【问题标题】:Unable to upload image to firebase storage from python server无法从 python 服务器将图像上传到 Firebase 存储
【发布时间】:2021-03-25 20:14:21
【问题描述】:

我正在创建一个 android 应用程序,它将图像发送到 python 服务器,我想从我的 python 服务器将接收到的图像上传到 firebase 存储。我的问题是,当我尝试从 python 服务器上传接收到的图像时,只有文件名存储在指定的集合中,但我的图像没有上传。我的 Firebase 存储看起来像这样 Firebase storage screenshot

在随附的屏幕截图中,以 Actual 开头的第一张图片是我从 android 上传的图片,第二张以 processed 开头的图片是我尝试从 python 上传但无法上传的图片。文件类型也不同于从android上传的文件类型。下面是我用来从服务器上传图像的代码: 我从android接收图像的功能:

def handle_request():
    print(flask.request.files)
    print(flask.request.form)
    imagefile = flask.request.files['image']
    userID = flask.request.form['user_ID']
    upload_to_firebase(imagefile, filename, userID)

将图像存储到 Firebase 存储的函数。

def upload_to_firebase(file, filename, userID):
    firebase = Firebase(config)
    firebase = pyrebase.initialize_app(config)
    storage = firebase.storage()
    storage.child(userID + "/" + filename + "/Processed_" + filename+"").put(file)
    downloadURL = storage.child(userID + "/" + filename + "/Processed_" + filename+"").get_url(None) 

有什么方法可以在发送图像时传递内容类型 image/jpeg 或任何其他可以解决此问题的方法。我已经搜索了很多这个解决方案,但到目前为止没有一个有效。

【问题讨论】:

    标签: python android firebase google-cloud-storage


    【解决方案1】:

    请注意,正如Firebase documentation 中所述:

    您可以将 Admin SDK 返回的存储桶引用与官方 Google Cloud Storage 客户端库结合使用,以上传、下载和修改与您的 Firebase 项目关联的存储桶中的内容

    因此,您需要将您的 upload_to_firebase 函数修改为类似于此处在 Google Cloud Storage client library for Python 的相关部分中解释的内容:

    from google.cloud import storage
    
    
    def upload_blob(bucket_name, source_file_name, destination_blob_name):
        """Uploads a file to the bucket."""
        # bucket_name = "your-bucket-name"
        # source_file_name = "local/path/to/file"
        # destination_blob_name = "storage-object-name"
    
        storage_client = storage.Client()
        bucket = storage_client.bucket(bucket_name)
        blob = bucket.blob(destination_blob_name)
    
        blob.upload_from_filename(source_file_name)
    
        print(
            "File {} uploaded to {}.".format(
                source_file_name, destination_blob_name
            )
        )
    
    

    您可以从您使用 Firebase Admin SDK 定义的存储桶对象的 name property 获取 bucket_name 变量(例如,如果您是项目的 using the default bucket):

    import firebase_admin
    from firebase_admin import credentials
    from firebase_admin import storage
    
    cred = credentials.Certificate('path/to/serviceAccountKey.json')
    firebase_admin.initialize_app(cred, {
        'storageBucket': '<BUCKET_NAME>.appspot.com'
    })
    
    bucket = storage.bucket()
    

    source_file_name 将对应于为您的 Flask 应用程序提供服务的服务器中上传图像的完整路径。

    请注意,如果不正确删除或管理上传到 Python 服务器的文件,您最终可能会遇到磁盘空间问题,因此在这方面要小心。

    【讨论】:

    • 感谢丹尼尔的帮助。我实现了此代码,并且我的图像存储在所需路径的 firebase 存储中,但只有我首先保存在设备上的图像。如何跳过文件路径部分并直接传递从客户端获取的图像,这样我就不必保存和加载图像。
    • 添加到第一条评论。当我尝试在 firebase 控制台中打开存储的图像时,会出现一个加载器,但它不显示图像。内容类型等属性的其余部分存储良好。
    • 另外,我怎样才能获取图像 url 而不是从 firebase 存储中下载图像?
    • 我发现这个code(还没有测试过)你可以适应你的用例(它使用upload_from_string()而不是upload_from_filename()方法。要获取URL,请利用Blob 的 public_url 属性。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-18
    • 2015-03-02
    • 2020-07-10
    • 2021-07-28
    • 2021-09-10
    • 2019-03-20
    相关资源
    最近更新 更多