Skip to content

Instantly share code, notes, and snippets.

{
"Version": "2012-10-17",
"Id": "Policy1532447175542",
"Statement": [
{
"Effect": "Allow",
"Principal": "*",
"Action": "s3:putObject",
"Resource": "arn:aws:s3:::YOUR-BUCKET-NAME/*"
}
<form onSubmit={this.submitFile}>
<input
label="Upload file"
type="file"
onChange={this.handleFileUpload}
/>
<button type="submit">Upload</button>
</form>
handleFileUpload = (event) => {
this.setState({ file: event.target.files });
};
submitFile = (event) => {
event.preventDefault();
const formData = new FormData();
formData.append('file', this.state.file[0]);
axios
.post(
const getContentType = (event) => {
const contentType = event.headers['content-type']
if (!contentType) {
return event.headers['Content-Type'];
}
return contentType;
};
const parser = (event) => new Promise((resolve, reject) => {
const busboy = new Busboy({
const uploadFile = (buffer) => new Promise((resolve, reject) => {
const bucketName = "YOUR-BUCKET-NAME";
const fileName = "YOUR-FILE-NAME.EXTENSION";
const data = {
Bucket: bucketName,
Key: fileName,
Body: buffer,
};
s3.putObject(data, (error) => {
if (!error) {
export async function main(event, context, callback) {
parser(event).then(() => {
uploadFile(event.body.file)
.then(() => {
// Handle successful upload
})
.catch(() => {
// Handle upload errors
});
});