2022-09-17 18:27:08 +00:00
|
|
|
import { URL } from 'node:url';
|
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import S3 from 'aws-sdk/clients/s3.js';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-09-20 20:33:11 +00:00
|
|
|
import type { Config } from '@/config.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import type { Meta } from '@/models/entities/Meta.js';
|
2022-12-04 01:16:03 +00:00
|
|
|
import { HttpRequestService } from '@/core/HttpRequestService.js';
|
2022-12-04 06:03:09 +00:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class S3Service {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.config)
|
|
|
|
private config: Config,
|
|
|
|
|
|
|
|
private httpRequestService: HttpRequestService,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public getS3(meta: Meta) {
|
2023-03-13 00:45:21 +00:00
|
|
|
const u = meta.objectStorageEndpoint
|
2022-09-17 18:27:08 +00:00
|
|
|
? `${meta.objectStorageUseSSL ? 'https://' : 'http://'}${meta.objectStorageEndpoint}`
|
|
|
|
: `${meta.objectStorageUseSSL ? 'https://' : 'http://'}example.net`;
|
2023-03-13 00:45:21 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
return new S3({
|
2023-03-13 00:45:21 +00:00
|
|
|
endpoint: meta.objectStorageEndpoint && meta.objectStorageEndpoint.length > 0
|
|
|
|
? meta.objectStorageEndpoint
|
|
|
|
: undefined,
|
2022-09-17 18:27:08 +00:00
|
|
|
accessKeyId: meta.objectStorageAccessKey!,
|
|
|
|
secretAccessKey: meta.objectStorageSecretKey!,
|
|
|
|
region: meta.objectStorageRegion ?? undefined,
|
|
|
|
sslEnabled: meta.objectStorageUseSSL,
|
|
|
|
s3ForcePathStyle: !meta.objectStorageEndpoint // AWS with endPoint omitted
|
|
|
|
? false
|
|
|
|
: meta.objectStorageS3ForcePathStyle,
|
|
|
|
httpOptions: {
|
2023-01-25 03:00:04 +00:00
|
|
|
agent: this.httpRequestService.getAgentByUrl(new URL(u), !meta.objectStorageUseProxy),
|
2022-09-17 18:27:08 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|