aws s3 uploader

This commit is contained in:
tonyrewin 2022-11-24 12:35:05 +03:00
parent 07f3a65c40
commit 866cf3c559
3 changed files with 1027 additions and 2 deletions

View File

@ -32,6 +32,8 @@
"vercel-build": "astro build" "vercel-build": "astro build"
}, },
"dependencies": { "dependencies": {
"@aws-sdk/client-s3": "^3.216.0",
"@aws-sdk/s3-presigned-post": "^3.216.0",
"mailgun.js": "^8.0.2" "mailgun.js": "^8.0.2"
}, },
"devDependencies": { "devDependencies": {

29
src/pages/api/upload.ts Normal file
View File

@ -0,0 +1,29 @@
// pages/api/upload.ts
import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3'
import { createPresignedPost } from '@aws-sdk/s3-presigned-post'
export default async function handler(req, res) {
const s3Client = new S3Client({
region: process.env.S3_REGION,
credentials: {
accessKeyId: process.env.S3_ACCESS_KEY,
secretAccessKey: process.env.S3_SECRET_KEY
}
})
const post = await createPresignedPost(s3Client, {
Bucket: process.env.S3_BUCKET_NAME,
Key: req.query.file,
Fields: {
acl: 'public-read',
'Content-Type': req.query.fileType
},
Expires: 600, // seconds
Conditions: [
['content-length-range', 0, 22 * 1048576] // up to 22 MB
]
})
res.status(200).json(post)
}

998
yarn.lock

File diff suppressed because it is too large Load Diff