Mr *_*Jax 3 node.js google-cloud-functions firebase-storage
我正在按照教程在上传时通过 Cloud Functions 调整图像大小,但遇到了两个我无法弄清楚的主要问题:
1) 如果上传了 PNG,它会生成大小正确的缩略图,但它们的预览不会在 Firestorage 中加载(加载微调器无限期显示)。它只在我点击“生成新的访问令牌”后显示图像(最初生成的缩略图都没有访问令牌)。
2) 如果上传了 JPEG 或任何其他格式,则 MIME 类型显示为“application/octet-stream”。我不确定如何正确提取扩展名以放入新生成的缩略图的文件名中?
export const generateThumbs = functions.storage
.object()
.onFinalize(async object => {
const bucket = gcs.bucket(object.bucket);
const filePath = object.name;
const fileName = filePath.split('/').pop();
const bucketDir = dirname(filePath);
const workingDir = join(tmpdir(), 'thumbs');
const tmpFilePath = 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 = join(workingDir, thumbName);
// Resize source image
await sharp(tmpFilePath)
.resize(size, size)
.toFile(thumbPath);
// Upload to GCS
return bucket.upload(thumbPath, {
destination: 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);
});
Run Code Online (Sandbox Code Playgroud)
非常感谢任何反馈!
我刚刚遇到了同样的问题,不知出于什么原因,Firebase 的调整图像大小故意从调整大小的图像中删除下载令牌
禁用删除下载访问令牌
注意:原始和调整大小将具有相同的令牌。
归档时间: |
|
查看次数: |
1116 次 |
最近记录: |