-
Notifications
You must be signed in to change notification settings - Fork 0
/
forge-oss.js
120 lines (98 loc) · 4.12 KB
/
forge-oss.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
const fs = require('fs');
const fetch = require('node-fetch');
const path = require('path');
const mkdirp = require('mkdirp');
const util = require('util');
const streamPipeline = util.promisify(require('stream').pipeline);
module.exports = class ForgeOss {
constructor() {
this.bearer = null;
this.baseUrl = 'https://developer.api.autodesk.com';
}
async auth(client_id, client_secret) {
const params = new URLSearchParams();
params.append('client_id', client_id);
params.append('client_secret', client_secret);
params.append('grant_type', 'client_credentials');
params.append('scope', 'bucket:read data:read bucket:create data:write data:create');
const response = await fetch(`${this.baseUrl}/authentication/v1/authenticate`, {
method: 'post',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: params
});
const responseJson = await response.json();
this.bearer = responseJson.access_token;
}
async getPagedOssItem(urlParam, getItemFunc) {
var nextStartAt = null;
var allItemKeys = [];
do {
const params = new URLSearchParams(nextStartAt);
const url = `${this.baseUrl}/${urlParam}?${params}`;
const response = await fetch(url, {
method: 'get',
headers: {
'Authorization': 'Bearer ' + this.bearer,
'Content-Type': 'application/json'
}
});
const responseJson = await response.json();
const itemKeys = responseJson.items.map(item => getItemFunc(item))
allItemKeys.push(...itemKeys);
nextStartAt = null;
if (responseJson.next !== undefined) {
const nextUrl = new URL(responseJson.next)
nextStartAt = nextUrl.search;
}
} while(nextStartAt !== null);
allItemKeys.sort();
return allItemKeys;
}
async getBuckets() {
return await this.getPagedOssItem(`oss/v2/buckets`, function(item) {return item.bucketKey});
}
async getObjects(bucketKey) {
return await this.getPagedOssItem(`oss/v2/buckets/${bucketKey}/objects`, function (item) {return item.objectKey});
}
async getObject(bucketKey, objectPath, localPath) {
const objectKey = encodeURIComponent(objectPath);
const response = await fetch(`${this.baseUrl}/oss/v2/buckets/${bucketKey}/objects/${objectKey}`, {
method: 'get',
headers: {
'Authorization': 'Bearer ' + this.bearer,
}
});
const dir = path.dirname(localPath);
mkdirp.sync(dir);
const file = fs.createWriteStream(localPath);
await streamPipeline(response.body, file);
}
async putObject(bucketKey, objectPath, localPath) {
const objectKey = encodeURIComponent(objectPath);
const stats = fs.statSync(localPath);
const fileSizeInBytes = stats.size;
let readStream = fs.createReadStream(localPath);
const response = await fetch(`${this.baseUrl}/oss/v2/buckets/${bucketKey}/objects/${objectKey}`, {
method: 'put',
headers: {
'Authorization': 'Bearer ' + this.bearer,
"Content-length": fileSizeInBytes
},
body: readStream
});
const output = await response.text();
}
async createBucket(bucketKey) {
const bodyText = `{ "bucketKey": "${bucketKey}", "policyKey": "persistent"}`;
const response = await fetch(`${this.baseUrl}/oss/v2/buckets`, {
method: 'post',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + this.bearer
},
body: bodyText
});
const responseJson = await response.json();
return responseJson;
}
}