上传文件到云存储时如何设置元数据属性

flu*_*ter 2 node.js google-cloud-storage

我正在使用云函数调整文件大小并将文件上传到 Firebase 函数。我想设置content-type为。图像/jpeg。我正在使用谷歌云存储来返回一个存储桶..

const bucket = gcs.bucket(object.bucket);

当我上传文件时..

bucket.upload(imgPath, {
                destination: join(bucketDir, newImgName),
                metadata: {
                    metadata: {
                        contentType: 'image/jpeg',
                        firebaseStorageDownloadTokens: uuid
                    }
                }

            });
Run Code Online (Sandbox Code Playgroud)

我能够访问上传到 Firebase Storage 的图像的文件类型,即application/octet-stream,这会触发操作。这也是调整大小的文件的内容类型。如何将内容类型设置为“image/jpeg”?

Dou*_*son 5

作为第二个参数传递的UploadOptions对象嵌套得太深。您仅将内部metadata属性用于自定义元数据。您可以contentType根据链接的文档在顶层进行设置。

bucket.upload(imgPath, {
    destination: join(bucketDir, newImgName),
    metadata: {
        contentType: 'image/jpeg',
    }
});
Run Code Online (Sandbox Code Playgroud)

我不知道firebaseStorageDownloadTokens是什么,所以我把它留下了。文档中似乎没有提到。如果您尝试将其设置为自定义元数据,请使用嵌套元数据属性。