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 logger.info('Initialized S3 client %s with region %s.', getEndpoint(), OBJECT_STORAGE.REGION, lTags())
37 // ---------------------------------------------------------------------------
44 // ---------------------------------------------------------------------------
47 function getEndpoint () {
48 if (endpoint) return endpoint
50 const endpointConfig = CONFIG.OBJECT_STORAGE.ENDPOINT
51 endpoint = endpointConfig.startsWith('http://') || endpointConfig.startsWith('https://')
52 ? CONFIG.OBJECT_STORAGE.ENDPOINT
53 : 'https://' + CONFIG.OBJECT_STORAGE.ENDPOINT