首页 > 解决方案 > 在 Nodejs 中将视频上传到 GCP 云存储

问题描述

我正在尝试将视频从我的颤振客户端上传到我的节点 API。我将来自客户端的文件作为流发送,我的服务器将其作为application/octet-stream.

如何在我的服务器上将八位字节流转换为 .mp4 格式?

当我刚刚multipart/form-data上传时,以 .mp4 格式上传到 GCP 存储没有问题。但是由于视频会很快变大,所以我想将其分块发送到我的 API。

这是我当前的实现:

  const newFileName = 'newFile';
  const bucket = storage.bucket('dev.appspot.com');
  const blob = bucket.file(newFileName);
  const blobStream = blob.createWriteStream({
    contentType: 'video/mp4',
    metadata: {
      contentType: 'video/mp4',
    },
  });
  blobStream.on('error', (err) => console.log(err));
  blobStream.on('finish', () => {
    console.log('finished');
  });

  req.pipe(blobStream);
  req.on('end', () => {
    console.log('upload complete');
  });

更新/修订

req.pipe(blobStream);没有工作,因为我将整个请求正文通过管道传输到导致问题的 CGS 中。

我现在有

const newFileName = 'newFile';
          const bucket = storage.bucket('dev.appspot.com/postVideos');
          const blob = bucket.file(newFileName);
          const blobStream = blob.createWriteStream({
            metadata: { contentType: 'video/mp4' },
          });
          blobStream.on('error', (err) => {
            reject(err);
          });
          blobStream.on('finish', () => {
            resolve();
          });

          req.on('data', function(chunks) {
            // this is recieving video from the client in buffer chunks, but I have not figured out how to pipe those chunks into GCS as one file
          })

现在我正在从客户端接收缓冲区块,我仍然无法弄清楚如何将这些块流式传输到 GCS 以获取一个连续的文件上传。

标签: node.jsgoogle-cloud-platformgoogle-cloud-storagemp4

解决方案


推荐阅读