|
6 | 6 | * disclosure restricted by GSA ADP Schedule Contract with IBM Corp. |
7 | 7 | ******************************************************************************/ |
8 | 8 |
|
9 | | -const ibm = require("ibm-cos-sdk"); |
| 9 | +const { ContainerAuthenticator } = require("ibm-cloud-sdk-core"); |
| 10 | +const { Readable } = require('node:stream'); |
10 | 11 |
|
| 12 | +const responseToReadable = (response) => { |
| 13 | + const reader = response.body.getReader(); |
| 14 | + const rs = new Readable(); |
| 15 | + rs._read = async () => { |
| 16 | + const result = await reader.read(); |
| 17 | + if (!result.done) { |
| 18 | + rs.push(Buffer.from(result.value)); |
| 19 | + } else { |
| 20 | + rs.push(null); |
| 21 | + return; |
| 22 | + } |
| 23 | + }; |
| 24 | + return rs; |
| 25 | +}; |
11 | 26 | class CosService { |
12 | | - cos; |
13 | 27 | config; |
| 28 | + authenticator; |
14 | 29 |
|
15 | 30 | constructor(config) { |
16 | 31 | const fn = "constructor"; |
17 | 32 | this.config = config; |
18 | | - this.cos = new ibm.S3(config); |
19 | | - console.debug( |
20 | | - `${fn}- initialized! instance: '${config.serviceInstanceId}'` |
21 | | - ); |
22 | | - } |
23 | 33 |
|
24 | | - getServiceInstanceId() { |
25 | | - return this.config.serviceInstanceId; |
| 34 | + // create an authenticator based on a trusted profile |
| 35 | + this.authenticator = new ContainerAuthenticator({ |
| 36 | + iamProfileName: config.trustedProfileName, |
| 37 | + }); |
| 38 | + console.log( |
| 39 | + `CosService init - region: '${this.config.cosRegion}', bucket: ${this.config.cosBucket}, trustedProfileName: '${this.config.trustedProfileName}'` |
| 40 | + ); |
26 | 41 | } |
27 | 42 |
|
28 | 43 | getContentTypeFromFileName(fileName) { |
@@ -60,61 +75,64 @@ class CosService { |
60 | 75 | /** |
61 | 76 | * https://ibm.github.io/ibm-cos-sdk-js/AWS/S3.html#putObject-property |
62 | 77 | */ |
63 | | - createObject(bucket, id, dataToUpload, mimeType, contentLength) { |
| 78 | + async createObject(id, dataToUpload, mimeType, contentLength) { |
64 | 79 | const fn = "createObject "; |
65 | 80 | console.debug(`${fn}> id: '${id}', mimeType: '${mimeType}', contentLength: '${contentLength}'`); |
66 | 81 |
|
67 | | - return this.cos |
68 | | - .putObject({ |
69 | | - Bucket: bucket, |
70 | | - Key: id, |
71 | | - Body: dataToUpload, |
72 | | - ContentType: mimeType, |
73 | | - ContentLength: contentLength, |
74 | | - }) |
75 | | - .promise() |
76 | | - .then((obj) => { |
77 | | - console.debug(`${fn}< done`); |
78 | | - return true; |
79 | | - }) |
80 | | - .catch((err) => { |
81 | | - console.error(err); |
82 | | - console.debug(`${fn}< failed`); |
83 | | - throw err; |
84 | | - }); |
85 | | - } |
| 82 | + // prepare the request to create the object files in the bucket |
| 83 | + const requestOptions = { |
| 84 | + method: "PUT", |
| 85 | + body: dataToUpload, |
| 86 | + headers: { |
| 87 | + "Content-Type": mimeType, |
| 88 | + "Content-Length": contentLength, |
| 89 | + }, |
| 90 | + }; |
86 | 91 |
|
87 | | - /** |
88 | | - * https://cloud.ibm.com/docs/cloud-object-storage?topic=cloud-object-storage-node#node-examples-list-objects |
89 | | - */ |
90 | | - getBucketContents(bucketName, prefix) { |
91 | | - const fn = "getBucketContents "; |
92 | | - console.debug(`${fn}> bucket: '${bucketName}', prefix: '${prefix}'`); |
93 | | - return this.cos |
94 | | - .listObjects({ Bucket: bucketName, Prefix: prefix }) |
95 | | - .promise() |
96 | | - .then((data) => { |
97 | | - console.debug(`${fn}< done`); |
98 | | - if (data != null && data.Contents != null) { |
99 | | - return data.Contents; |
100 | | - } |
101 | | - }) |
102 | | - .catch((err) => { |
103 | | - console.error(err); |
104 | | - console.debug(`${fn}< failed`); |
105 | | - return undefined; |
106 | | - }); |
| 92 | + // authenticate the request |
| 93 | + await this.authenticator.authenticate(requestOptions); |
| 94 | + |
| 95 | + // perform the request |
| 96 | + const response = await fetch( |
| 97 | + `https://s3.direct.${this.config.cosRegion}.cloud-object-storage.appdomain.cloud/${this.config.cosBucket}/${id}`, |
| 98 | + requestOptions |
| 99 | + ); |
| 100 | + |
| 101 | + if (response.status !== 200) { |
| 102 | + console.error(`Unexpected status code: ${response.status}`); |
| 103 | + throw new Error(`Failed to upload image: '${response.status}'`); |
| 104 | + } |
| 105 | + return; |
107 | 106 | } |
108 | 107 |
|
109 | 108 | /** |
110 | 109 | * https://ibm.github.io/ibm-cos-sdk-js/AWS/S3.html#getObject-property |
111 | 110 | * @param id |
112 | 111 | */ |
113 | | - getObjectAsStream(bucket, id) { |
| 112 | + async getObjectAsStream(id) { |
114 | 113 | const fn = "getObjectAsStream "; |
115 | 114 | console.debug(`${fn}> id: '${id}'`); |
116 | 115 |
|
117 | | - return this.cos.getObject({ Bucket: bucket, Key: id }).createReadStream(); |
| 116 | + // prepare the request to list the files in the bucket |
| 117 | + const requestOptions = { |
| 118 | + method: "GET", |
| 119 | + }; |
| 120 | + |
| 121 | + // authenticate the request |
| 122 | + await this.authenticator.authenticate(requestOptions); |
| 123 | + |
| 124 | + // perform the request |
| 125 | + return fetch( |
| 126 | + `https://s3.direct.${this.config.cosRegion}.cloud-object-storage.appdomain.cloud/${this.config.cosBucket}/${id}`, |
| 127 | + requestOptions |
| 128 | + ).then((response) => { |
| 129 | + if (!response.ok) { |
| 130 | + console.error(`${fn}< HTTP error, status = ${response.status}`); |
| 131 | + throw new Error(`HTTP error, status = ${response.status}`); |
| 132 | + } |
| 133 | + console.debug(`${fn}< receiving response as readable stream`); |
| 134 | + return responseToReadable(response); |
| 135 | + }); |
118 | 136 | } |
119 | 137 | } |
120 | 138 |
|
|
0 commit comments