简体   繁体   English

Multer 无法在 node.js typescript 上将文件上传到 GCP

[英]Multer could not upload file to GCP on node.js typescript

I am trying to upload a single file using Multer with "multipart/form-data" content type to a bucket of google cloud storage.我正在尝试使用具有"multipart/form-data"内容类型的 Multer 将单个文件上传到谷歌云存储桶。

I am using "Multer.memoryStorage()" and "@google-cloud/storage"我正在使用"Multer.memoryStorage()""@google-cloud/storage"

  try {
    const documentFile = (req as MulterRequest).file;
    const blob = bucket.file(documentFile.originalname);
    console.log(documentFile)
    const blobStream = blob.createWriteStream()

    blobStream.on("error", (err: any) => {
      return res.status(500).send({ message: err.message });
    });

    streamifier.createReadStream(documentFile.buffer.length)
      .on('error', (e: any) => {
        console.log("error")
        console.log(e)
        next(e);
      })
      .pipe(blobStream)
      .on('finish', (res: Response) => {
        console.log("success")
        //res.send('done')
        return res.status(200).json({
          message: 'Upload user document successfully',
          data: documentFile,
        });
      });
  } catch (e) {
    next(e);
  }
};

I received an error message of "The \"chunk\" argument must be one of type string or Buffer. Received type number" at const blobStream = blob.createWriteStream() and the file was not uploaded.我在const blobStream = blob.createWriteStream()收到一条错误消息"The \"chunk\" argument must be one of type string or Buffer. Received type number"并且文件未上传。 Whenever i upload a file, i will receive this error and i do not know where am i do wrong.每当我上传文件时,我都会收到此错误,我不知道我在哪里做错了。 Does anyone know which steps did i do wrong and hopefully can guide me through this.有谁知道我做错了哪些步骤,希望能指导我完成这个。 Thanks in advance.提前致谢。

The documentFile is containing the following details: documentFile包含以下详细信息:

{
  fieldname: 'file',
  originalname: '1.jpg',
  encoding: '7bit',
  mimetype: 'image/jpeg',
  buffer: <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 00 00 01 00 01 00 00 ff db 00 84 00 0a 0a 0a 0a 0a 0a 0b 0c 0c 0b 0f 10 0e 10 0f 16 14 13 13 14 16 22 18 1a 18 ... 19425 more bytes>,
  size: 19475
}

I was having the same problem when trying to upload, this is the way I fix it.我在尝试上传时遇到了同样的问题,这是我修复它的方式。 I use this example from nodejs-storage to solve this. 我使用 nodejs-storage 中的这个例子来解决这个问题。

const filename = `${Date.now()}-${req.file.originalname}`;
const file = bucket.file(filename);

const passthroughStream = new stream.PassThrough();
passthroughStream.write(req.file.buffer);
passthroughStream.end();

async function streamFileUpload() {
    passthroughStream.pipe(file.createWriteStream()).on('finish', () => {
        console.log('uploaded');
    });

    console.log(`${filename} uploaded to ${bucket.name}`);
}

streamFileUpload().catch(console.error);

Just write the req.file.buffer to solve the error.只需编写 req.file.buffer 即可解决错误。

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

相关问题 Node.js 从服务器上传图片文件到firebase失败 - Node.js failed to upload image file to firebase from the server 为上传到 GCP 存储桶的文件提供自定义元数据的正确方法是什么? 使用 node.js sdk - What is the right way of providing custom metadata to file uploaded to GCP Storage bucket? Using node.js sdk AWS S3 Node.js SDK Multer `NotImplemented` 错误 - AWS S3 Node.js SDK `NotImplemented` Error with Multer GCP Node.js 在单独的子文件夹中执行应用程序 - GCP Node.js execute app in separate subfolder GCP Cloud Functions:Node.JS 版本路线图 - GCP Cloud Functions: Node.JS Versions Roadmap aws sdk 使用 node.js 分段上传到 s3 - aws sdk Multipart Upload to s3 with node.js Firebase 存储 | 从 node.js 云函数上传时文件是“application/octet-stream” - Firebase Storage | File is "application/octet-stream" on upload from node.js cloud function 我们如何使用 node.js 将 m3u8 文件和相应的 ts 文件上传到 amazon s3 存储桶 - How can we upload m3u8 file and corresponding ts file to amazon s3 bucket using node.js 无法使用 Node.js 在 CloudRun 上访问 Cloud Firestore 后端 - Could not reach Cloud Firestore backend on CloudRun with Node.js 有没有办法在 node.js 上按名称搜索文件 - Is there a way to search for a file by name on node.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM