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

34 lines
1 KiB
TypeScript
Raw Normal View History

2023-01-13 05:40:33 +01:00
import { URL } from "node:url";
import S3 from "aws-sdk/clients/s3.js";
import type { Meta } from "@/models/entities/meta.js";
import { getAgentByUrl } from "@/misc/fetch.js";
export function getS3(meta: Meta) {
2023-01-13 05:40:33 +01:00
const u =
meta.objectStorageEndpoint != null
? `${meta.objectStorageUseSSL ? "https://" : "http://"}${
meta.objectStorageEndpoint
}`
: `${meta.objectStorageUseSSL ? "https://" : "http://"}example.net`;
try {
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: getAgentByUrl(new URL(u), !meta.objectStorageUseProxy),
},
});
} catch (e) {
2023-07-28 20:35:58 +02:00
throw new Error(
`Failed to construct S3 client, assembled S3 URL: ${u}\n${e}`,
);
}
}