如何在Firebase Storage中将上传的图像存储到两个不同的路径/位置?我试过了,但是没用
const { currentUser } = firebase.auth();
const ref = firebase.storage().ref().child(`images/${currentUser.uid}`);
const ref = firebase.storage().ref().child('photos')
const snapshot = await ref.put(blob);
blob.close();
答案 0 :(得分:1)
好的,首先,您不能重新声明这样的变量(const ref = ...;
,然后是其下方的const ref = ....;
)。其次,您需要对每个引用执行put
。所以应该看起来像这样:
const { currentUser } = firebase.auth();
const ref1 = firebase.storage().ref().child(`images/${currentUser.uid}`);
const ref2 = firebase.storage().ref().child('photos')
const snapshot1 = await ref1.put(blob);
const snapshot2 = await ref2.put(blob);
blob.close();
或者如果您想要更多优化的代码:
const { currentUser } = firebase.auth();
const ref = firebase.storage().ref();
const imagesUpload = await ref.child(`images/${currentUser.uid}`).put(blob);
const photosUpload = await ref.child('photos').put(blob);
blob.close();
如果您希望对此进行更高级的了解并且仅执行一个上传任务,请在此处阅读更多信息:https://cloud.google.com/storage/docs/json_api/v1/how-tos/batch