2013-10-24 04:23:28 +00:00
|
|
|
# loopback-storage-service
|
2013-06-24 21:02:30 +00:00
|
|
|
|
2013-10-24 04:23:28 +00:00
|
|
|
LoopBack Storage Service
|
2013-06-24 23:57:16 +00:00
|
|
|
|
|
|
|
## Storage
|
|
|
|
|
2013-10-24 04:23:28 +00:00
|
|
|
The `loopback-storage-service` module is designed to make it easy to upload and download files to various infrastructure providers.
|
2013-06-24 23:57:16 +00:00
|
|
|
|
2013-07-16 17:42:47 +00:00
|
|
|
To get started with a `loopback-storage-service` provider just create one:
|
2013-06-24 23:57:16 +00:00
|
|
|
|
|
|
|
``` js
|
2013-07-16 17:42:47 +00:00
|
|
|
var storageService = require('loopback-storage-service')({
|
2013-06-24 23:57:16 +00:00
|
|
|
//
|
|
|
|
// The name of the provider (e.g. "file")
|
|
|
|
//
|
|
|
|
provider: 'provider-name',
|
|
|
|
|
|
|
|
//
|
|
|
|
// ... Provider specific credentials
|
|
|
|
//
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
|
|
|
Each compute provider takes different credentials to authenticate; these details about each specific provider can be found below:
|
|
|
|
|
2013-10-24 04:23:28 +00:00
|
|
|
* Amazon
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
provider: 'amazon',
|
|
|
|
key: '...',
|
|
|
|
keyId: '...'
|
|
|
|
}
|
|
|
|
|
|
|
|
* Rackspace
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
provider: 'rackspace',
|
|
|
|
username: '...',
|
|
|
|
apiKey: '...'
|
|
|
|
}
|
|
|
|
|
|
|
|
* Azure
|
|
|
|
|
|
|
|
* Local File System
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
provider: 'filesystem',
|
|
|
|
root: '/tmp/storage'
|
|
|
|
}
|
2013-06-24 23:57:16 +00:00
|
|
|
|
|
|
|
Each instance of `storage.Client` returned from `storage.createClient` has a set of uniform APIs:
|
|
|
|
|
|
|
|
### Container
|
2013-07-03 01:10:23 +00:00
|
|
|
* `storageService.getContainers(function (err, containers) { })`
|
|
|
|
* `storageService.createContainer(options, function (err, container) { })`
|
|
|
|
* `storageService.destroyContainer(containerName, function (err) { })`
|
|
|
|
* `storageService.getContainer(containerName, function (err, container) { })`
|
2013-06-24 23:57:16 +00:00
|
|
|
|
|
|
|
### File
|
2013-07-03 01:10:23 +00:00
|
|
|
* `storageService.upload(options, function (err) { })`
|
|
|
|
* `storageService.download(options, function (err) { })`
|
|
|
|
* `storageService.getFiles(container, function (err, files) { })`
|
|
|
|
* `storageService.getFile(container, file, function (err, server) { })`
|
|
|
|
* `storageService.removeFile(container, file, function (err) { })`
|
2013-06-24 23:57:16 +00:00
|
|
|
|
|
|
|
Both the `.upload(options)` and `.download(options)` have had **careful attention paid to make sure they are pipe and stream capable:**
|
|
|
|
|
|
|
|
### Upload a File
|
|
|
|
``` js
|
2013-07-16 17:42:47 +00:00
|
|
|
var storage = require('loopback-storage-service'),
|
2013-06-24 23:57:16 +00:00
|
|
|
fs = require('fs');
|
|
|
|
|
2013-07-03 01:10:23 +00:00
|
|
|
var storageService = storage({ /* ... */ });
|
2013-06-24 23:57:16 +00:00
|
|
|
|
2013-07-03 01:10:23 +00:00
|
|
|
fs.createReadStream('a-file.txt').pipe(storageService.uploadStream('a-container','remote-file-name.txt'));
|
2013-06-24 23:57:16 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
### Download a File
|
|
|
|
``` js
|
2013-07-16 17:42:47 +00:00
|
|
|
var storage = require('loopback-storage-service'),
|
2013-06-24 23:57:16 +00:00
|
|
|
fs = require('fs');
|
|
|
|
|
2013-07-03 01:10:23 +00:00
|
|
|
var storageService = storage({ /* ... */ });
|
2013-06-24 23:57:16 +00:00
|
|
|
|
2013-07-03 01:10:23 +00:00
|
|
|
storageService.downloadStream({
|
2013-06-24 23:57:16 +00:00
|
|
|
container: 'a-container',
|
|
|
|
remote: 'remote-file-name.txt'
|
|
|
|
}).pipe(fs.createWriteStream('a-file.txt'));
|
|
|
|
```
|
|
|
|
|