【问题标题】:try find a way to upload a blob to firebase and edit it with cloud functions尝试找到一种将 blob 上传到 firebase 并使用云功能对其进行编辑的方法
【发布时间】:2021-08-04 04:28:27
【问题描述】:

几天来,我一直在尝试通过 firebase 函数将文件上传到 firestorage,但仍未成功。 这是我来的最远的:

export const tester = functions.https.onRequest(async (request, response) => {
  await STORAGE.bucket("[url]").upload("dir", {
    destination: "dest",
  });

  response.send(STORAGE);
});

但还是不行,因为我要上传blob

而且我之前已经在git中尝试过google的例子,但是我的typescript有问题

function uploadRef() {
  // [START storage_upload_ref]
  // Create a root reference
  var storageRef = firebase.storage().ref();

  // Create a reference to 'mountains.jpg'
  var mountainsRef = storageRef.child('mountains.jpg');

  // Create a reference to 'images/mountains.jpg'
  var mountainImagesRef = storageRef.child('images/mountains.jpg');

  // While the file names are the same, the references point to different files
  mountainsRef.name === mountainImagesRef.name;           // true
  mountainsRef.fullPath === mountainImagesRef.fullPath;   // false 
  // [END storage_upload_ref]
}

有什么帮助吗?

【问题讨论】:

  • 您可以添加您要执行的步骤列表吗?你将什么数据传递给函数?是multipart/form-data 请求吗?

标签: typescript firebase google-cloud-functions


【解决方案1】:

如果您从 Firebase 函数上传,该文件可以位于函数实例中的其他位置,它可以位于函数目录 (/workspace) 或 /tmp。您只能将文件保存在/tmp

确保pathString 是目录和文件名的组合。例如:

const path = require('path')
const admin = require("firebase-admin")

admin.initializeApp({
    storageBucket: 'my-firebase-proj.appspot.com'
});
const bucket = admin.storage().bucket();

...

const filePath = path.join(__dirname, 'filename'); //assuming the file is at /workspace
await bucket.upload(filePath, {
  destination: "dest",
});

更新:或者如果您正在流式传输文件,请考虑使用createWriteStream()

【讨论】:

  • 它不需要在文件系统上,您可以将请求体流直接通过管道传输到存储。
猜你喜欢
  • 2017-12-21
  • 1970-01-01
  • 1970-01-01
  • 2021-03-07
  • 2021-10-28
  • 2018-01-03
  • 1970-01-01
  • 2019-06-23
  • 2021-04-21
相关资源
最近更新 更多