简体   繁体   English

使用 node.js 在谷歌云存储中上传超过 1 GB 的大数据

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

I uploaded files less than 100MB & it took 1-2 minutes, now I'm trying to upload huge files, like more than 1GB.我上传了小于 100MB 的文件,花了 1-2 分钟,现在我正在尝试上传大文件,比如超过 1GB。 I tried this way but after 4-5 minutes it give me an error (Like: Fail to fetch....)我尝试过这种方式,但在 4-5 分钟后它给了我一个错误(例如:无法获取......)

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.json({ limit: "5000mb" }));

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

An alternative architecture could be for your app to generate a signed URL and for your client to upload the data directly to GCS.另一种架构可能是让您的应用程序生成签名的 URL并让您的客户端将数据直接上传到 GCS。

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

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