bloody-simple-s3
Advanced tools
Comparing version 0.3.0-alpha to 0.3.0
@@ -0,1 +1,5 @@ | ||
## 0.3.0 - 2015-04-03 | ||
* Add #rename as alias of #move | ||
## 0.3.0-alpha - 2015-03-16 | ||
@@ -2,0 +6,0 @@ |
@@ -7,10 +7,11 @@ # Bloody Simple S3 | ||
* [Methods](#methods) | ||
* [createReadStream([callback])](#createReadStream) | ||
* [writeFileStream(path, readable, [callback])](#writeFileStream) | ||
* [createReadStream(filename)](#createReadStream) | ||
* [writeFile(filename, contents, [callback])](#writeFile) | ||
* [list(dir, options, [callback])](#list) | ||
* [copy(source, destination, [callback])](#copy) | ||
* [move(source, destination, [callback])](#move) | ||
* [rename(source, destination, [callback])](#rename) | ||
* [remove(path, [callback])](#remove) | ||
* [download(path, options, [callback])](#download) | ||
* [upload(path, options, [callback])](#upload) | ||
* [download(source, [destination], [callback])](#download) | ||
* [upload(source, [destination], [callback])](#upload) | ||
@@ -50,3 +51,3 @@ ## Constructor | ||
### <a name="createReadStream" href="#createReadStream">#</a>createReadStream(path) -> ReadableStream | ||
### <a name="createReadStream" href="#createReadStream">#</a>createReadStream(filename) -> ReadableStream | ||
@@ -57,7 +58,7 @@ Creates and returns a readable stream to the designated file. | ||
* `path` _(String)_ relative file path on S3 | ||
* `filename` _(String)_ relative file path on S3 | ||
##### Throws | ||
_(Error)_ if path is invalid. | ||
_(Error)_ if filename is invalid. | ||
@@ -75,10 +76,10 @@ ##### Returns | ||
### <a name="writeFileStream" href="#writeFileStream">#</a>writeFileStream(path, readable, [callback]) -> Promise | ||
### <a name="writeFile" href="#writeFile">#</a>writeFile(filename, contents, [callback]) -> Promise | ||
Creates of updates the designated file, consuming a readable stream. | ||
Creates of updates the designated file with the given contents. | ||
##### Parameters | ||
* `path` _(String)_ relative file path on S3 | ||
* `readable` _(ReadableStream)_ a readable file stream to pull file data | ||
* `filename` _(String)_ file path on S3 | ||
* `contents` _(ReadableStream, Buffer, String)_ the contents of the file | ||
* `callback` _(Function)_ optional callback function with (err, file) arguments | ||
@@ -90,3 +91,3 @@ | ||
* `path` _(String)_ absolute file path in local filesystem | ||
* `name` _(String)_ relative file path on S3 | ||
@@ -98,3 +99,3 @@ ##### Example | ||
s3.writeFileStream('images/test.png', readable) | ||
s3.writeFile('images/test.png', readable) | ||
.then(function (file) { | ||
@@ -124,3 +125,3 @@ // do something on success | ||
* `path` _(String)_ relative file path on S3 | ||
* `name` _(String)_ relative file path on S3 | ||
* `size` _(Number)_ file size | ||
@@ -135,3 +136,3 @@ * `last_modified` _(Date)_ date file was last modified | ||
files.forEach(function (file, i) { | ||
console.log(i, file.path); | ||
console.log(i, file.name); | ||
}); | ||
@@ -144,5 +145,5 @@ }) | ||
### <a name="copy" href="#copy">#</a>copy(source, destination, [callback]) -> Promise | ||
### <a name="copy" href="#copy">#</a>copy(source, target, [callback]) -> Promise | ||
Copies the designated source file to the specified destination. | ||
Copies the designated source file to the specified target. | ||
@@ -152,3 +153,3 @@ ##### Parameters | ||
* `source` _(String)_ relative source file path on S3 | ||
* `destination` _(Object)_ relative destination file path on S3 | ||
* `target` _(Object)_ relative target file path on S3 | ||
* `callback` _(Function)_ optional callback function with (err, file) arguments | ||
@@ -160,3 +161,3 @@ | ||
* `path` _(String)_ relative file path on S3 | ||
* `name` _(String)_ relative file path on S3 | ||
* `last_modified` _(Date)_ date file was last modified | ||
@@ -176,3 +177,3 @@ | ||
### <a name="move" href="#move">#</a>move(source, destination, [callback]) -> Promise | ||
### <a name="move" href="#move">#</a>move(source, target, [callback]) -> Promise | ||
@@ -184,3 +185,3 @@ Moves the designated file within S3. | ||
* `source` _(String)_ relative source file path on S3 | ||
* `destination` _(Object)_ relative destination file path on S3 | ||
* `target` _(Object)_ relative target file path on S3 | ||
* `callback` _(Function)_ optional callback function with (err, file) arguments | ||
@@ -204,4 +205,8 @@ | ||
### <a name="remove" href="#remove">#</a>remove(path, [callback]) -> Promise | ||
### <a name="rename" href="#rename">#</a>rename(source, target, [callback]) -> Promise | ||
Alias of [#move](#move). | ||
### <a name="remove" href="#remove">#</a>remove(filename, [callback]) -> Promise | ||
Removes the designated file from S3. | ||
@@ -211,3 +216,3 @@ | ||
* `path` _(String)_ relative file path on S3 | ||
* `filename` _(String)_ relative file path on S3 | ||
* `callback` _(Function)_ optional callback function with (err) arguments | ||
@@ -217,3 +222,3 @@ | ||
A empty promise. | ||
An empty promise. | ||
@@ -232,3 +237,3 @@ ##### Example | ||
### <a name="download" href="#download">#</a>download(path, options, [callback]) -> Promise | ||
### <a name="download" href="#download">#</a>download(source, target, [callback]) -> Promise | ||
@@ -239,5 +244,4 @@ Downloads the designated file from S3 to the local filesystem. | ||
* `path` _(String)_ relative file path on S3 | ||
* `options` _(Object)_ download options | ||
* `destination` _(String)_ destination file path; defaults to `os.tmpdir()` | ||
* `source` _(String)_ relative file path on S3 | ||
* `target` _(String)_ local file or directory path; defaults to `os.tmpdir()` | ||
* `callback` _(Function)_ optional callback function with (err, localPath) arguments | ||
@@ -249,3 +253,3 @@ | ||
* `path` _(String)_ absolute file path on the local filesystem | ||
* `name` _(String)_ absolute file path on the local filesystem | ||
@@ -255,5 +259,5 @@ ##### Example | ||
```javascript | ||
s3.download('images/test-123.png', {destination: '/Users/jmike/image.png'}) | ||
s3.download('images/test-123.png', '/Users/jmike/image.png') | ||
.then(function (file) { | ||
console.log(file.path); // prints "/Users/jmike/image.png" | ||
console.log(file.name); // prints "/Users/jmike/image.png" | ||
}) | ||
@@ -265,3 +269,3 @@ .catch(function (err) { | ||
### <a name="upload" href="#upload">#</a>upload(path, options, [callback]) -> Promise | ||
### <a name="upload" href="#upload">#</a>upload(source, target, [callback]) -> Promise | ||
@@ -272,5 +276,4 @@ Uploads the designated file from the local filesystem to S3. | ||
* `path` _(String)_ relative or absolute file path on the local filesystem | ||
* `options` _(Object)_ download options | ||
* `destination` _(String)_ destination file path on S3 | ||
* `source` _(String)_ relative or absolute file path on the local filesystem | ||
* `target` _(String)_ target file path on S3 | ||
* `callback` _(Function)_ optional callback function with (err, file) arguments | ||
@@ -280,5 +283,5 @@ | ||
A promise resolving to the attributes of the uploaded file, i.e. an object with the followin properties: | ||
A promise resolving to the attributes of the uploaded file, i.e. an object with the following properties: | ||
* `path` _(String)_ relative file path on S3 | ||
* `name` _(String)_ relative file path on S3 | ||
@@ -288,3 +291,3 @@ ##### Example | ||
```javascript | ||
s3.upload('/Users/jmike/image.png', {destination: 'images/jmike.png'}) | ||
s3.upload('/Users/jmike/image.png', 'images/jmike.png') | ||
.then(function (file) { | ||
@@ -291,0 +294,0 @@ // do something on success |
{ | ||
"name": "bloody-simple-s3", | ||
"version": "0.3.0-alpha", | ||
"version": "0.3.0", | ||
"description": "A bloody simple interface to S3, based on the official AWS sdk", | ||
@@ -11,3 +11,3 @@ "repository": { | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "node test/index.js" | ||
}, | ||
@@ -27,7 +27,11 @@ "keywords": [ | ||
"dependencies": { | ||
"aws-sdk": "^2.1.17", | ||
"aws-sdk": "^2.1.21", | ||
"bluebird": "^2.9.14", | ||
"dotenv": "^0.5.1", | ||
"lodash": "^3.5.0" | ||
"lodash": "^3.6.0" | ||
}, | ||
"devDependencies": { | ||
"chai": "^2.2.0", | ||
"dotenv": "^1.1.0", | ||
"mocha": "^2.2.1" | ||
} | ||
} |
# Bloody simple S3 | ||
A bloody simple S3 client, based on the official AWS sdk, using Bluebird promises. | ||
A bloody simple S3 client, based on the official AWS SDK. | ||
[ ![Build Status for visionmobile/bloody-simple-sqs](https://codeship.com/projects/ce3c9d80-bb71-0132-8afa-3ee0b98d9f7e/status?branch=master)](https://codeship.com/projects/72166) | ||
#### Features | ||
* Promise and callback interface; | ||
* Bloody simple API. | ||
## Quick start | ||
```javascript | ||
var S3 = require('bloody-simple-s3'); | ||
var s3 = new S3({ | ||
bucket: 'bucket-name', | ||
region: 'us-east-1', | ||
accessKeyId: 'AKIA-access-key', | ||
secretAccessKey: 'secret-access-key', | ||
sslEnabled: true | ||
}); | ||
s3.upload('/Users/john/Photos/monkey.jpg', 'images/monkey-1.jpg) | ||
.then(function (file) { | ||
console.log(file.path); | ||
}) | ||
.catch(function (err) { | ||
console.error(err); | ||
}); | ||
``` | ||
For further information on how to use this library please refer to the [API docs](https://github.com/jmike/bloody-simple-s3/blob/master/docs/API.md). | ||
## Installation | ||
@@ -11,8 +42,22 @@ | ||
## Contributors | ||
#### Requirements | ||
Author: [Dimitrios C. Michalakos](https://github.com/jmike) | ||
* Node.js 0.8+* | ||
## Contribute | ||
Source code contributions are most welcome. The following rules apply: | ||
1. JavaScript source code needs to follow the [Airbnb Style Guide](https://github.com/airbnb/javascript); | ||
2. Functions need to be well documented in [API docs](https://github.com/jmike/bloody-simple-s3/blob/master/docs/API.md); | ||
3. Unit tests are necessary. | ||
## Support | ||
If you are having issues with this library, please let us know. | ||
* Issue Tracker: [github.com/jmike/bloody-simple-s3/issues](https://github.com/jmike/bloody-simple-s3/issues) | ||
## License | ||
MIT |
@@ -1,2 +0,2 @@ | ||
var pathjs = require('path'); | ||
var path = require('path'); | ||
var fs = require('fs'); | ||
@@ -9,3 +9,2 @@ var os = require('os'); | ||
function BloodySimpleS3(options) { | ||
@@ -16,3 +15,2 @@ if (!_.isPlainObject(options)) { | ||
// set default options | ||
options = _.defaults(options, { | ||
@@ -44,2 +42,3 @@ region: 'us-east-1', | ||
this.bucket = options.bucket; | ||
this.s3 = new AWS.S3({ | ||
@@ -54,12 +53,11 @@ accessKeyId: options.accessKeyId, | ||
BloodySimpleS3.prototype.createReadStream = function (path) { | ||
BloodySimpleS3.prototype.createReadStream = function (filename) { | ||
var params; | ||
if (!_.isString(path)) { | ||
throw new Error('Invalid path param; expected string, received ' + typeof(path)); | ||
if (!_.isString(filename)) { | ||
throw new Error('Invalid filename param; expected string, received ' + typeof(filename)); | ||
} | ||
params = { | ||
Key: path, | ||
Key: filename, | ||
Bucket: this.bucket | ||
@@ -71,31 +69,25 @@ }; | ||
BloodySimpleS3.prototype.download = function (path, options, callback) { | ||
BloodySimpleS3.prototype.download = function (source, target, callback) { | ||
var _this = this; | ||
var resolver; | ||
if (!_.isString(path)) { | ||
return Promise.reject(new Error('Invalid path param; expected string, received ' + typeof(path))) | ||
if (!_.isString(source)) { | ||
return Promise.reject(new Error('Invalid source param; expected string, received ' + typeof(source))) | ||
.nodeify(callback); | ||
} | ||
if (_.isFunction(options)) { | ||
callback = options; | ||
options = {}; | ||
} else if (_.isUndefined(options)) { | ||
options = {}; | ||
if (_.isFunction(target)) { | ||
callback = target; | ||
target = os.tmpdir(); | ||
} else if (_.isUndefined(target)) { | ||
target = os.tmpdir(); | ||
} | ||
if (!_.isPlainObject(options)) { | ||
return Promise.reject(new Error('Invalid options param; expected object, received ' + typeof(options))) | ||
if (!_.isString(target)) { | ||
return Promise.reject(new Error('Invalid target param; expected string, received ' + typeof(target))) | ||
.nodeify(callback); | ||
} | ||
// set default options | ||
options = _.defaults(options, { | ||
destination: os.tmpdir() | ||
}); | ||
resolver = function(resolve, reject) { | ||
fs.stat(options.destination, function (err, stats) { | ||
fs.stat(target, function (err, stats) { | ||
var file, writable, readable; | ||
@@ -106,12 +98,12 @@ | ||
if (stats.isDirectory()) { | ||
file = pathjs.join(options.destination, pathjs.basename(path)); | ||
file = path.join(target, path.basename(source)); | ||
} else if (stats.isFile()) { | ||
file = options.destination; | ||
file = target; | ||
} else { | ||
return reject(new Error('Invalid destination path; expected folder or file')); | ||
return reject(new Error('Invalid target path; expected directory or file')); | ||
} | ||
readable = _this.createReadStream(path); | ||
readable = _this.createReadStream(source); | ||
writable = fs.createWriteStream(file); | ||
@@ -121,6 +113,5 @@ readable.pipe(writable); | ||
readable.on('error', reject); | ||
writable.on('finish', function () { | ||
resolve({ | ||
path: file | ||
}); | ||
resolve({name: file}); | ||
}); | ||
@@ -130,7 +121,7 @@ }); | ||
return new Promise(resolver).nodeify(callback); | ||
return new Promise(resolver) | ||
.nodeify(callback); | ||
}; | ||
BloodySimpleS3.prototype.writeFileStream = function (path, readable, callback) { | ||
BloodySimpleS3.prototype.writeFile = function (filename, contents, callback) { | ||
var _this = this; | ||
@@ -140,9 +131,13 @@ var params; | ||
if (!_.isString(path)) { | ||
return Promise.reject(new Error('Invalid path param; expected string, received ' + typeof(path))) | ||
if (!_.isString(filename)) { | ||
return Promise.reject(new Error('Invalid filename param; expected string, received ' + typeof(filename))) | ||
.nodeify(callback); | ||
} | ||
if (!(readable instanceof stream.Readable)) { | ||
return Promise.reject(new Error('Invalid readable param; expected ReadableStream, received ' + typeof(readable))) | ||
if ( | ||
!(contents instanceof stream.Readable) && | ||
!(Buffer.isBuffer(contents)) && | ||
!_.isString(contents) | ||
) { | ||
return Promise.reject(new Error('Invalid contents param; expected readable stream, buffer or string, received ' + typeof(contents))) | ||
.nodeify(callback); | ||
@@ -152,4 +147,4 @@ } | ||
params = { | ||
Key: path, | ||
Body: readable, | ||
Key: filename, | ||
Body: contents, | ||
Bucket: _this.bucket | ||
@@ -161,44 +156,35 @@ }; | ||
if (err) return reject(err); | ||
resolve({ | ||
path: path | ||
}); | ||
resolve({name: filename}); | ||
}); | ||
}; | ||
return new Promise(resolver).nodeify(callback); | ||
return new Promise(resolver) | ||
.nodeify(callback); | ||
}; | ||
BloodySimpleS3.prototype.upload = function (path, options, callback) { | ||
BloodySimpleS3.prototype.upload = function (source, target, callback) { | ||
var _this = this; | ||
var resolver; | ||
if (!_.isString(path)) { | ||
return Promise.reject(new Error('Invalid path param; expected string, received ' + typeof(path))) | ||
if (!_.isString(source)) { | ||
return Promise.reject(new Error('Invalid source param; expected string, received ' + typeof(source))) | ||
.nodeify(callback); | ||
} | ||
if (_.isFunction(options)) { | ||
callback = options; | ||
options = {}; | ||
} else if (_.isUndefined(options)) { | ||
options = {}; | ||
source = path.resolve(__dirname, source); | ||
if (_.isFunction(target)) { | ||
callback = target; | ||
target = path.basename(source); | ||
} else if (_.isUndefined(target)) { | ||
target = path.basename(source); | ||
} | ||
if (!_.isPlainObject(options)) { | ||
return Promise.reject(new Error('Invalid options param; expected object, received ' + typeof(options))) | ||
if (!_.isString(target)) { | ||
return Promise.reject(new Error('Invalid target param; expected string, received ' + typeof(target))) | ||
.nodeify(callback); | ||
} | ||
// resolve (possible) relative path | ||
path = pathjs.resolve(__dirname, path); | ||
// set default options | ||
options = _.defaults(options, { | ||
destination: pathjs.basename(path) | ||
}); | ||
resolver = function(resolve, reject) { | ||
fs.stat(path, function (err, stats) { | ||
fs.stat(source, function (err, stats) { | ||
var readable; | ||
@@ -209,15 +195,15 @@ | ||
if (!stats.isFile()) { | ||
return reject(new Error('File path is invalid; you must reference an actual file')); | ||
return reject(new Error('Source is invalid; you must reference a file')); | ||
} | ||
readable = fs.createReadStream(path); | ||
readable = fs.createReadStream(source); | ||
resolve(_this.writeFileStream(options.destination, readable)); | ||
resolve(_this.writeFile(target, readable)); | ||
}); | ||
}; | ||
return new Promise(resolver).nodeify(callback); | ||
return new Promise(resolver) | ||
.nodeify(callback); | ||
}; | ||
BloodySimpleS3.prototype.list = function (dir, options, callback) { | ||
@@ -247,3 +233,3 @@ var _this = this; | ||
Bucket: this.bucket, | ||
Prefix: pathjs.normalize(dir), | ||
Prefix: path.normalize(dir), | ||
Marker: options.cursor, | ||
@@ -261,4 +247,3 @@ MaxKeys: options.limit | ||
return { | ||
key: obj.Key, // legacy | ||
path: obj.Key, | ||
name: obj.Key, | ||
size: obj.Size, | ||
@@ -276,4 +261,3 @@ last_modified: obj.LastModified | ||
BloodySimpleS3.prototype.copy = function (source, destination, options, callback) { | ||
BloodySimpleS3.prototype.copy = function (source, target, options, callback) { | ||
var _this = this; | ||
@@ -288,4 +272,4 @@ var params; | ||
if (!_.isString(destination)) { | ||
return Promise.reject(new Error('Invalid destination param; expected string, received ' + typeof(destination))) | ||
if (!_.isString(target)) { | ||
return Promise.reject(new Error('Invalid target param; expected string, received ' + typeof(target))) | ||
.nodeify(callback); | ||
@@ -308,4 +292,4 @@ } | ||
Bucket: this.bucket, | ||
CopySource: encodeURIComponent(pathjs.join(this.bucket, source)), | ||
Key: destination, | ||
CopySource: encodeURIComponent(path.join(this.bucket, source)), | ||
Key: target, | ||
MetadataDirective: 'COPY' | ||
@@ -317,8 +301,3 @@ }); | ||
if (err) return reject(err); | ||
resolve({ | ||
key: destination, | ||
path: destination, | ||
last_modified: data.LastModified | ||
}); | ||
resolve({name: target, last_modified: data.LastModified}); | ||
}); | ||
@@ -330,4 +309,3 @@ }; | ||
BloodySimpleS3.prototype.remove = function (path, callback) { | ||
BloodySimpleS3.prototype.remove = function (filename, callback) { | ||
var _this = this; | ||
@@ -337,4 +315,4 @@ var params; | ||
if (!_.isString(path)) { | ||
return Promise.reject(new Error('Invalid path param; expected string, received ' + typeof(path))) | ||
if (!_.isString(filename)) { | ||
return Promise.reject(new Error('Invalid filename param; expected string, received ' + typeof(filename))) | ||
.nodeify(callback); | ||
@@ -345,3 +323,3 @@ } | ||
Bucket: this.bucket, | ||
Key: path | ||
Key: filename | ||
}; | ||
@@ -359,4 +337,3 @@ | ||
BloodySimpleS3.prototype.move = function (source, destination, options, callback) { | ||
BloodySimpleS3.prototype.move = function (source, target, options, callback) { | ||
if (_.isFunction(options)) { | ||
@@ -367,3 +344,3 @@ callback = options; | ||
return this.copy(source, destination, options) | ||
return this.copy(source, target, options) | ||
.bind(this) | ||
@@ -376,3 +353,4 @@ .then(function (data) { | ||
BloodySimpleS3.prototype.rename = BloodySimpleS3.prototype.move; | ||
module.exports = BloodySimpleS3; |
@@ -1,38 +0,15 @@ | ||
require('dotenv').load(); // load environmental variables | ||
var path = require('path'); | ||
var Mocha = require('mocha'); | ||
var Mocha = require('mocha'), | ||
fs = require('fs'), | ||
path = require('path'), | ||
mocha; | ||
function loadFiles(directory) { | ||
fs.readdirSync(directory) | ||
.filter(function (file) { // exclude index.js | ||
return file !== 'index.js'; | ||
}) | ||
.forEach(function (file) { | ||
var location, stats; | ||
location = path.join(directory, file); | ||
stats = fs.statSync(location); | ||
if (stats.isDirectory()) { | ||
loadFiles(location); // traverse directory | ||
} else if (file.substr(-3) === '.js') { // keep only .js files | ||
mocha.addFile(location); | ||
} | ||
}); | ||
} | ||
// init mocha | ||
mocha = new Mocha({ | ||
var mocha = new Mocha({ | ||
reporter: 'spec', | ||
timeout: 10000 // 10 secs | ||
timeout: 30000 // 30 secs | ||
}); | ||
// load the test files | ||
loadFiles(__dirname); | ||
mocha.addFile(path.resolve(__dirname, './s3')); | ||
// run the tests | ||
mocha.run(function(failures){ | ||
mocha.run(function (failures) { | ||
process.on('exit', function () { | ||
@@ -39,0 +16,0 @@ process.exit(failures); |
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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances 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
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
94269
3
11
373
2
63
3
4
- Removeddotenv@^0.5.1
- Removeddotenv@0.5.1(transitive)
Updatedaws-sdk@^2.1.21
Updatedlodash@^3.6.0