2021-08-17 08:26:20 +02:00
|
|
|
import { S3Client } from '@aws-sdk/client-s3'
|
2022-05-09 11:49:25 +02:00
|
|
|
import { NodeHttpHandler } from '@aws-sdk/node-http-handler'
|
2021-08-17 08:26:20 +02:00
|
|
|
import { logger } from '@server/helpers/logger'
|
2022-05-09 11:49:25 +02:00
|
|
|
import { isProxyEnabled } from '@server/helpers/proxy'
|
|
|
|
import { getAgent } from '@server/helpers/requests'
|
2021-08-17 08:26:20 +02:00
|
|
|
import { CONFIG } from '@server/initializers/config'
|
|
|
|
import { lTags } from './logger'
|
|
|
|
|
2022-05-09 11:49:25 +02:00
|
|
|
function getProxyRequestHandler () {
|
|
|
|
if (!isProxyEnabled()) return null
|
|
|
|
|
|
|
|
const { agent } = getAgent()
|
|
|
|
|
|
|
|
return new NodeHttpHandler({
|
|
|
|
httpAgent: agent.http,
|
|
|
|
httpsAgent: agent.https
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-17 08:26:20 +02:00
|
|
|
let endpointParsed: URL
|
|
|
|
function getEndpointParsed () {
|
|
|
|
if (endpointParsed) return endpointParsed
|
|
|
|
|
|
|
|
endpointParsed = new URL(getEndpoint())
|
|
|
|
|
|
|
|
return endpointParsed
|
|
|
|
}
|
|
|
|
|
|
|
|
let s3Client: S3Client
|
|
|
|
function getClient () {
|
|
|
|
if (s3Client) return s3Client
|
|
|
|
|
|
|
|
const OBJECT_STORAGE = CONFIG.OBJECT_STORAGE
|
|
|
|
|
|
|
|
s3Client = new S3Client({
|
|
|
|
endpoint: getEndpoint(),
|
|
|
|
region: OBJECT_STORAGE.REGION,
|
|
|
|
credentials: OBJECT_STORAGE.CREDENTIALS.ACCESS_KEY_ID
|
|
|
|
? {
|
|
|
|
accessKeyId: OBJECT_STORAGE.CREDENTIALS.ACCESS_KEY_ID,
|
|
|
|
secretAccessKey: OBJECT_STORAGE.CREDENTIALS.SECRET_ACCESS_KEY
|
|
|
|
}
|
2022-05-09 11:49:25 +02:00
|
|
|
: undefined,
|
|
|
|
requestHandler: getProxyRequestHandler()
|
2021-08-17 08:26:20 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
logger.info('Initialized S3 client %s with region %s.', getEndpoint(), OBJECT_STORAGE.REGION, lTags())
|
|
|
|
|
|
|
|
return s3Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
getEndpointParsed,
|
|
|
|
getClient
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
let endpoint: string
|
|
|
|
function getEndpoint () {
|
|
|
|
if (endpoint) return endpoint
|
|
|
|
|
|
|
|
const endpointConfig = CONFIG.OBJECT_STORAGE.ENDPOINT
|
|
|
|
endpoint = endpointConfig.startsWith('http://') || endpointConfig.startsWith('https://')
|
|
|
|
? CONFIG.OBJECT_STORAGE.ENDPOINT
|
|
|
|
: 'https://' + CONFIG.OBJECT_STORAGE.ENDPOINT
|
|
|
|
|
|
|
|
return endpoint
|
|
|
|
}
|