Spaces:
Sleeping
Sleeping
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/${signedFileName}`, | |
fields: res.fields, | |
}; | |
}; | |