Node.js 云函数:未加载已调整大小的图像
我在上传时通过云功能调整图像大小时遇到了两个我无法解决的主要问题: 1) 如果上载了PNG,它会生成大小正确的缩略图,但它们的预览不会加载到Firestorage中(加载微调器会无限期显示)。它仅在我单击“生成新的访问令牌”后显示图像(生成的缩略图最初都没有访问令牌) 2) 如果上载JPEG或任何其他格式,MIME类型将显示为“应用程序/八位字节流”。我不知道如何正确地提取扩展名以放入新生成的缩略图的文件名中Node.js 云函数:未加载已调整大小的图像,node.js,google-cloud-functions,firebase-storage,Node.js,Google Cloud Functions,Firebase Storage,我在上传时通过云功能调整图像大小时遇到了两个我无法解决的主要问题: 1) 如果上载了PNG,它会生成大小正确的缩略图,但它们的预览不会加载到Firestorage中(加载微调器会无限期显示)。它仅在我单击“生成新的访问令牌”后显示图像(生成的缩略图最初都没有访问令牌) 2) 如果上载JPEG或任何其他格式,MIME类型将显示为“应用程序/八位字节流”。我不知道如何正确地提取扩展名以放入新生成的缩略图的文件名中 export const generateThumbs = functions.sto
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);
});
如有任何反馈,将不胜感激 我自己刚开始使用这个扩展。我注意到,在单击“创建访问令牌”之前,我无法从firebase控制台访问图像预览 我猜在图像可用之前,您必须通过编程创建此令牌
我希望它能有所帮助我也遇到了同样的问题,不知为什么Firebase的Resize Images on故意从已调整大小的图像中删除下载令牌 禁用删除下载访问令牌的步骤
- 后藤
- 从左侧选择云功能
- 选择外部存储调整图像大小生成器调整图像大小
- 单击编辑
- 从内联编辑器转到文件FUNCTIONS/LIB/INDEX.JS
- 在此行之前添加//(删除metadata.metadata.firebaseStorageDownloadTokens;)
- 对该文件中的同一行也进行注释FUNCTIONS/SRC/INDEX.TS
- 按展开并等待完成
注意:原始和调整大小的标记相同。2020年11月 关于@someone answer,我在中似乎找不到ext storage resize images generateResizedImage 更好的方法是重用原始文件的
firebaseStorageDownloadTokens
我就是这样做的
functions
.storage
.object()
.onFinalize((object) => {
// some image optimization code here
// get the original file access token
const downloadtoken = object.metadata?.firebaseStorageDownloadTokens;
return bucket.upload(tempLocalFile, {
destination: file,
metadata: {
metadata: {
optimized: true, // other custom flags
firebaseStorageDownloadTokens: downloadtoken, // access token
}
});
});
那很有效,谢谢!真想知道为什么他们会删除这个标记…这是我喜欢看到的答案类型!这对我也有用,谢谢!该行已在0.1.7版扩展中删除。如果您只需更新到该版本(或更高版本),您的问题将得到解决:)@RosárioPereiraFernandes很高兴知道他们最终解决了它。