Skip to content

Commit c2eacdf

Browse files
Upload attachments through Maester (#56)
1 parent 9853065 commit c2eacdf

File tree

10 files changed

+91
-300
lines changed

10 files changed

+91
-300
lines changed

CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
## 1.2.1 (January 14, 2022)
1+
## 2.0.0 (January 14, 2022)
2+
* Now method `uploadAttachment` from `AttachmentProcessor` saves attachments directly to `Maester`
3+
* Now method `uploadAttachment` from `AttachmentProcessor` accepts additional argument `contentType`
24
* Update dev dependencies
35

46
## 1.2.0 (November 12, 2021)

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,8 @@ Contains functions to transform platform data that contains JSONata expressions
199199
## Attachment Processor
200200
The attachment processor function can be used to store attachments on the platform. It exposes the following functions
201201

202-
- `uploadAttachment(streamContent)`, which will upload an attachment to the platform and return the result and file url
202+
- `uploadAttachment(body, contentType)`, which will upload an attachment to the platform `Maester` storage and return the result object.
203+
Where `body` - attachment content, `contentType` - it's corresponding `Content-Type` value.
203204
- `getAttachment(url, contentType)`, which will retrieve an attachment from `steward` or `maester` storage. To specify the storage - query parameter
204205
`storage_type` must be provided. To get items from `maester` storage - `?storage_type=maester` should added to the `url` argument. By default attachments are retrieved from `steward` storage, so `?storage_type=steward` is not obligated to be added to the `url` argument. `contentType` -
205206
one of [`stream`, `arraybuffer` ]
@@ -210,9 +211,9 @@ Example:
210211
const { AttachmentProcessor } = require('@elastic.io/component-commons-library');
211212

212213
const stream = new Stream();
213-
const result = await new AttachmentProcessor().uploadAttachment(stream);
214+
const result = await new AttachmentProcessor().uploadAttachment(body, 'application/octet-stream');
214215

215-
const storedFileUrl = result.config.url;
216+
const { objectId } = result.data;
216217
```
217218
```javascript
218219
const { AttachmentProcessor } = require('@elastic.io/component-commons-library');

lib/attachment/AttachmentProcessor.ts

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import axios, { AxiosRequestConfig } from 'axios';
22
import { URL } from 'url';
33
import { StorageClient, ObjectStorage } from '@elastic.io/maester-client/dist';
4-
const restNodeClient = require('elasticio-rest-node')();
54

65
export const STORAGE_TYPE_PARAMETER = 'storage_type';
76
export const DEFAULT_STORAGE_TYPE = 'steward';
87
export const MAESTER_OBJECT_ID_ENDPOINT = '/objects/';
9-
const { ELASTICIO_OBJECT_STORAGE_TOKEN = '' , ELASTICIO_OBJECT_STORAGE_URI = '' } = process.env;
8+
const { ELASTICIO_OBJECT_STORAGE_TOKEN = '', ELASTICIO_OBJECT_STORAGE_URI = '' } = process.env;
109
const maesterCreds = { jwtSecret: ELASTICIO_OBJECT_STORAGE_TOKEN, uri: ELASTICIO_OBJECT_STORAGE_URI };
1110
const REQUEST_TIMEOUT = process.env.REQUEST_TIMEOUT ? parseInt(process.env.REQUEST_TIMEOUT, 10) : 10000; // 10s
1211
const REQUEST_MAX_RETRY = process.env.REQUEST_MAX_RETRY ? parseInt(process.env.REQUEST_MAX_RETRY, 10) : 7; // 10s
@@ -33,15 +32,19 @@ export class AttachmentProcessor {
3332
}
3433
}
3534

36-
async uploadAttachment(body) {
37-
const putUrl = await AttachmentProcessor.preparePutUrl();
35+
async uploadAttachment(body, contentType) {
3836
const ax = axios.create();
3937
AttachmentProcessor.addRetryCountInterceptorToAxios(ax);
38+
const url = `${ELASTICIO_OBJECT_STORAGE_URI}${MAESTER_OBJECT_ID_ENDPOINT}`;
4039

4140
const axConfig = {
42-
url: putUrl,
41+
url,
4342
data: body,
44-
method: 'put',
43+
method: 'post',
44+
headers: {
45+
Authorization: `Bearer ${ELASTICIO_OBJECT_STORAGE_TOKEN}`,
46+
'Content-Type': contentType,
47+
},
4548
timeout: REQUEST_TIMEOUT,
4649
retry: REQUEST_MAX_RETRY,
4750
delay: REQUEST_RETRY_DELAY,
@@ -51,11 +54,6 @@ export class AttachmentProcessor {
5154
return ax(axConfig);
5255
}
5356

54-
static async preparePutUrl() {
55-
const signedUrl = await restNodeClient.resources.storage.createSignedUrl();
56-
return signedUrl.put_url;
57-
}
58-
5957
static async getStewardAttachment(axConfig) {
6058
const ax = axios.create();
6159
AttachmentProcessor.addRetryCountInterceptorToAxios(ax);

lib/platformApi/PlatformApiLogicClient.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export class PlatformApiLogicClient extends PlatformApiRestClient {
9191
*/
9292
async fetchAllSecretsForWorkspace(options: any = {}) {
9393
const { workspaceId } = options;
94-
if (!workspaceId) throw new Error(`workspaceId not provided, can't fetch secrets`)
94+
if (!workspaceId) throw new Error("workspaceId not provided, can't fetch secrets");
9595
const secrets = await this.makeRequest({ method: 'GET', url: `/workspaces/${workspaceId}/secrets` });
9696
const resp: any = [];
9797

@@ -107,7 +107,7 @@ export class PlatformApiLogicClient extends PlatformApiRestClient {
107107
componentIds = clientResponse.data.relationships.components.data.map(x => x.id);
108108
}
109109
} catch (e: any) {
110-
this.emitter.logger.info(`Can't find related to secret component - ${e.message}`)
110+
this.emitter.logger.info(`Can't find related to secret component - ${e.message}`);
111111
}
112112
resp.push({ secretId, secretName, componentIds });
113113
}

0 commit comments

Comments
 (0)