Upload Video to GCP Cloud Storage in Nodejs

0 votes

I am trying to upload a video from my flutter client to my Node API. I am sending the file from the client as a stream which is being received by my server as an application/octet-stream.

How do I translate the octet-stream into .mp4 format on my server?

When I was just doing a multipart/form-data upload I had no problem uploading to GCP Storage in .mp4 format. But since the videos can get quick large I wanted to send it in chunks to my API.

Here is my current implementation:

  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');
  });

Update / Revision

req.pipe(blobStream); did not work because I was piping the entire request body into CGS which was causing the issue.

I now have

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
          })

Now that I am receiving buffer chunks from the client, I am still unable to figure out how to stream those chunks into GCS to get one continuous file upload.

Apr 11, 2022 in GCP by Rahul
• 3,380 points
1,678 views

1 answer to this question.

0 votes

I hope this answers your question.

The problem you were facing was due to the fact that the multiparty module was creating content-type: octet stream headers on the part object that I was passing into the pipe to GCS. This caused GCS to receive two content-types of which the octet part was last. As a result GCS was using this for the inbound file.

If you need to know more about Cloud Computing, We recommend joining Cloud Computing Certification today.

answered Apr 11, 2022 by Korak
• 5,820 points

Related Questions In GCP

0 votes
1 answer

How to create a cloud storage bucket in GCP?

Buckets are the basic containers that hold your ...READ MORE

answered Oct 24, 2019 in GCP by Sirajul
• 59,230 points
2,664 views
+2 votes
2 answers

How to download multiple files in Google Cloud Storage?

You can achieve this through the gsutil ...READ MORE

answered May 9, 2018 in GCP by kurt_cobain
• 9,350 points
23,538 views
+1 vote
1 answer
0 votes
1 answer
0 votes
1 answer

Can I give a user access to cloud storage who doesn't have a GCP account?

Yes, you could certainly do this.  You can ...READ MORE

answered Nov 5, 2019 in GCP by Sirajul
• 59,230 points
992 views
0 votes
1 answer
0 votes
1 answer

Google cloud: How to list all service-accounts from all Projects in GCP

Your code sample suggests you want the ...READ MORE

answered Mar 14, 2022 in GCP by Korak
• 5,820 points
4,062 views
0 votes
1 answer

Google Cloud Platform: Logging in to GCP from command line

You have a couple of options here ...READ MORE

answered Apr 6, 2022 in GCP by Korak
• 5,820 points
1,370 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP