因此我的Firestore集合用户中存在一些错误记录:image字段(错误路径:users / uid / name.jpg,预期输出:
x = 'This is a nice day'
result = [i.strip() for i in x.split(' ' * 3)]
print(result)
['This is', 'a nice day']
) -存储器中的图像另存为:
https://firebasestorage.googleapis.com/v0/b/project-id.appspot.com/o/users%2F4yWeML3ktpVt6oWowtEhg8oJx1I3%2Fcropped828392415291985722.jpg?alt=media&token=6daeeab9-6b34-4a27-a4d5-0789a7d773c3
所以我写了一个云函数来从存储桶“用户”中获取所有文件
users > user_id > cropped{fileName}.jpg
我部署了此功能,然后使用firebase模拟器在本地测试,所有软件包都更新为package.json中的最新版本:
import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
import { Storage } from "@google-cloud/storage";
admin.initializeApp({
credential: admin.credential.cert({
projectId: "myProjectId",
clientEmail:
"firebase-adminsdk-myAdmin@project-id.iam.gserviceaccount.com",
privateKey:
"-----BEGIN PRIVATE KEY-----\nmyprivatekey=\n-----END PRIVATE KEY-----\n"
}),
storageBucket: "myProject.appspot.com"
});
const db = admin.firestore();
const storage = new Storage({
projectId: "myProjectId",
credentials: {
client_email: "firebase-adminsdk-myAdmin@project-id.iam.gserviceaccount.com",
private_key: "-----BEGIN PRIVATE KEY-----\nMyPrivateKey=\n-----END PRIVATE KEY-----\n"
}
});
export const updateUserImage = functions.https.onRequest((request, response) => {
const wrongData: any = [];
db.collection("users")
.where("image", ">=", "/users")
.orderBy("image", "asc")
.get()
.then(res => {
res.forEach(ele => {
if (ele.data().hasOwnProperty("image")) {
console.log(ele.data().image.includes("/firebasestorage"));
if (!ele.data().image.includes("/firebasestorage")) {
wrongData.push(ele);
}
}
});
console.log('Wrong docs: ' + wrongData.length);
for (const doc of wrongData) {
const getFileName = doc.data().image.split("/")[3];
const subFolder = doc.data().image.split("/")[2];
console.log(getFileName);
storage.bucket('users').file(`${subFolder}/${getFileName}`).get()
.then((fileData: any) => {
console.log(fileData);
}).catch(err => console.log(err))
}
})
.catch(err => console.log(err));
}
);
但是,当我通过以下命令进行调试时:"dependencies": {
"@google-cloud/firestore": "^2.2.6",
"@google-cloud/storage": "^3.1.0",
"@google/maps": "^0.5.5",
"@types/google__maps": "^0.5.5",
"express": "^4.17.1",
"firebase": "^6.3.5",
"firebase-admin": "^8.3.0",
"firebase-functions": "^3.2.0"
},
即使admin SDK可以获取数据,但从存储中获取文件时也会引发错误:
firebase serve --only functions
我上网浏览并设置了Google Cloud Storage的权限-该帐户的Google Cloud平台控制台,包括以下角色:
Firebase admin SDK管理代理(Google在生成json时默认)
存储管理员
所有者
存储对象管理员
然后等待5分钟,但是当我尝试在本地调用该函数时仍然出现相同的错误。配置中缺少什么吗?我对云功能和云存储有点陌生
答案 0 :(得分:0)
您的代码正在尝试访问名为“用户”的存储桶中的文件:
storage.bucket('users')
但是我100%肯定您的存储桶名称不是“用户”。存储桶名称与存储桶内显示的“文件夹”名称不同。存储桶的名称很可能与项目ID相匹配,或者是创建时创建的全局唯一名称(在所有Google Cloud的所有项目的所有存储桶中)。