Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 353
Gwendal Le Cren Avatar asked Nov 16 '17 11:11

Gwendal Le Cren


People also ask

What causes a socket hang up?

The error code [socket hang up][ECONNRESET] indicates that the target server has closed the connection with Edge Microgateway. This can be searched in the logs to determine how often it is happening.

What does Error socket hang up mean?

I think "socket hang up" is a fairly general error indicating that the connection has been terminated from the server end. In other words, the sockets being used to maintain the connection between the client and the server have been disconnected.


1 Answers

I fixed his issue by using the lib "request" (2.83.0) instead of "request-promise". Here is the simplified code I use:

const request = require('request').defaults({
    timeout: 500000,
    gzip: true,
    forever: false,
    pool: {
        maxSockets: Infinity
    }
});
const Storage = require('@google-cloud/storage');
const storage = new Storage();
storage.interceptors.push({
    request: function(reqOpts) {
        reqOpts.forever = false;
        return reqOpts
    }
});

/**
 * HTTP Cloud Function.
 *
 * @param {Object} req Cloud Function request context.
 * @param {Object} res Cloud Function response context.
 */
exports.exportToGCS = function exportToGCS(req, res) {
   var bucket = req.body.bucket;
   var fileName = req.body.fileName;

    try {
        console.log("Getting bucket " + bucket + "...");
        var bucket = storage.bucket(bucket);
        console.log("Creating file " + fileName + "...");
        const file = bucket.file(fileName);
        console.log("Creating writeStream...");
        var writeStream = createWriteStream(file);
        // Get the stream from a request to send out to GCS
        var options = createRequest();
        request
            .get(options)
            .pipe(writeStream);
        console.log("Streaming to Storage...");
        res.send("The export has been successfully initialized");
    } catch (e) {
        console.error(e.message, e.name);
        res.status(500).send("An error occurred during the export initialization");
    }
};

// Initialize the PDF write stream
function createWriteStream(file) {
    var writeStream = file.createWriteStream({
        metadata: {
            //metadata you want to set
        }
    });
    writeStream .on('error', function(e) { console.error("An error occurred : " + e); });
    writeStream .on('finish', function() { console.log("Export  completed"); });
    return writeStream ;
}

Hope it helps!

like image 115
Gwendal Le Cren Avatar answered Sep 20 '22 14:09

Gwendal Le Cren