-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paths3.js
128 lines (110 loc) · 3.04 KB
/
s3.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
'use strict';
const assert = require('assert');
const AWS = require('aws-sdk');
const uuid = require('uuid');
const AwsDriverError = require('./aws-driver-error');
const Driver = require('./driver');
class S3Driver extends Driver {
constructor(options) {
const clientOpts = {
apiVersion: '2006-03-01',
signatureVersion: 'v4',
};
super(AWS.S3, clientOpts, options);
}
getSignedUrl(context, { operation, parameters, urlTTL }) {
assert(context, 'missing context');
assert(operation, 'missing operation');
assert(urlTTL !== undefined, 'missing urlTTL');
const params = {
Expires: urlTTL,
...parameters,
};
this._logger.log(context, { params, operation }, 'generating signed URL');
return new Promise((resolve, reject) => {
this._client.getSignedUrl(operation, params, (err, res) => {
if (err) {
reject(err);
} else {
resolve(res);
}
});
})
.catch(err => {
const message = 'failed to generate signed URL';
this._logger.log(context, { err, params, operation }, message);
throw new AwsDriverError({
cause: err,
details: {
params,
operation,
},
message,
});
});
}
putObject(context, { bucket, body, key }, params) {
assert(context, 'missing context');
assert(bucket, 'missing bucket');
assert(body, 'missing body');
const putParams = {
...params,
Body: body,
Bucket: bucket,
Key: key || uuid.v4(),
};
const { Body, ...otherParams } = putParams; // eslint-disable-line no-unused-vars
this._logger.log(context, otherParams, 'storing object in S3');
return this._client
.putObject(putParams)
.promise()
.catch(err => {
const message = 'failed to store object in S3';
this._logger.log(context, {
err,
...otherParams,
}, message);
throw new AwsDriverError({
cause: err,
details: putParams,
message,
});
});
}
upload(context, { bucket, body, key, options }, params) {
assert(context, 'missing context');
assert(bucket, 'missing bucket');
assert(body, 'missing body');
const uploadParams = {
...params,
Body: body,
Bucket: bucket,
Key: key || uuid.v4(),
};
const { Body, ...otherParams } = uploadParams; // eslint-disable-line no-unused-vars
this._logger.log(context, {
params: otherParams,
options,
}, 'uploading object to S3');
return this._client
.upload(uploadParams, options)
.promise()
.catch(err => {
const message = 'failed to upload object to S3';
this._logger.log(context, {
err,
params: otherParams,
options,
}, message);
throw new AwsDriverError({
cause: err,
details: {
params: uploadParams,
options,
},
message,
});
});
}
}
module.exports = S3Driver;