Repository navigation
Expand file tree
/
Copy paths3.js
More file actions
71 lines (60 loc) · 1.63 KB
/
Copy paths3.js
File metadata and controls
71 lines (60 loc) · 1.63 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const {
S3Client,
PutObjectCommand,
GetObjectCommand,
ListObjectsCommand,
} = require("@aws-sdk/client-s3");
const { getSignedUrl } = require("@aws-sdk/s3-request-presigner");
require("dotenv").config();
const fs = require("fs");
const client = new S3Client({
region: process.env.AWS_BUCKET_REGION,
credentials: {
accessKeyId: process.env.AWS_PUBLIC_KEY,
secretAccessKey: process.env.AWS_SECRET_KEY,
},
});
async function uploadFile(filepath, filename) {
const stream = fs.createReadStream(filepath);
const uploadParams = {
Bucket: process.env.AWS_BUCKET_NAME,
Key: `${filename}`,
Body: stream,
};
const putCommand = new PutObjectCommand(uploadParams);
const putResult = await client.send(putCommand);
const downloadParams = {
Bucket: process.env.AWS_BUCKET_NAME,
Key: `${filename}`,
};
const getCommand = new GetObjectCommand(downloadParams);
const downloadUrl = await getSignedUrl(client, getCommand, {
expiresIn: 300,
});
return { putResult, downloadUrl, key: `${filename}` };
}
async function getFiles() {
const command = new ListObjectsCommand({
Bucket: process.env.AWS_BUCKET_NAME,
});
return await client.send(command);
}
async function getFileDownload(key) {
if (!key) {
return { error: "Missing filename" };
}
const downloadParams = {
Bucket: process.env.AWS_BUCKET_NAME,
Key: key,
};
const getCommand = new GetObjectCommand(downloadParams);
const downloadUrl = await getSignedUrl(client, getCommand, {
expiresIn: 300,
});
return { key: key, downloadUrl: downloadUrl };
}
module.exports = {
uploadFile,
getFiles,
getFileDownload,
};