1 import { S3Client } from '@aws-sdk/client-s3'
2 import { logger } from '@server/helpers/logger'
3 import { CONFIG } from '@server/initializers/config'
4 import { lTags } from './logger'
6 let endpointParsed: URL
7 function getEndpointParsed () {
8 if (endpointParsed) return endpointParsed
10 endpointParsed = new URL(getEndpoint())
15 let s3Client: S3Client
16 function getClient () {
17 if (s3Client) return s3Client
19 const OBJECT_STORAGE = CONFIG.OBJECT_STORAGE
21 s3Client = new S3Client({
22 endpoint: getEndpoint(),
23 region: OBJECT_STORAGE.REGION,
24 credentials: OBJECT_STORAGE.CREDENTIALS.ACCESS_KEY_ID
26 accessKeyId: OBJECT_STORAGE.CREDENTIALS.ACCESS_KEY_ID,
27 secretAccessKey: OBJECT_STORAGE.CREDENTIALS.SECRET_ACCESS_KEY
32 // FIXME: https://github.com/aws/aws-sdk-js-v3/issues/2445 workaround
33 s3Client.middlewareStack.add(
34 (next, _context) => (args: any) => {
35 if (typeof args.request?.body === 'string' && args.request.body.includes('CompletedMultipartUpload')) {
36 args.request.body = args.request.body.replace(/CompletedMultipartUpload/g, 'CompleteMultipartUpload')
46 logger.info('Initialized S3 client %s with region %s.', getEndpoint(), OBJECT_STORAGE.REGION, lTags())
51 // ---------------------------------------------------------------------------
58 // ---------------------------------------------------------------------------
61 function getEndpoint () {
62 if (endpoint) return endpoint
64 const endpointConfig = CONFIG.OBJECT_STORAGE.ENDPOINT
65 endpoint = endpointConfig.startsWith('http://') || endpointConfig.startsWith('https://')
66 ? CONFIG.OBJECT_STORAGE.ENDPOINT
67 : 'https://' + CONFIG.OBJECT_STORAGE.ENDPOINT