使用Firebase Storage Admin SDK并顺序上传文件(然后在每次成功上传后生成签名的URL)时,某些上传会失败(失败率是任意的,例如5k个文件中有5-10个失败):>
Error: Cannot parse JSON response
at Util.parseHttpRespBody (C:\Git\MyStore\my-store-firebase\node_modules\@google-cloud\common\build\src\util.js:185:42)
at Util.handleResp (C:\Git\MyStore\my-store-firebase\node_modules\@google-cloud\common\build\src\util.js:134:117)
at retryRequest (C:\Git\MyStore\my-store-firebase\node_modules\@google-cloud\common\build\src\util.js:422:22)
at onResponse (C:\Git\MyStore\my-store-firebase\node_modules\retry-request\index.js:200:7)
at C:\Git\MyStore\my-store-firebase\node_modules\teeny-request\build\src\index.js:158:17
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:189:7)
在使用Firebase Blaze帐户并在本地服务帐户初始化的Node.js实例上运行Admin SDK时,会发生这种情况。
[{
"imgUri": "img/1.png"
},
{
"imgUri": "img/2.jpg"
},
{
"imgUri": "img/3.jpg"
},
{
"imgUri": "img/1.png"
}...]
const bucket = admin.storage().bucket('*bucket_name*');
async function uploadProductImages(productData) {
var results = [];
for (var product of productData) {
results.push(await uploadProductImage(vendorId, product.imgUri));
}
return results;
}
async function uploadProductImage(filePath) {
try {
let fileRelativePath = filePath;
let uploadOptions = {
predefinedAcl: 'publicRead',
destination: Date.now()+'_'+'*image-destination*'+'*file_name(DOT)file_extension*'
};
var fileData = await bucket.upload(fileRelativePath, uploadOptions);
return await getFileUrl(file);
} catch (err) {
//This is where the error that I pasted, gets thrown
console.error('uploadProductImage | ' + err.stack);
}
}
async function getFileUrl(file){
var url = (await file.getSignedUrl({
action: 'read',
expires: '01-01-2020'
}))[0];
return url;
}
减小文件大小:
记录已使用的内存以排除任何内存泄漏,但是在rss,heapTotal,heapUsed或外部内存上从未超过100 MB
const used = process.memoryUsage();
for (let key in used) {
console.log(`${key} ${Math.round(used[key] / 1024 / 1024 * 100) / 100} MB`);
}
--max-old-space-size=8192
我想知道是否有一种方法可以将大量图像一致地上传到Firebase Storage。