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:
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?
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.
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With