Last active
November 6, 2021 13:10
-
-
Save ashishtilara/81df2c368df20e31bfb010ab11d0460c to your computer and use it in GitHub Desktop.
node.js: S3 getSignedUrl and createPresignedPost example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const AWS = require('aws-sdk'); | |
const FormData = require('form-data'); | |
const BUCKET = 'ashish-personal-bucket'; | |
const REGION = 'ap-southeast-2'; | |
const fileName = 'record.json' | |
const fileContent = `{"dt":"${new Date().toISOString()}"}` | |
const fileContentType = 'application/json'; | |
const s3 = new AWS.S3({ apiVersion: '2006-03-01', region: REGION }); | |
function getPostUrl(key, contentType) { | |
return new Promise((resolve, reject) => { | |
const params = { | |
Bucket: BUCKET, | |
Fields: { | |
key, | |
}, | |
Expires: 100, | |
Conditions: [ | |
['content-length-range', 0, 10000000], // 10 Mb | |
['eq', '$Content-Type', contentType], | |
] | |
}; | |
s3.createPresignedPost(params, (err, data) => { | |
if (err) { | |
reject(err); | |
return; | |
} | |
data.fields['content-type'] = contentType; | |
resolve(data); | |
}); | |
}); | |
} | |
(async () => { | |
const signed = await getPostUrl(fileName, fileContentType); | |
const form = new FormData(); | |
Object.keys(signed.fields).forEach(key => form.append(key, signed.fields[key])); | |
form.append('file', Buffer.from(fileContent, 'utf-8'), { | |
contentType: fileContentType | |
}); | |
form.submit(signed.url, (err, res) => { | |
if (err) { | |
console.log(err); | |
return; | |
} | |
console.log('Done', res.statusCode, res.statusMessage); | |
}); | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const AWS = require('aws-sdk'); | |
const axios = require('axios'); | |
const BUCKET = 'my-bucket'; | |
const REGION = 'ap-southeast-2'; | |
const fileName = 'record.json' | |
const fileContent = `{"dt":"${new Date().toISOString()}"}` | |
const fileContentType = 'application/json'; | |
const s3 = new AWS.S3({ apiVersion: '2006-03-01', region: REGION }); | |
(async () => { | |
try { | |
const presignedS3Url = s3.getSignedUrl('putObject', { | |
Bucket: BUCKET, | |
Key: fileName, | |
ContentType: fileContentType, | |
}); | |
const axiosResponse = await axios.put(presignedS3Url, fileContent, { headers: { 'Content-Type': fileContentType } }); | |
console.info(axiosResponse) | |
} catch (e) { | |
console.error(e) | |
} | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"AllowedHeaders": [ | |
"*" | |
], | |
"AllowedMethods": [ | |
"PUT", | |
"POST" | |
], | |
"AllowedOrigins": [ | |
"*" | |
], | |
"ExposeHeaders": [] | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment