简体   繁体   English

FetchError,原因:读取ECONNRESET; 将图片从 node.js 上传到谷歌云存储时

[英]FetchError, reason: read ECONNRESET; When uploading images to google cloud storage from node.js

I am using node version 13.5.0.我正在使用节点版本 13.5.0。 Google cloud storage gives an error named "FetchError, reason: read ECONNRESET" when trigger to upload.谷歌云存储在触发上传时给出一个名为“FetchError,原因:读取ECONNRESET”的错误。 I parsed the images by multer@1.4.2 with express@4.17.1.我通过 multer@1.4.2 和 express@4.17.1 解析图像。 I also created the bucket and service account correctly.我还正确创建了存储桶和服务帐户。 Here is code:这是代码:


const storage = new Storage({
     projectId: 'ultra-brand-263520',
     keyFilename: './google.json',
   });
   const bucketName = 'chimaera-img';
   const bucket = storage.bucket(bucketName);
   const gcsFileName = `${Date.now()}-${req.file.originalname}`;
   const file = bucket.file(gcsFileName);

   const stream = file.createWriteStream({
     metadata: {
       contentType: req.file.mimetype,
     },
   });

   stream.on('error', (err) => {
     req.file.cloudStorageError = err;
     // next(err);
     console.log(err)
   });

   stream.on('finish', () => {
     req.file.cloudStorageObject = gcsFileName;

     file.makePublic()
       .then(() => {
         req.file.gcsUrl = gcsHelpers.getPublicUrl(bucketName, gcsFileName);
         // next();
         console.log(req.file.gcsUrl)
       });
   });

   stream.end(req.file.buffer);

and the error is:错误是:

     at ClientRequest.<anonymous> (C:\Users\Md Tofazzal Haque\Documents\chimarea\brainiacs-chimaera-webapp\node_modules\gaxios\node_modules\node-fetch\lib\index.js:1455:11)
     at ClientRequest.emit (events.js:305:20)
     at TLSSocket.socketErrorListener (_http_client.js:423:9)
     at TLSSocket.emit (events.js:305:20)
     at emitErrorNT (internal/streams/destroy.js:84:8)
     at processTicksAndRejections (internal/process/task_queues.js:84:21) {
   message: 'request to https://storage.googleapis.com/upload/storage/v1/b/chimaera-img/o?name=1577912738314-Capture.PNG&uploadType=resumable failed, reason: read ECONNRESET',
   type: 'system',
   errno: 'ECONNRESET',
   code: 'ECONNRESET',
   config: {
     method: 'POST',
     url: 'https://storage.googleapis.com/upload/storage/v1/b/chimaera-img/o?name=1577912738314-Capture.PNG&uploadType=resumable',
     params: [Object: null prototype] {
       name: '1577912738314-Capture.PNG',
       uploadType: 'resumable'
     },
     data: { contentType: 'image/png' },
     headers: {
       'X-Upload-Content-Type': 'image/png',
       Authorization: 'Bearer ya29.c.KmK3BzZirjwc-_rTcRddTj3knZ1ujoZoJ9ez9hVaiUBitJRH1k4Qcd8GbvOHM8O8yO16EfzWUzKRtgxTEfaao_UHlj2hLxh8Wht8iJFRMm-NnFMpRdSo5Zp4DOaQTtG2v3hOGg',
       'User-Agent': 'google-api-nodejs-client/5.7.0',
       'x-goog-api-client': 'gl-node/13.5.0 auth/5.7.0',
       'Content-Type': 'application/json',
       Accept: 'application/json'
     },
     validateStatus: [Function (anonymous)],
     paramsSerializer: [Function: paramsSerializer],
     body: '{"contentType":"image/png"}',
     responseType: 'json'
   }
 }

As John Hanley pointed out you are not creating a read stream so you're not uploading any data.正如约翰汉利指出的那样,您没有创建读取流,因此您没有上传任何数据。 Furthermore the preferable way to upload a file to Cloud Storage is through the function upload() which happens to be a convenient wrapper around createWriteStream.此外,将文件上传到 Cloud Storage 的首选方法是通过函数 upload(),它恰好是围绕 createWriteStream 的便捷包装器。

I suggest you try to follow the sample code for uploading a file given in the official documentation .我建议您尝试按照官方文档中给出的示例代码上传文件 Additionally if you want to know further about the functions and how-to of this client library you might take a look at the Nodejs reference forupload and createWriteStream .此外,如果您想进一步了解此客户端库的功能和操作方法,您可以查看上传createWriteStream的 Nodejs 参考。

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

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