-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
98 lines (85 loc) · 2.84 KB
/
index.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
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/// This code snippet demonstrates how to generate a pre-signed URL
/// for an object in an Amazon S3 bucket using the AWS SDK for JavaScript v3.
// For the Private Buckets, we need to generate a pre-signed URL to access the object in the bucket.
import { DeleteObjectCommand, GetObjectCommand, ListObjectsV2Command, PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
import dotenv from 'dotenv';
dotenv.config();
const newClient = new S3Client({
region: process.env.AWS_DEFAULT_REGION,
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
},
});
// Get object function
async function getObject(key) {
try {
const command = new GetObjectCommand({
Bucket: process.env.S3_BUCKET_NAME,
Key: key
});
const url = await getSignedUrl(newClient, command, { expiresIn: 20 });
return url;
} catch (error) {
console.error("Error getting object:", error);
throw error;
}
}
// Put object function
async function putObject(filename, contentType) {
try {
const command = new PutObjectCommand({
Bucket: process.env.S3_BUCKET_NAME,
Key: `putImage/${filename}`,
ContentType: contentType,
});
const url = await getSignedUrl(newClient, command, { expiresIn: 600 });
return url;
} catch (error) {
console.error("Error putting object:", error);
throw error;
}
}
// List objects function
async function listObjects() {
try {
const command = new ListObjectsV2Command({
Bucket: process.env.S3_BUCKET_NAME,
Prefix: "putImage"
});
const result = await newClient.send(command);
console.log("List of objects in the bucket:", result);
} catch (error) {
console.error("Error listing objects:", error);
throw error;
}
}
// Delete object function
async function deleteObject(key) {
try {
const command = new DeleteObjectCommand({
Bucket: process.env.S3_BUCKET_NAME,
Key: key
});
const result = await newClient.send(command);
console.log("Object deleted:", result);
} catch (error) {
console.error("Error deleting object:", error);
throw error;
}
}
// Initialize function
async function init() {
try {
const url = await getObject("putImage/video-1738988554573.mp4");
console.log("URL for newImage.png is:", url);
const putUrl = await putObject(`video-${Date.now()}.mp4`, "video/mp4");
console.log("URL for uploading:", putUrl);
await listObjects();
await deleteObject("newImage.png");
} catch (error) {
console.error("Error in init:", error);
}
}
init();