Firebase 存储 - 通过 Python google-cloud-storage lib 上传 png 图像时出错 [英] Firebase Storage - error with uploading png image via Python google-cloud-storage lib

查看:26
本文介绍了Firebase 存储 - 通过 Python google-cloud-storage lib 上传 png 图像时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行基于 Firebase 实时数据库和 Firebase 存储的网络应用.

我需要通过 Python google-cloud-storage lib 每小时将新图片上传到 Firebase google 存储桶.以下是

感谢您的帮助!

解决方案

每当您使用 Firebase 控制台上传图片时,都会自动生成一个访问令牌.但是,如果您使用任何 Admin SDK 或 gsutil 上传图像,您将需要自己手动生成此 访问令牌.

这是一个关于如何使用 Admin Python SDK 为图像生成和设置访问令牌的示例.

导入 firebase_admin从 firebase_admin 导入凭据从 firebase_admin 导入存储# 导入 UUID4 以创建令牌从 uuid 导入 uuid4cred = credentials.Certificate("path/to/your/service_account.json")default_app = firebase_admin.initialize_app(cred, {'storageBucket': '<BUCKET_NAME>.appspot.com'})桶 = storage.bucket()blob = bucket.blob(img_src)# 创建新令牌new_token = uuid4()# 使用元数据创建新字典元数据 = {firebaseStorageDownloadTokens":new_token}# 将元数据设置为 blobblob.metadata = 元数据# 上传文件blob.upload_from_filename(filename=img_path, content_type='image/png')

这里是快速解释:

  • 导入 UUID4 库以创建令牌.从 uuid 导入 uuid4
  • 创建一个新的 UUID4.new_token = uuid4()
  • 使用键值对创建一个新字典.元数据 = {"firebaseStorageDownloadTokens": new_token}
  • 将其设置为 blob 的元数据.blob.metadata = 元数据
  • 上传文件.blob.upload_from_filename(...)

此解决方案可用于任何 Admin SDK.

Firebase 支持 表示正在修复此问题,但我认为遇到此问题的任何人都应该这样做,而不是等待 Firebase 解决此问题.

I'm running web app based on Firebase Realtime Database and Firebase Storage.

I need to upload new images to Firebase google bucket every hour via Python google-cloud-storage lib. Here are the docs.

My code for image upload (img_src path is correct):

    bucket = storage.bucket()
    blob = bucket.blob(img_src)
    blob.upload_from_filename(filename=img_path, content_type='image/png')

Image seem to be uploaded successfully, but when manually viewing it in Firebase Storage, it doesn't load. All the image's specs seem to be correct. Please compare specs of manually uploaded image (loads fine) with corrupted one.

Thanks for help!

解决方案

Whenever you upload an image using Firebase Console, an access token will be automatically generated. However, if you upload an image using any Admin SDK or gsutil you will need to manually generate this access token yourself.

Here is an example on how to generate and set an access token for an image using the Admin Python SDK.

import firebase_admin
from firebase_admin import credentials
from firebase_admin import storage

# Import UUID4 to create token
from uuid import uuid4

cred = credentials.Certificate("path/to/your/service_account.json")
default_app = firebase_admin.initialize_app(cred, {
    'storageBucket': '<BUCKET_NAME>.appspot.com'
})

bucket = storage.bucket()
blob = bucket.blob(img_src)

# Create new token
new_token = uuid4()

# Create new dictionary with the metadata
metadata  = {"firebaseStorageDownloadTokens": new_token}

# Set metadata to blob
blob.metadata = metadata

# Upload file
blob.upload_from_filename(filename=img_path, content_type='image/png')

Here is quick explanation:

  • Import the UUID4 library to create a token. from uuid import uuid4
  • Create a new UUID4. new_token = uuid4()
  • Create a new dictionary with the key-value pair. metadata = {"firebaseStorageDownloadTokens": new_token}
  • Set it as the blob's metadata. blob.metadata = metadata
  • Upload the file. blob.upload_from_filename(...)

This solution can be implemented for any Admin SDK.

Firebase Support says that this is being fixed, but I think anyone having this problem should go this way instead of waiting for Firebase to fix this.

这篇关于Firebase 存储 - 通过 Python google-cloud-storage lib 上传 png 图像时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆