vision-agent / lib /aws.ts
wuyiqun0718's picture
feat: increase file size limit
9bda098
raw
history blame
1.08 kB
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 = 20971520; // 20MB
const s3Client = new S3Client({
region: process.env.AWS_REGION,
credentials: fromEnv(),
});
export const upload = async (base64: string, fileName: string, fileType: string) => {
const { url, fields } = await createPresignedPost(s3Client, {
Bucket: process.env.AWS_BUCKET_NAME ?? 'vision-agent-dev',
Key: fileName,
Conditions: [
['content-length-range', 0, FILE_SIZE_LIMIT],
['starts-with', '$Content-Type', fileType],
],
Fields: {
acl: 'public-read',
'Content-Type': fileType,
},
Expires: 600,
});
const formData = new FormData();
Object.entries(fields).forEach(([key, value]) => {
formData.append(key, value as string);
});
const res = await fetch(base64);
const blob = await res.blob();
formData.append('file', blob);
return fetch(url, {
method: 'POST',
body: formData,
});
};