File size: 1,074 Bytes
26c4b30
 
 
 
f665131
5b99fc6
26c4b30
 
 
 
 
1a37272
 
 
 
 
 
 
 
26c4b30
1a37272
26c4b30
 
 
 
 
 
 
 
 
 
1a37272
 
 
9ead915
1a37272
 
26c4b30
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
import { createPresignedPost } from '@aws-sdk/s3-presigned-post';
import { S3Client } from '@aws-sdk/client-s3';
import { fromEnv } from '@aws-sdk/credential-providers';

const FILE_SIZE_LIMIT = 104857600; // 100MB

const s3Client = new S3Client({
  region: process.env.AWS_REGION,
  credentials: fromEnv(),
});

export const getPresignedUrl = async (
  fileName: string,
  fileType: string,
  id: string,
  user: string,
) => {
  const signedFileName = `${user}/${id}/${fileName}`;
  const res = await createPresignedPost(s3Client, {
    Bucket: process.env.AWS_BUCKET_NAME ?? 'vision-agent-dev',
    Key: signedFileName,
    Conditions: [
      ['content-length-range', 0, FILE_SIZE_LIMIT],
      ['starts-with', '$Content-Type', fileType],
    ],
    Fields: {
      acl: 'public-read',
      'Content-Type': fileType,
    },
    Expires: 600,
  });
  return {
    id,
    signedUrl: res.url,
    publicUrl: `https://${process.env.AWS_BUCKET_NAME}.s3.${process.env.AWS_REGION}.amazonaws.com/${user}/${id}/${encodeURIComponent(fileName)}`,
    fields: res.fields,
  };
};