-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
44 lines (37 loc) · 1.05 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
/**
* Copyright (c) IBM, Corp. and its affiliates.
*
* This source code is licensed under the Apache license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
const ibm = require('ibm-cos-sdk');
const buildUrl = (options, key) => `https://${options.endpoint}/${options.params.Bucket}/${key}`;
module.exports = {
init: providerOptions => {
const cos = new ibm.S3(providerOptions);
return {
async upload(file, customParams = {}) {
await cos
.putObject({
Key: file.hash,
Body: Buffer.from(file.buffer, 'binary'),
ACL: 'public-read',
ContentType: file.mime,
...customParams,
})
.promise();
// eslint-disable-next-line no-param-reassign
file.url = buildUrl(providerOptions, file.hash);
},
async delete(file, customParams = {}) {
return cos
.deleteObject({
Key: file.hash,
...customParams,
})
.promise();
},
};
},
};