Skip to content
Snippets Groups Projects
Verified Commit b44a2764 authored by Konstantin Tsabolov's avatar Konstantin Tsabolov
Browse files

Merge remote-tracking branch 'origin/s3-api' into chore/e2e

parents 964cec86 3d43ae65
No related branches found
No related tags found
1 merge request!40End to end run preparation
......@@ -22,3 +22,5 @@ export * from './modules/health/health.module.js';
export * from './modules/tsa/index.js';
export * from './interceptors/response-format.interceptor.js';
export * from './staticStorage.js';
import axios, { AxiosHeaders } from 'axios';
export type UploadToS3Options = {
s3Url: string;
bucketName: string;
fileId: string;
content: Uint8Array;
accessKey: string;
secret: string;
};
const generateRfc1123Date = () =>
new Date()
.toLocaleString('en-GB', {
timeZone: 'UTC',
hour12: false,
weekday: 'short',
year: 'numeric',
month: 'short',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
})
.replace(/(?:(\d),)/, '$1') + ' GMT';
// Upload to S3 and return the URL to fetch it from
export const uploadToS3 = async ({
s3Url,
content,
fileId,
bucketName,
accessKey,
secret,
}: UploadToS3Options) => {
// TODO: double check all headers
const headers = new AxiosHeaders()
.set('Host', s3Url)
.set('Date', generateRfc1123Date())
.set('Content-Type', 'application/octet-stream')
.set('Authorization', accessKey)
.set('Secret', secret);
const sanitizedUrl = s3Url.endsWith('/')
? s3Url.slice(0, s3Url.length - 1)
: s3Url;
const url = `${sanitizedUrl}/${bucketName}/${fileId}`;
// TODO: check whether we need to include the sig or not
const result = await axios.put(url, content, {
headers,
});
if (result.status > 299) {
throw new Error(`Error uploading to S3. Error: ${JSON.stringify(result)}`);
}
return url;
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment