[英]Get Download URL from file uploaded with Cloud Functions for Firebase
[英]Firebase google cloud functions - Download File - Not Found
我正在尝试做拇指生成器示例(上传时生成图像缩略图)。 此示例是来自先前版本 API 的适配器。
const functions = require('firebase-functions');
const { Storage } = require('@google-cloud/storage');
const os = require('os');
const path = require('path');
const sharp = require('sharp');
const fs = require('fs-extra');
exports.generateThumbs = functions.storage
.object()
.onFinalize(async (object) => {
const storage = new Storage();
const bucket = await storage.bucket(object.name);
const filePath = object.name;
const fileName = filePath.split('/').pop();
const bucketDir = path.dirname(filePath);
const workingDir = path.join(os.tmpdir(), 'thumbs');
const tmpFilePath = path.join(workingDir, 'source.png');
if (fileName.includes('thumb@') || !object.contentType.includes('image')) {
console.log('exiting function');
return false;
}
// 1. Ensure thumbnail dir exists
await fs.ensureDir(workingDir);
// 2. Download Source File
const file = await bucket.file(filePath);
await file.download({
destination: tmpFilePath
});
// 3. Resize the images and define an array of upload promises
const sizes = [64, 128, 256];
const uploadPromises = sizes.map(async size => {
const thumbName = `thumb@${size}_${fileName}`;
const thumbPath = path.join(workingDir, thumbName);
// Resize source image
await sharp(tmpFilePath)
.resize(size, size)
.toFile(thumbPath);
// Upload to GCS
return bucket.upload(thumbPath, {
destination: path.join(bucketDir, thumbName)
});
});
// 4. Run the upload operations
await Promise.all(uploadPromises);
// 5. Cleanup remove the tmp/thumbs from the filesystem
return fs.remove(workingDir);
});
但我收到以下错误:
Error: Not Found
at new ApiError (/srv/node_modules/@google-cloud/common/build/src/util.js:58:28)
at Util.parseHttpRespMessage (/srv/node_modules/@google-cloud/common/build/src/util.js:159:41)
at Util.handleResp (/srv/node_modules/@google-cloud/common/build/src/util.js:136:74)
at Duplexify.requestStream.on.on.res (/srv/node_modules/@google-cloud/storage/build/src/file.js:392:31)
at emitOne (events.js:116:13)
at Duplexify.emit (events.js:211:7)
at emitOne (events.js:116:13)
at DestroyableTransform.emit (events.js:211:7)
at onResponse (/srv/node_modules/retry-request/index.js:194:19)
at Request.<anonymous> (/srv/node_modules/retry-request/index.js:149:11)
在bucket.file(...).download()
API 2.XX 引入了一些更改,我似乎无法完成这项工作。 谁能帮我一把? 谢谢你。
得到它与以下代码的工作:
const functions = require('firebase-functions');
const { Storage } = require('@google-cloud/storage');
const os = require('os');
const path = require('path');
const sharp = require('sharp');
const fs = require('fs-extra');
exports.generateThumbs = functions.storage
.object()
.onFinalize(async (object) => {
const storage = new Storage();
const bucket = storage.bucket(object.bucket);
const filePath = object.name;
const fileName = filePath.split('/').pop();
const bucketDir = path.dirname(filePath);
const workingDir = path.join(os.tmpdir(), 'thumbs');
const tmpFilePath = path.join(workingDir, 'source.png');
if (fileName.includes('thumb@') || !object.contentType.includes('image')) {
console.log('exiting function');
return false;
}
// 1. Ensure thumbnail dir exists
await fs.ensureDir(workingDir);
// 2. Download Source File
await bucket.file(filePath).download({
destination: tmpFilePath
});
// 3. Resize the images and define an array of upload promises
const sizes = [64, 128, 256];
const uploadPromises = sizes.map(async size => {
const thumbName = `thumb@${size}_${fileName}`;
const thumbPath = path.join(workingDir, thumbName);
// Resize source image
await sharp(tmpFilePath)
.resize(size, size)
.toFile(thumbPath);
// Upload to GCS
return bucket.upload(thumbPath, {
destination: path.join(bucketDir, thumbName)
});
});
// 4. Run the upload operations
await Promise.all(uploadPromises);
// 5. Cleanup remove the tmp/thumbs from the filesystem
return fs.remove(workingDir);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.