Comparing version 3.0.2 to 3.1.0
109
index.js
@@ -100,3 +100,3 @@ var AWS = require('aws-sdk'); | ||
uploader.progressAmount = 0; | ||
uploader.progressTotal = 1; | ||
uploader.progressTotal = 0; | ||
@@ -107,7 +107,7 @@ var localFile = params.localFile; | ||
if (!localFileStat || !localFileStat.md5sum) { | ||
doStatAndMd5Sum(); | ||
if (!localFileStat) { | ||
doStat(); | ||
} else { | ||
uploader.progressTotal = localFileStat.size; | ||
startPuttingObject(); | ||
setImmediate(startPuttingObject); | ||
} | ||
@@ -117,8 +117,4 @@ | ||
function doStatAndMd5Sum() { | ||
var md5sum; | ||
var pend = new Pend(); | ||
pend.go(doStat); | ||
pend.go(doMd5Sum); | ||
pend.wait(function(err) { | ||
function doStat() { | ||
fs.stat(localFile, function(err, stat) { | ||
if (err) { | ||
@@ -128,42 +124,9 @@ uploader.emit('error', err); | ||
} | ||
localFileStat.md5sum = md5sum; | ||
localFileStat = stat; | ||
uploader.progressTotal = stat.size; | ||
startPuttingObject(); | ||
}); | ||
function doStat(cb) { | ||
fs.stat(localFile, function(err, stat) { | ||
if (!err) { | ||
localFileStat = stat; | ||
uploader.progressTotal = stat.size; | ||
} | ||
cb(err); | ||
}); | ||
} | ||
function doMd5Sum(cb) { | ||
var inStream = fs.createReadStream(localFile); | ||
var counter = new StreamCounter(); | ||
inStream.on('error', function(err) { | ||
cb(err); | ||
}); | ||
uploader.emit('stream', inStream); | ||
var hash = crypto.createHash('md5'); | ||
hash.on('data', function(digest) { | ||
md5sum = digest; | ||
cb(); | ||
}); | ||
counter.on('progress', function() { | ||
uploader.progressMd5Amount = counter.bytes; | ||
uploader.emit('progress'); | ||
}); | ||
inStream.pipe(hash); | ||
inStream.pipe(counter); | ||
} | ||
} | ||
function startPuttingObject() { | ||
if (localFileStat.size > MAX_PUTOBJECT_SIZE) { | ||
uploader.emit('error', new Error("file exceeded max size for putObject")); | ||
return; | ||
} | ||
doWithRetry(tryPuttingObject, self.s3RetryCount, self.s3RetryDelay, function(err, data) { | ||
@@ -180,4 +143,12 @@ if (err) { | ||
function tryPuttingObject(cb) { | ||
if (localFileStat.size > MAX_PUTOBJECT_SIZE) { | ||
var err = new Error("file exceeded max size for putObject"); | ||
err.retryable = false; | ||
uploader.emit('error', err); | ||
return; | ||
} | ||
self.s3Pend.go(function(pendCb) { | ||
var pend = new Pend(); | ||
var inStream = fs.createReadStream(localFile); | ||
uploader.emit('stream', inStream); | ||
var errorOccurred = false; | ||
@@ -189,4 +160,13 @@ inStream.on('error', function(err) { | ||
}); | ||
var hash = crypto.createHash('md5'); | ||
pend.go(function(cb) { | ||
hash.on('data', function(digest) { | ||
localFileStat.md5sum = digest; | ||
cb(); | ||
}); | ||
}); | ||
s3Params.Body = inStream; | ||
s3Params.ContentMD5 = localFileStat.md5sum.toString('base64'); | ||
if (localFileStat.md5sum) { | ||
s3Params.ContentMD5 = localFileStat.md5sum.toString('base64'); | ||
} | ||
s3Params.ContentLength = localFileStat.size; | ||
@@ -199,3 +179,13 @@ uploader.progressAmount = 0; | ||
}); | ||
pend.go(function(cb) { | ||
counter.on('finish', function() { | ||
uploader.progressAmount = counter.bytes; | ||
uploader.progressTotal = counter.bytes; | ||
uploader.emit('progress'); | ||
localFileStat.size = counter.bytes; | ||
cb(); | ||
}); | ||
}); | ||
inStream.pipe(counter); | ||
inStream.pipe(hash); | ||
self.s3.putObject(s3Params, function(err, data) { | ||
@@ -209,8 +199,10 @@ pendCb(); | ||
} | ||
if (!compareETag(data.ETag, localFileStat.md5sum)) { | ||
errorOccurred = true; | ||
cb(new Error("ETag does not match MD5 checksum")); | ||
return; | ||
} | ||
cb(null, data); | ||
pend.wait(function() { | ||
if (!compareETag(data.ETag, localFileStat.md5sum)) { | ||
errorOccurred = true; | ||
cb(new Error("ETag does not match MD5 checksum")); | ||
return; | ||
} | ||
cb(null, data); | ||
}); | ||
}); | ||
@@ -800,8 +792,14 @@ }); | ||
var prevAmountDone = 0; | ||
var prevAmountTotal = localFileStat.size; | ||
uploader.on('error', handleError); | ||
uploader.on('progress', function() { | ||
if (fatalError) return; | ||
var delta = uploader.progressAmount - prevAmountDone; | ||
var amountDelta = uploader.progressAmount - prevAmountDone; | ||
prevAmountDone = uploader.progressAmount; | ||
ee.progressAmount += delta; | ||
ee.progressAmount += amountDelta; | ||
var totalDelta = uploader.progressTotal - prevAmountTotal; | ||
prevAmountTotal = uploader.progressTotal; | ||
ee.progressTotal += totalDelta; | ||
ee.emit('progress'); | ||
@@ -1027,3 +1025,4 @@ }); | ||
function getPublicUrl(bucket, key, bucketLocation) { | ||
var hostnamePrefix = bucketLocation ? ("s3-" + bucketLocation) : "s3"; | ||
var nonStandardBucketLocation = (bucketLocation && bucketLocation !== 'us-east-1'); | ||
var hostnamePrefix = nonStandardBucketLocation ? ("s3-" + bucketLocation) : "s3"; | ||
var parts = { | ||
@@ -1030,0 +1029,0 @@ protocol: "https:", |
{ | ||
"name": "s3", | ||
"version": "3.0.2", | ||
"version": "3.1.0", | ||
"description": "high level amazon s3 client. upload and download files and directories", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -196,5 +196,4 @@ # High Level Amazon S3 Client | ||
* `localFile`: path to the file on disk you want to upload to S3. | ||
* `localFileStat`: optional - if you happen to have the stat object from | ||
`fs.stat` and the md5sum of the file, you can provide it here. Otherwise it | ||
will be computed for you. | ||
* `localFileStat`: optional - if you happen to already have the stat object | ||
from `fs.stat`, you can provide it here. | ||
@@ -275,5 +274,5 @@ The difference between using AWS SDK `putObject` and this one: | ||
* Retry based on the client's retry settings. | ||
* Retries based on the client's retry settings. | ||
* Supports recursive directory listing. | ||
* Make multiple requests if the number of objects to list is greater than 1000. | ||
* Makes multiple requests if the number of objects to list is greater than 1000. | ||
@@ -305,3 +304,3 @@ Returns an `EventEmitter` with these properties: | ||
The difference between using AWS SDK `deleteObjects` and this one is that this one will: | ||
The difference between using AWS SDK `deleteObjects` and this one: | ||
@@ -487,3 +486,3 @@ * Retry based on the client's retry settings. | ||
The difference between using AWS SDK `copyObject` and this one will: | ||
The difference between using AWS SDK `copyObject` and this one: | ||
@@ -490,0 +489,0 @@ * Retry based on the client's retry settings. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
46128
933
585