Ho avuto esattamente lo stesso problema durante il download da file molto grandi S3.
La soluzione esempio dalla documentazione AWS semplicemente non funziona:
var file = fs.createWriteStream(options.filePath);
file.on('close', function(){
if(self.logger) self.logger.info("S3Dataset file download saved to %s", options.filePath);
return callback(null,done);
});
s3.getObject({ Key: documentKey }).createReadStream().on('error', function(err) {
if(self.logger) self.logger.error("S3Dataset download error key:%s error:%@", options.fileName, error);
return callback(error);
}).pipe(file);
Anche se questa soluzione funziona:
var file = fs.createWriteStream(options.filePath);
s3.getObject({ Bucket: this._options.s3.Bucket, Key: documentKey })
.on('error', function(err) {
if(self.logger) self.logger.error("S3Dataset download error key:%s error:%@", options.fileName, error);
return callback(error);
})
.on('httpData', function(chunk) { file.write(chunk); })
.on('httpDone', function() {
file.end();
if(self.logger) self.logger.info("S3Dataset file download saved to %s", options.filePath);
return callback(null,done);
})
.send();
Il tentativo createReadStream
semplicemente non si attiva la richiamata end
, close
o error
per qualche ragione. Vedi here su questo.
sto usando questa soluzione anche per scrivere gli archivi di gzip, dal momento che il primo (AWS esempio) non funziona in questo caso sia:
var gunzip = zlib.createGunzip();
var file = fs.createWriteStream(options.filePath);
s3.getObject({ Bucket: this._options.s3.Bucket, Key: documentKey })
.on('error', function (error) {
if(self.logger) self.logger.error("%@",error);
return callback(error);
})
.on('httpData', function (chunk) {
file.write(chunk);
})
.on('httpDone', function() {
file.end();
if(self.logger) self.logger.info("downloadArchive downloaded %s", options.filePath);
fs.createReadStream(options.filePath)
.on('error', (error) => {
return callback(error);
})
.on('end',() => {
if(self.logger) self.logger.info("downloadArchive unarchived %s", options.fileDest);
return callback(null, options.fileDest);
})
.pipe(gunzip)
.pipe(fs.createWriteStream(options.fileDest))
})
.send();
contents.Body.toString() al posto dei contenuti .Body – Jason