paricafe/packages/backend/src/core/S3Service.ts

41 lines
1.3 KiB
TypeScript
Raw Normal View History

2022-09-17 13:27:08 -05: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 15:33:11 -05:00
import type { Config } from '@/config.js';
2022-09-17 13:27:08 -05:00
import type { Meta } from '@/models/entities/Meta.js';
2022-12-03 19:16:03 -06:00
import { HttpRequestService } from '@/core/HttpRequestService.js';
import { bindThis } from '@/decorators.js';
2022-09-17 13:27:08 -05:00
@Injectable()
export class S3Service {
constructor(
@Inject(DI.config)
private config: Config,
private httpRequestService: HttpRequestService,
) {
}
@bindThis
2022-09-17 13:27:08 -05:00
public getS3(meta: Meta) {
const u = meta.objectStorageEndpoint != null
? `${meta.objectStorageUseSSL ? 'https://' : 'http://'}${meta.objectStorageEndpoint}`
: `${meta.objectStorageUseSSL ? 'https://' : 'http://'}example.net`;
return new S3({
endpoint: meta.objectStorageEndpoint ?? undefined,
accessKeyId: meta.objectStorageAccessKey!,
secretAccessKey: meta.objectStorageSecretKey!,
region: meta.objectStorageRegion ?? undefined,
sslEnabled: meta.objectStorageUseSSL,
s3ForcePathStyle: !meta.objectStorageEndpoint // AWS with endPoint omitted
? false
: meta.objectStorageS3ForcePathStyle,
httpOptions: {
agent: this.httpRequestService.getAgentByUrl(new URL(u), !meta.objectStorageUseProxy),
2022-09-17 13:27:08 -05:00
},
});
}
}