forked from parse-community/parse-server-s3-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
118 lines (104 loc) · 3.62 KB
/
index.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
'use strict';
// COS Adapter
//
// Stores Parse files in Tencent Cloud COS.
// var AWS = require('aws-sdk');
var COS = require('cos-nodejs-sdk-v5');
var optionsFromArguments = require('./lib/optionsFromArguments');
var fs = require('fs');
// Creates an COS session.
// Providing Tencent Cloud access, secret keys and bucket are mandatory
// Region will use same defaults if omitted
function COSAdapter() {
var options = optionsFromArguments(arguments);
this._Region = options.Region;
this._Bucket = options.Bucket;
// this._bucketPrefix = options.bucketPrefix;
// this._directAccess = options.directAccess;
// this._baseUrl = options.baseUrl;
// this._baseUrlDirect = options.baseUrlDirect;
// this._signatureVersion = options.signatureVersion;
this._globalCacheControl = options.globalCacheControl;
// this._encryption = options.ServerSideEncryption;
let COSOptions = {
params: { Bucket: this._Bucket },
region: this._Region,
signatureVersion: this._signatureVersion,
globalCacheControl: this._globalCacheControl
};
if (options.SecretId && options.SecretKey) {
COSOptions.SecretId = options.SecretId;
COSOptions.SecretKey = options.SecretKey;
}
// Object.assign(COSOptions, options.s3overrides);
this._cosClient = new COS(COSOptions);
// this._s3Client = new AWS.S3(COSOptions);
}
// For a given config object, filename, and data, store a file in S3
// Returns a promise containing the S3 object creation response
COSAdapter.prototype.createFile = function(filename, data, contentType) {
let params = {
Bucket: this._Bucket,
Region: this._Region,
Key: filename,
Body:Buffer.from(data)
};
if (contentType) {
params.ContentType = contentType;
}
if(this._globalCacheControl) {
params.CacheControl = this._globalCacheControl;
}
return new Promise((resolve, reject) => {
this._cosClient.putObject(params, (err, data) => {
if (err !== null) {
return reject(err);
}
resolve(data);
});
});
}
COSAdapter.prototype.deleteFile = function(filename) {
return new Promise((resolve, reject) => {
let params = {
Bucket: this._Bucket,
Region: this._Region,
Key: filename,
};
this._cosClient.deleteObject(params, (err, data) =>{
if(err !== null) {
return reject(err);
}
resolve(data);
});
});
}
// Search for and return a file if found by filename
// Returns a promise that succeeds with the buffer result from S3
COSAdapter.prototype.getFileData = function(filename) {
let params = {
Bucket: this._Bucket,
Region: this._Region,
Key: filename,
};
return new Promise((resolve, reject) => {
this._cosClient.getObject(params, (err, data) => {
if (err !== null) {
return reject(err);
}
// Something happened here...
if (data && !data.Body) {
return reject(data);
}
resolve(data.Body);
});
});
}
// Generates and returns the location of a file stored in S3 for the given request and filename
// The location is the direct S3 link if the option is set, otherwise we serve the file through parse-server
COSAdapter.prototype.getFileLocation = function(config, filename) {
filename = encodeURIComponent(filename);
return (config.mount + '/files/' + config.applicationId + '/' + filename);
}
module.exports = COSAdapter;
module.exports.default = COSAdapter;