-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paths3-presign-upload.js
100 lines (82 loc) · 2.41 KB
/
s3-presign-upload.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
import AWS from 'aws-sdk'
import fs from 'fs'
import fetch from 'node-fetch'
const createS3Client = () => {
const s3Config = {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
region: process.env.AWS_S3_REGION,
maxRetries: 5,
sslEnabled: true,
correctClockSkew: true,
logger: console.log,
apiVersion: 'latest'
}
if (process.env.AWS_S3_SIGNATURE_VERSION) {
s3Config.signatureVersion = process.env.AWS_S3_SIGNATURE_VERSION
}
return new AWS.S3(s3Config)
}
const client = createS3Client()
const dataFile = fs.readFileSync('./e2e-1.jpg',
{flag:'r'});
const paramsFile = {
Bucket: process.env.AWS_S3_BUCKET,
Key: `s3-e2e-${1* new Date()}`,
Body: Buffer.from(dataFile, 'binary'),
}
// Create the an S3 secure url to upload files in S3. Limit the TTL of url to 10 seconds
const createSignedUploadUrl = async () => {
try {
const data = await client.getSignedUrl('putObject', {
Bucket: paramsFile.Bucket,
Key: paramsFile.Key,
Expires: 10
});
console.log(`URL to upload to.\n (Valid for 10 Seconds:)\n` , data)
return data
} catch(e) {
console.log('Error generating upload url', e)
}
}
// grap a file content and upload it to the signed url
const runUploadFile = async (signedUploadUrl) => {
if (!signedUploadUrl) {
console.log("\nPlease provide a signed url to upload")
}
try {
const response = await fetch(signedUploadUrl, {
method: 'PUT',
body: paramsFile.Body,
headers: {'Content-Type': 'image/jpeg'} // this needs the correct mime-type of the file
});
if (response.status === 200) {
console.log("\nFile uploaded successfully")
}
} catch(e) {
console.log('\nError uploading the file\n', e)
}
}
// with object in the S3, generate a secure link to download the file
const getSignedDownloadUrl = async (key) => {
if (!key) {
console.log('\nPlease provide a object key')
return null
}
try {
const signedAsset = client.getSignedUrl('getObject', {
Bucket: paramsFile.Bucket,
Key: key,
Expires: 60 * 60 * 1,
})
console.log(`\n${key} with signed url valid 1 hour:\n` , signedAsset)
} catch(e) {
console.log('\nError Getting the download sign url', e)
}
}
const run = async () => {
const signedUpload = await createSignedUploadUrl()
await runUploadFile(signedUpload)
await getSignedDownloadUrl(paramsFile.Key)
}
run()