mirror of
https://github.com/skidoodle/albert.lol.git
synced 2025-02-15 06:09:15 +01:00
Code refactoring (s3)
This commit is contained in:
parent
6c649c1c62
commit
4e73740c16
1 changed files with 12 additions and 14 deletions
|
@ -1,7 +1,6 @@
|
|||
import aws, { ConfigurationOptions } from 'aws-sdk';
|
||||
import aws from 'aws-sdk';
|
||||
import { NextApiRequest, NextApiResponse } from 'next';
|
||||
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
aws.config.s3 = ({
|
||||
accessKeyId: process.env.ACCESS_KEY,
|
||||
|
@ -12,19 +11,18 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||
});
|
||||
const s3 = new aws.S3();
|
||||
const params = {
|
||||
Bucket: 'i.albrt.hu'
|
||||
Bucket: process.env.BUCKET!,
|
||||
}
|
||||
const data = await s3.listObjectsV2(params).promise()
|
||||
let totalsize = data.Contents!.reduce((acc, curr) => {
|
||||
return acc + curr.Size! / 1024 / 1024 / 1024
|
||||
}, 0)
|
||||
|
||||
res.setHeader(
|
||||
'Cache-Control',
|
||||
'public, s-maxage=10, stale-while-revalidate=59'
|
||||
)
|
||||
res.status(200).json (JSON.stringify({
|
||||
'objectCount': data.KeyCount,
|
||||
'totalSize': (Math.round(totalsize * 100) / 100)
|
||||
let size = 0;
|
||||
data.Contents!.forEach(item => {
|
||||
size += item.Size! / 1024 / 1024 / 1024;
|
||||
});
|
||||
size = Number(size.toFixed(2));
|
||||
let objects = data.Contents!.length
|
||||
res.setHeader('Cache-Control', 'public, s-maxage=10, stale-while-revalidate=59');
|
||||
res.status(200).json(JSON.stringify({
|
||||
object: objects,
|
||||
size: size
|
||||
}, null, 2))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue