【问题标题】:How to change the path of image in Node.js with Firebase storage?如何使用 Firebase 存储更改 Node.js 中图像的路径?
【发布时间】:2021-01-25 16:24:02
【问题描述】:

我有一个新客户提出帐户请求的系统。他们创建个人资料并在临时路径中上传个人资料图片。

当我手动接受请求时,我想通过云功能将文件从requests/{requestID}/profileImage.jpg 移动到userFiles/{userID}/profileImage.jpg

我搜索了一下,似乎无法剪切和粘贴文件。但是,我想可以复制图像并删除旧图像。

我知道如何删除文件,但我不知道如何获取临时文件并将其复制到新的目的地。你有什么想法吗?

  • 来自 URL?
  • 是否必须将图像 URL 转换为 base64 才能重新上传?

【问题讨论】:

    标签: javascript node.js firebase google-cloud-functions google-cloud-storage


    【解决方案1】:

    我的错,这是可能的,而且很容易。这是文档https://cloud.google.com/storage/docs/copying-renaming-moving-objects#storage-rename-object-nodejs,代码如下:

    async function moveFile(oldEntirePath, newEntirePath) {
      var bucket = admin.storage().bucket();
    
      return bucket.file(oldEntirePath).move(newEntirePath)
      .then(resp => {
    
        let bucketPart = resp[1].resource.bucket
        let namePart = resp[0].id
        let tokenPart = resp[1].resource.metadata.firebaseStorageDownloadTokens
    
        const url = "https://firebasestorage.googleapis.com/v0/b/"+ bucketPart +"/o/"+ namePart +"?alt=media&token=" + tokenPart
        console.log(`Image url = ${url}`)
        return url
    
      })
      .catch(err => {
          console.log(`Unable to upload image ${err}`)
      })
    }
    

    【讨论】:

      猜你喜欢
      • 2012-03-30
      • 1970-01-01
      • 2020-12-09
      • 2020-09-25
      • 2019-03-10
      • 1970-01-01
      • 2021-07-18
      • 1970-01-01
      相关资源
      最近更新 更多