]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/object-storage/shared/client.ts
85682c4926e397c877764e52e29b79c98dadf9b0
[github/Chocobozzz/PeerTube.git] / server / lib / object-storage / shared / client.ts
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'
5
6 let endpointParsed: URL
7 function getEndpointParsed () {
8 if (endpointParsed) return endpointParsed
9
10 endpointParsed = new URL(getEndpoint())
11
12 return endpointParsed
13 }
14
15 let s3Client: S3Client
16 function getClient () {
17 if (s3Client) return s3Client
18
19 const OBJECT_STORAGE = CONFIG.OBJECT_STORAGE
20
21 s3Client = new S3Client({
22 endpoint: getEndpoint(),
23 region: OBJECT_STORAGE.REGION,
24 credentials: OBJECT_STORAGE.CREDENTIALS.ACCESS_KEY_ID
25 ? {
26 accessKeyId: OBJECT_STORAGE.CREDENTIALS.ACCESS_KEY_ID,
27 secretAccessKey: OBJECT_STORAGE.CREDENTIALS.SECRET_ACCESS_KEY
28 }
29 : undefined
30 })
31
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')
37 }
38 return next(args)
39 },
40 {
41 step: 'build',
42 priority: 'high'
43 }
44 )
45
46 logger.info('Initialized S3 client %s with region %s.', getEndpoint(), OBJECT_STORAGE.REGION, lTags())
47
48 return s3Client
49 }
50
51 // ---------------------------------------------------------------------------
52
53 export {
54 getEndpointParsed,
55 getClient
56 }
57
58 // ---------------------------------------------------------------------------
59
60 let endpoint: string
61 function getEndpoint () {
62 if (endpoint) return endpoint
63
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
68
69 return endpoint
70 }