6

We keep getting ECONNRESET error when trying to upload images to google cloud storage.

var storage = require('gcloud').storage({
    projectId: projectNumber,
    credentials: credentials
});

//...

var file = Storage.bucket('myBucket').file('test.png');
fs.createReadStream('/path/to/image.png').pipe(file.createWriteStream({
    metadata: {
        contentType: 'image/png',
        cacheControl: 'public, max-age=2592000'
    }
}).on('complete', function () {
  //
}).on('error', function (err) {
  // err = Error: socket hang up || Request Timeout after 30000ms
});
Wilson
  • 9,006
  • 3
  • 42
  • 46
Guy Korland
  • 9,139
  • 14
  • 59
  • 106

2 Answers2

5

Not sure why no one has answered this question. I have the same error, and resolve it by setting resumable and validation to false:

var gcsStream = Storage.bucket('myBucket').file('test.png').createWriteStream({
      resumable: false,
      validation: false,
      metadata: {
        contentType: 'image/png'
      }
    });
fs.createReadStream('/path/to/image.png').pipe(gcsStream);

Without the 2 settings, I get Socket hangup error whenever I upload 20~30 images at the same time.

Kan Nguyen
  • 199
  • 1
  • 4
2

Not sure if this is still relevant but I was facing the same issue. However for me what ended up causing the error was these two things:

const [exists] = await file.exists();

And

const writeStream = file.createWriteStream();
fetch(url)
    .then(res => {
        res.body.pipe(writeStream);
        res.body.on('error', err => {
            reject(err);
        });
        writeStream.on('finish', async () => {
            resolve();
        });
    })
    .catch(console.error);

It is important to note that there is (in my case) nothing wrong with these things but the problem lied with the amount of requests we where doing over a short amount of time. We where pushing 200 to 600 new images to google at once.

What worked for us was so simply add a setTimeout() with something like 40 * index as timer. This worked for us as this was an action that didn't need to be fast or something. It just had to work.

servinlp
  • 715
  • 1
  • 8
  • 17