使用 firebase-admin 将图像上传到 Firebase 存储

开发者

当我使用firebase-admin将图像上传到我的 Firebase 项目时,我总是很困难

这是我以管理员用户身份上传图像时得到的结果,使用我的应用程序的管理部分,该部分使用常规firebaseJS 包。

这是我使用的代码:

const metadata = {
  cacheControl: "public,max-age=31536000,must-revalidate"
  // contentType: "image/jpeg" // THIS IS AUTO INFERRED BY FIREBASE
};


const storageRef = firebase.storage().ref(`${directory}/${fileName}`);
const uploadTask = storageRef.put(file,metadata);

uploadTask.on("state_changed",
  function progress() {...},
  function error() {...},
  function complete() {...}
);

这就是我在 Firebase 存储控制台上得到的:带有链接和下载令牌的漂亮预览。即使cacheControl元数据没有显示,它也被设置了,因为当我在浏览器上访问图像的 URL 时它是可见的。

在此处输入图片说明

现在我正在编写一个admin脚本来将我在本地机器上的一些图像上传到我的 Firebase 存储。

这是代码:

async function uploadImage() {

  admin.storage().bucket().upload(
    `./temp/${imageLocation}`,       // THIS IS MY LOCAL PATH
    { 
      destination: imageLocation,    // THIS IS THE PATH FOR THE STORAGE
    }
  );
  console.log(`Uploaded: ${imageLocation}`);
}

一切正常,文件确实已上传,但这是我在 Firebase 存储控制台上得到的:

在此处输入图片说明

问题

我如何使用 ,firebase-admin以获得与firebase在浏览器上使用JS 包上传图像时相同的一致结果

我可以firebase在我的 NodeJs 管理脚本中使用常规包吗?我想我必须admin在发出firebase.storage().ref().put()请求之前用户身份进行身份验证,因为所有存储路径都受allow write: if request.auth.token.admin == true;.

梅斯卡尔·卡拉维

你需要添加 uuidv4 包:

const { uuid } = require('uuidv4')

bucket.upload('cat.png', {
    destination: 'cat.png',
    metadata: {
        metadata: {
            firebaseStorageDownloadTokens: uuid(),
        }
    },
})

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用Golang将图像上传到Firebase存储

使用当前用户的UID将图像上传到Firebase存储

如何使用 Java 中的 Firebase Admin 将文件上传到 Firebase 存储?

在 Node 中使用 firebase-admin 将火上传到 Firebase 存储

从抖动将图像上传到Firebase存储

将图像从URL上传到Firebase存储

将多个图像上传到Firebase存储

将图像上传到Firebase存储

如何使用admin SDK将文件上传到带有路径的Firebase的云存储中?

在 Flutter Web 上使用 firebase_storage 4.0.0 将图像上传到 Firebase 存储?

使用Antd上传操作将图像上传到Firebase存储时出现问题

是否可以使用URL而非Android中的SDK将图像上传到Firebase存储?

无法使用Firebase函数将字节数组图像上传到火存储

将图像作为 np 数组上传到 FIrebase 存储

将图像上传到Firebase存储和数据库

如何将图像上传到Firebase存储?

无法将图像从RPI 3上传到Firebase存储

通过 POST 请求将图像上传到 Firebase 存储

如何从输入字段将图像上传到Firebase云存储?

如何检查将图像上传到Firebase存储的进度

Kotlin - 无法将图像上传到 Firebase 存储

无法将捕获的图像上传到Firebase存储

无法从抖动的网络将图像上传到Firebase存储

在Swift 4中将图像上传到Firebase存储

如何将图像从 Ionic 上传到 Firebase 存储?

将Facebook图像URL上传到Firebase存储

使用cordovaFileTransfer将视频上传到Firebase(3.0)存储

使用Node.js将文件上传到Firebase存储

使用图片URI将照片上传到Firebase存储