-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaliyun.server.js
133 lines (117 loc) · 4.03 KB
/
aliyun.server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// We use the official aws sdk
Aliyun = Npm.require('aliyun-sdk');
/**
* Creates an Aliyun OSS store instance on server. Inherits `FS.StorageAdapter`
* type.
*
* @public
* @constructor
* @param {String} name The store name
* @param {Object} options Storage options
* @return {FS.Store.OSS} An instance of FS.StorageAdapter.
*/
FS.Store.OSS = function(name, options) {
var self = this;
if (!(self instanceof FS.Store.OSS)) {
throw new Error('FS.Store.OSS missing keyword "new"');
}
options = options || {};
// Determine which folder (key prefix) in the bucket to use
var folder = options.folder;
folder = typeof folder === 'string' && folder.length ?
folder.replace(/^\//, '').replace(/\/?$/, '/') : '';
folder = folder === '/' ? '' : folder;
// Determine which bucket to use, reruired
var bucket = options.bucket;
if (!bucket) {
throw new Error('FS.Store.OSS requires "buckect"');
}
// Those ACL values are allowed: 'private', 'public-read', 'public-read-write'
var defaultAcl = options.ACL || 'private';
var region = options.region || 'oss-cn-hangzhou';
var regionList = ['oss-cn-qingdao', 'oss-cn-beijing', 'oss-cn-hangzhou',
'oss-cn-shanghai', 'oss-cn-hongkong', 'oss-cn-shenzhen', 'oss-us-west-1',
'oss-us-east-1', 'oss-ap-southeast-1'];
if (regionList.indexOf(region) === -1) {
throw new Error('FS.Store.OSS invalid region');
}
var endpoint = 'http://' + region +
(options.aliyunInternal ? '-internal' : '') + '.aliyuncs.com';
var serviceParams = FS.Utility.extend({
accessKeyId: null, // Required
secretAccessKey: null, // Required
endpoint: endpoint,
timeout: 60000,
apiVersion: '2013-10-15' // Required, DO NOT UPDATE
}, options);
// Create S3 service
var ossStore = new Aliyun.OSS(serviceParams);
/**
* Pick keys from object
* @param {Object} obj Original object
* @param {Array} keys Array of keys to be preserved
* @return {Object} New object
*/
function pick(obj, keys) {
var result = {}, iteratee = keys[0];
if (obj == null || arguments.length < 2) return result;
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
if (obj.hasOwnProperty(key)) {
result[key] = obj[key];
}
}
return result;
}
return new FS.StorageAdapter(name, options, {
typeName: 'storage.oss',
fileKey: function(fileObj) {
// Lookup the copy
var info = fileObj && fileObj._getInfo(name);
// If the store and key is found return the key
if (info && info.key) return info.key;
var filename = fileObj.name();
var filenameInStore = fileObj.name({store: name});
// If no store key found we resolve / generate a key
return fileObj.collectionName + '/' +
fileObj._id + '-' + (filenameInStore || filename);
},
createReadStream: function(fileKey, options) {
return ossStore.createReadStream({
Bucket: bucket,
Key: fileKey
}, options);
},
// Comment to documentation: Set options.ContentLength otherwise the
// indirect stream will be used creating extra overhead on the filesystem.
// An easy way if the data is not transformed is to set the
// options.ContentLength = fileObj.size ...
createWriteStream: function(fileKey, options) {
options = options || {};
// We dont support array of aliases
delete options.aliases;
// We dont support contentType
delete options.contentType;
// We dont support metadata use Metadata?
delete options.metadata;
// Set options
var options = FS.Utility.extend({
Bucket: bucket,
Key: folder + fileKey,
ACL: defaultAcl
}, options);
return ossStore.createWriteStream(options);
},
remove: function(fileKey, callback) {
ossStore.deleteObject({
Bucket: bucket,
Key: fileKey
}, function(error) {
callback(error, !error);
});
},
watch: function() {
throw new Error('OSS does not support watch.');
}
});
};