我正在构建一个将图像上传到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)
答案 0 :(得分:0)
正如“ Klaus D.”的评论所说,解决方案是实现“ uuid”模块
import uuid
.....
.....
blob = bucket.blob("images/" + str(uuid.uuid4()))