简体   繁体   English

如何使用 Node.js 将 Blob base64 字符串上传到谷歌云存储

[英]How do you upload a blob base64 string to google cloud storage using Node.js

I'm building an application that will allow me to take a picture from my react app which accesses the web cam, then I need to upload the image to google cloud storage using a Hapi node.js server.我正在构建一个应用程序,允许我从访问 web cam 的 react 应用程序中拍照,然后我需要使用 Hapi node.js 服务器将图像上传到谷歌云存储。 The problem I'm encountering is that the react app snaps a picture and gives me this blob string (I actually don't even know if that's what it's called) But the string is very large and looks like this (I've shortened it due to it's really large size:我遇到的问题是 react 应用程序拍摄了一张图片并给了我这个 blob 字符串(我实际上什至不知道这是否就是它的名字)但是字符串非常大并且看起来像这样(我已经缩短了它由于它的尺寸非常大:

"imageBlob": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/...

I'm finding it hard to find resources that show me how to do this exactly, I need to upload that blob file and save it to a google cloud storage bucket.我发现很难找到向我展示如何准确执行此操作的资源,我需要上传该 blob 文件并将其保存到谷歌云存储桶中。

I have this in my app so-far:到目前为止,我的应用程序中有这个:

Item.postImageToStorage = async (request, h) => {
  const image = request.payload.imageBlob;
  const projectId = 'my-project-id'
  const keyFilename = 'path-to-my-file'
  const gc = new Storage({
    projectId: projectId,
    keyFilename: keyFilename
  })

  const bucket = gc.bucket('my-bucket.appspot.com/securityCam');
  const blob = bucket.file(image);
  const blobStream = blob.createWriteStream();
  blobStream.on('error', err => {
    h.response({
      success: false,
      error: err.message || '=-->' + err
    })
  });
  console.log('===---> ', 'no errors::::')

  blobStream.on('finish', () => {
    console.log('done::::::', `https://storage.googleapis.com/${bucket.name}/${blob.name}`)
    // The public URL can be used to directly access the file via HTTP.
    const publicUrl = format(
      `https://storage.googleapis.com/${bucket.name}/${blob.name}`
    );
  });
  console.log('===---> ', 'past finish::::')
  blobStream.end(image);
  console.log('===---> ', 'at end::::')
  return h.response({
    success: true,
  })
  // Utils.postRequestor(path, payload, headers, timeout)
}

I ge to the success message/response h.response but no console logs appear except the ones outside of the blobStream.on I see all that start with ===---> but nothing else.我看到成功消息/响应h.response但没有控制台日志出现,除了 blobStream.on 之外的日志我看到所有以 ===---> 开头的东西,但没有别的。

Not sure what I'm doing wrong, thanks in advance!不知道我做错了什么,在此先感谢!

At the highest level, let us assume you want to write into file my-file.dat that is to live in bucket my-bucket/my-folder .在最高级别,让我们假设您要写入文件my-file.dat ,该文件位于存储桶my-bucket/my-folder中。 Let us assume that the data you want to write is a binary chunk of data that is stored in a JavaScript Buffer object referenced by a variable called my_data .让我们假设您要写入的数据是存储在 JavaScript 缓冲区 object 中的二进制数据块,该缓冲区由名为my_data的变量引用。 We would then want to code something similar to:然后,我们想要编写类似于以下内容的代码:

const bucket = gc.bucket('my-bucket/my-folder');
const my_file = bucket.file('my-file.dat');      
const my_stream = my_file.createWriteStream();
my_stream.write(my_data);
my_stream.end();

In your example, something looks fishy with the value you are passing in as the file name in the line:在您的示例中,您作为文件名传入的值看起来很可疑

const blob = bucket.file(image);

I'm almost imagining you are thinking you are passing in the content of the file rather than the name of the file.我几乎在想象您正在认为您正在传递文件的内容而不是文件的名称。

Also realize that your JavaScript object field called "imageBlob" will be a String.还要意识到名为“imageBlob”的 JavaScript object 字段将是一个字符串。 It may be that it indeed what you want to save but I can also imagine that what you want to save is binary data corresponding to your webcam image.可能确实是您要保存的内容,但我也可以想象您要保存的是与网络摄像头图像相对应的二进制数据。 In which case you will have to decode the string to a binary Buffer.在这种情况下,您必须将字符串解码为二进制缓冲区。 This looks like it will be extracting the string data starting data:image/jpeg;base64, and then creating a Buffer from that by treating the string as Base64 encoded binary.这看起来将提取字符串数据起始data:image/jpeg;base64,然后通过将字符串视为 Base64 编码二进制文件来创建缓冲区。

Edit: fixed typo编辑:修正错字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM