aboutsummaryrefslogtreecommitdiff
path: root/services/docker/mail-server/aws-sendmail/aws/context.ts
blob: 65c83713905a437b23129377d0f629f6a0b8a936 (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 { getConfig } from "../config.ts";

export class AwsContext {
  readonly region = "ap-southeast-1";

  accessKeyId = getConfig().getValue("awsAccessKeyId");
  secretAccessKey = getConfig().getValue("awsSecretAccessKey");

  getCredentials() {
    const { accessKeyId, secretAccessKey } = this;
    return Promise.resolve({ accessKeyId, secretAccessKey });
  }

  readonly credentials = this.getCredentials.bind(this);
}

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

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