FoundKey/packages/backend/src/services/drive/s3.ts

25 lines
895 B
TypeScript
Raw Normal View History

import { URL } from 'node:url';
2022-02-28 18:34:40 +00:00
import S3 from 'aws-sdk/clients/s3.js';
import { Meta } from '@/models/entities/meta.js';
import { getAgentByUrl } from '@/misc/fetch.js';
2022-10-16 22:20:20 +00:00
export function getS3(meta: Meta): S3 {
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: {
2021-12-09 14:58:30 +00:00
agent: getAgentByUrl(new URL(u), !meta.objectStorageUseProxy),
},
});
}