firebase - Firebase 云函数存储桶上传没有这样的文件或目录
问题描述
我正在编写一个云函数,每次将文件上传到我的默认 Firebase 存储桶时触发,如果它是图像,它会将其转换为 JPG。这是官方的火力基地示例:
exports.processTripImage = functions.storage.object().onFinalize((object) => {
const filePath = object.name;
const baseFileName = path.basename(filePath, path.extname(filePath));
const fileDir = path.dirname(filePath);
const JPEGFilePath = path.normalize(path.format({ dir: fileDir, name: baseFileName, ext: JPEG_EXTENSION }));
const tempLocalFile = path.join(os.tmpdir(), filePath);
const tempLocalDir = path.dirname(tempLocalFile);
const tempLocalJPEGFile = path.join(os.tmpdir(), JPEGFilePath);
// Exit if this is triggered on a file that is not an image.
if (!object.contentType.startsWith('image/')) {
console.log('This is not an image.');
return null;
}
// Exit if the image is already a JPEG.
if (object.contentType.startsWith('image/jpeg')) {
console.log('Already a JPEG.');
return null;
}
const bucket = gcs.bucket(object.bucket);
// Create the temp directory where the storage file will be downloaded.
return mkdirp(tempLocalDir).then(() => {
// Download file from bucket.
return bucket.file(filePath).download({ destination: tempLocalFile });
}).then(() => {
console.log('The file has been downloaded to', tempLocalFile);
// Convert the image to JPEG using ImageMagick.
return spawn('convert', [tempLocalFile, tempLocalJPEGFile]);
}).then(() => {
console.log('JPEG image created at', tempLocalJPEGFile);
// Uploading the JPEG image.
return bucket.upload(tempLocalJPEGFile, { destination: JPEGFilePath });
}).then(() => {
console.log('JPEG image uploaded to Storage at', JPEGFilePath);
// Once the image has been converted delete the local files to free up disk space.
fs.unlinkSync(tempLocalJPEGFile);
fs.unlinkSync(tempLocalFile);
return;
});
})
问题是它写入文件并正确转换它,但是当它尝试将它上传回存储桶时,它无法在它刚刚创建的 tmp 目录中找到该文件。我的云功能日志:
解决方案
推荐阅读
- r - 如何解决 R 上的重复行名错误
- azure - Azure 资源标记更改历史记录/还原已删除的标记
- docker - Statefulset 在本地 minikube 中不起作用。它给出,来自服务器的错误(BadRequest):pod mysql-0 没有分配主机
- android - 在没有安装 PDF 直接软件的情况下在 Android 中以编程方式打印 ZPL 文件
- java - 为什么 Spring "ForwardedHeaderFilter" 和 "RelativeRedirectFilter" 使用代码 303 而不是 302 重定向?
- java - 如何遍历 JPA 响应并从中获取值
- css - 定位 Tailwind CSS 文件
- node.js - 使用 Flutter 和 nodejs 实现 socket.io
- amazon-web-services - 如何为大量数据更新 AWS S3 对象的 ACL 访问?
- python - QRCode 存储的信息不使用 OpenCV 解码