Files
graphql-api-js/src/s3.ts
T

49 lines
1.0 KiB
TypeScript

import {
S3Client,
CopyObjectCommand,
DeleteObjectCommand,
PutObjectCommand,
} from '@aws-sdk/client-s3';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
export const EXPIRES_DEFAULT_SECS = 300;
export const getSignedPutObjectUrl = (
bucket: string,
key: string,
contentType: string,
): Promise<string> => {
const s3 = new S3Client({ region: 'eu-west-1' });
return getSignedUrl(
s3,
new PutObjectCommand({
Bucket: bucket,
Key: key,
ContentType: contentType,
}),
{ expiresIn: EXPIRES_DEFAULT_SECS },
);
};
export const moveS3File = async (
fromBucket: string,
fromKey: string,
toBucket: string,
toKey: string,
): Promise<any> => {
const s3 = new S3Client({ region: 'eu-west-1' });
const copyCmd = new CopyObjectCommand({
Bucket: toBucket,
CopySource: `${fromBucket}/${fromKey}`,
Key: toKey,
});
await s3.send(copyCmd);
const deleteCmd = new DeleteObjectCommand({
Bucket: fromBucket,
Key: fromKey,
});
return s3.send(deleteCmd);
};