UNPKG

2.58 kBJavaScriptView Raw
1/**
2 * Artycles - S3 Store
3 * - Saving media in a remote S3 bucket
4**/
5
6var s3 = require("s3");
7
8var Model = function( options ){
9
10 // use the provided store (error control?)
11 //this.store = options.store;
12 // save other options
13 //delete options.store;
14 this.options = options;
15
16 var config = this.options.store;
17 this.store = s3.createClient({
18 //multipartUploadThreshold: 20971520, // this is the default (20 MB)
19 //multipartUploadSize: 15728640, // this is the default (15 MB)
20 s3Options: {
21 accessKeyId: config.key,
22 secretAccessKey: config.secret,
23 region: config.region
24 //secure: false,
25 }
26 });
27}
28
29
30Model.prototype = {
31
32 constructor: Model,
33
34 create: function( data, callback ){
35 // create request options
36 var options = {};
37
38 var params = {
39 localFile: data.source,
40 s3Params: {
41 Bucket: this.options.store.bucket,
42 Key: data.destination,
43 ACL: this.options.store.acl || 'private'
44 //ACL: 'private | public-read | public-read-write | authenticated-read | bucket-owner-read | bucket-owner-full-control',
45 // other options supported by putObject, except Body and ContentLength.
46 // See: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property
47 },
48 };
49 // use generic permissions
50 if( this.options.permissions == "public" ){
51 //if( this.options.store.acl == "public" ){
52 options['ACL'] = 'public-read';
53 // options: 'private | public-read | public-read-write | authenticated-read | bucket-owner-read | bucket-owner-full-control',
54 }
55
56 // upload a file to s3
57 var uploader = this.store.uploadFile( params );
58 // events
59 uploader.on('error', function(err) {
60 console.error("unable to upload:", err.stack);
61 });
62 uploader.on('progress', function() {
63 //console.log("progress", uploader.progressMd5Amount, uploader.progressAmount, uploader.progressTotal);
64 });
65 uploader.on('end', function() {
66 // error control?
67 callback( true );
68 });
69 },
70
71 read: function( query, callback ){
72 var key = query.access_token || query.code || false;
73 if( !key ) return callback(null, false);
74 // connect to store
75 this.store.get( key, function(err, data){
76 if(err) return callback(err);
77 // parse data into an object
78 data = JSON.parse( data.toString() );
79 callback( null, data );
80 });
81 },
82
83 destroy: function( item, callback ){
84 var key = query.access_token || query.code || false;
85 if( !key ) return callback(null, false);
86 // connect to store
87 this.store.del( key, function(err, data){
88 if(err) return callback(err);
89 callback( null, true );
90 });
91 }
92
93}
94
95
96module.exports = Model;
\No newline at end of file