通过file.createWriteStream()流式传输到GCP时,获取ESOCKETTIMEOUT,ECONNRESET或套接字挂起大文件

时间:2017-11-16 11:31:46

标签: node.js sockets file-upload google-cloud-storage google-cloud-functions

在尝试通过Google Cloud功能将大文件(> 50MB)上传到Google云端存储时,我会根据我设置的选项遇到这些异常:

  • 在设置请求选项'forever:false'时,我得到:错误:套接字挂起
  • 在设置请求选项'resumable:true'时,我得到:错误:写ECONNRESET
  • 在设置请求选项'resumable:false'时,我得到:错误:ESOCKETTIMEDOUT with resumable:false

以下是我使用的代码:

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);
}

我有什么遗失的吗?

1 个答案:

答案 0 :(得分:7)

我通过使用lib" request"修复了他的问题。 (2.83.0)而不是" request-promise"。这是我使用的简化代码:

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 ;
}

希望它有所帮助!