aboutsummaryrefslogtreecommitdiff
path: root/services/docker/mail-server/relay/aws/context.ts
blob: c4f67bccba295e3b4030a86f76687b6b8511e3d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import {
  CopyObjectCommand,
  DeleteObjectCommand,
  S3Client,
} from "@aws-sdk/client-s3";
import { FetchHttpHandler } from "@smithy/fetch-http-handler";

import { getConfigValue } from "../config.ts";

export class AwsContext {
  readonly credentials = () =>
    Promise.resolve({
      accessKeyId: getConfigValue("awsAccessKeyId"),
      secretAccessKey: getConfigValue("awsSecretAccessKey"),
    });
  readonly requestHandler = new FetchHttpHandler();

  get region() {
    return getConfigValue("awsRegion");
  }
}

export async function s3MoveObject(
  client: S3Client,
  bucket: string,
  path: string,
  newPath: string,
): Promise<void> {
  const copyCommand = new CopyObjectCommand({
    Bucket: bucket,
    Key: newPath,
    CopySource: `${bucket}/${path}`,
  });
  await client.send(copyCommand);

  const deleteCommand = new DeleteObjectCommand({
    Bucket: bucket,
    Key: path,
  });
  await client.send(deleteCommand);
}