Getting ESOCKETTIMEOUT, ECONNRESET or socket hang up with large file when streaming to GCP through file.createWriteStream()

Viewed 2891

While trying to upload a large file (>50MB) to Google Cloud Storage through a Google Cloud Function, I run into these exceptions depending of the options I set:

  • While settting request option 'forever: false' I get: Error: socket hang up
  • While settting request option 'resumable: true' I get: Error: write ECONNRESET
  • While settting request option 'resumable: false' I get: Error: ESOCKETTIMEDOUT with resumable: false

Here is the code I use:

function uploadFile(bucketName, filename, data) {
  console.log("Starting uploading blob...");
  const Storage = require('@google-cloud/storage');

  console.log("Creating client...");
  const storage = new Storage();
  storage.interceptors.push({
      request: function(reqOpts) {
        reqOpts.forever = false;
        return reqOpts
      }
    });

  console.log("Getting bucket " + bucketName + "...");
  const bucket = storage.bucket(bucketName);
  console.log("Creating file " + filename + "...");
  const file = bucket.file(filename);
  console.log("Creating write stream...");

  var writeStream = file.createWriteStream({
    metadata: {
      contentType: 'plain/text'
    },
    resumable: false
  });

  writeStream.on('error', function(e) { console.error("An error occurred : " + e); });
  writeStream.on('finish', function() { console.log("Success"); });

  console.log("Initializing Streaming...");
  var bufferStream = new stream.PassThrough();
  bufferStream.end(data);
  bufferStream.pipe(writeStream);
}

Is there something I'm missing?

1 Answers
Related