-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paths3-postsign-upload.js
90 lines (73 loc) · 1.81 KB
/
s3-postsign-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
import AWS from 'aws-sdk'
import fs from 'fs'
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 params = {
Bucket: process.env.AWS_S3_BUCKET,
Key: `test-${1* new Date()}`,
Body: "aaa",
ACL: 'private',
}
const runUpload = async () => {
try {
const data = await client.upload(params).promise()
console.log("Plain Text upload:", data)
} catch(e) {
console.log('Error', e)
}
}
const dataFile = fs.readFileSync('./s3.js',
{encoding:'utf8', flag:'r'});
console.log(dataFile)
const paramsFile = {
Bucket: process.env.AWS_S3_BUCKET,
Key: `s3-${1* new Date()}.js`,
Body: Buffer.from(dataFile, 'binary'),
ACL: 'private',
}
const runUploadWithFile = async () => {
try {
const data = await client.upload(paramsFile).promise()
console.log("FileUpload:", data)
} catch(e) {
console.log('Error', e)
}
}
const runGetSignedUrl = async (key) => {
if (!key) {
console.log('Please provide a object key')
return null
}
try {
const chartAsset = client.getSignedUrl('getObject', {
Bucket: process.env.AWS_S3_BUCKET,
Key: key,
Expires: 60 * 60 * 1,
})
console.log(`\n${key} signed url:` , chartAsset)
} catch(e) {
console.log('Error', e)
}
}
const run = async () => {
await runUpload()
await runGetSignedUrl(params.Key)
await runUploadWithFile()
await runGetSignedUrl(paramsFile.Key)
}
run()