我正在构建一个将图像上传到Firebase存储的API,在这方面一切正常,问题在于语法使我可以在每次上传中指定文件名,并且在生产模式下,该API将接收来自多个设备的上传请求,因此我需要进行编码,以便它检查可用的id,将其设置为“ blob()”对象,然后进行正常的上传,但是我不知道该怎么做。或随机名称,我不在乎,只要它不会覆盖其他图片

这是我当前的代码:

from flask_pymongo import PyMongo
import firebase_admin
from firebase_admin import credentials, auth, storage, firestore
import os
import io


cred = credentials.Certificate('service_account_key.json')
firebase_admin.initialize_app(cred, {'storageBucket': 'MY-DATABASE-NAME.appspot.com'})

bucket = storage.bucket()
blob = bucket.blob("images/newimage.png") #here is where im guessing i #should put the next available name

# "apple.png" is a sample image #for testing in my directory
with open("apple.png", "rb") as f:
    blob.upload_from_file(f)

最佳答案

正如“ Klaus D.”的评论所说,解决方案是实现“ uuid”模块

import uuid

.....
.....
blob = bucket.blob("images/" + str(uuid.uuid4()))

关于python - (Flask API),使Firebase上传的图像具有唯一的名称,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54500252/

10-12 00:32
查看更多