-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3-modules.js
38 lines (32 loc) · 961 Bytes
/
s3-modules.js
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
import env from "dotenv";
import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3";
import { randomBytes } from "crypto";
env.config();
const bucketName= process.env.AWS_BUCKET_NAME;
const bucketRegion = process.env.AWS_BUCKET_REGION;
const accessKey = process.env.AWS_ACCESS_KEY;
const secretAccessKey = process.env.AWS_SECRET_ACCESS_KEY;
const s3 = new S3Client({
credentials: {
accessKeyId: accessKey,
secretAccessKey: secretAccessKey,
},
region: bucketRegion,
});
async function generateRandomHash() {
const rawBytes = await randomBytes(16);
const hash = rawBytes.toString("hex");
return hash;
}
export async function uploadToS3(file) {
const uniquehash= generateRandomHash();
const params = {
Bucket: bucketName,
Key: uniquehash,
Body: file.buffer,
ContentType: file.mimetype,
};
const command = new PutObjectCommand(params);
await s3.send(command);
console.log(`File ${params.Key} was uploaded`);
}