Uploading large data more then 1 GB in google cloud storage with node.js

Viewed 61

I uploaded files less than 100MB & it took 1-2 minutes, now I'm trying to upload huge files, like more than 1GB. I tried this way but after 4-5 minutes it give me an error (Like: Fail to fetch....)

Is there any way to upload it? Please help!

Uploading approach

const multer = Multer({
  storage: Multer.memoryStorage(),
});

 app.post("/api/upload/", multer.single("file"),
    async function (req, res, next) {
      try {
        if (!req.file) {
          res.status(400).json({
            success: false,
            message: "File is not sent",
          });
          return;
        }

        // Create a new blob in the bucket and upload the file data.
        const blob = bucket.file(req.file.originalname);
        const blobStream = blob.createWriteStream({
         // metadata: {
         //   contentType: "video/mp4",
         // },
          gzip: true,
          resumable: true,
        });

        blobStream.on("error", (err) => {
          console.log(err.message);
          console.log(err.name);
          next(err);
        });

        blobStream.on("finish", async () => {
          // The public URL can access the file via HTTP directly.

          const publicUrl = format(
            `https://storage.googleapis.com/${bucket.name}/${blob.name}`
          );
       
          res.status(201).json({
            success: true,
            message: "Upload success",
          });
        });

        blobStream.end(req.file.buffer);
      } catch (error: any) {
        return ThrowError(error);
      }
    }
  );

app.use(express.json({ limit: "5000mb" }));

app.use(express.urlencoded({ extended: true, limit: "5000mb" }));

1 Answers

An alternative architecture could be for your app to generate a signed URL and for your client to upload the data directly to GCS.

Related