Last active
March 25, 2020 07:04
-
-
Save arash16/4e1b53284100eb21b55d74ecc054140e to your computer and use it in GitHub Desktop.
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
/* | |
export AWS_ACCESS_KEY=key | |
export AWS_SECRET_ACCESS_KEY=secret | |
export AWS_BUCKET_NAME=bucket | |
yarn init | |
yarn add aws-sdk | |
node server.js | |
*/ | |
const fs = require('fs'); | |
const path = require('path'); | |
const AWS = require('aws-sdk'); | |
const s3 = new AWS.S3({ | |
endpoint: 'https://s3.ir-thr-at1.arvanstorage.com', | |
accessKeyId: process.env.AWS_ACCESS_KEY, | |
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY, | |
}); | |
const Bucket = process.env.AWS_BUCKET_NAME; | |
const { url, fields } = s3.createPresignedPost({ | |
Bucket, | |
Fields: { | |
key: process.env.FILE_NAME || String(Math.random()), | |
acl: 'public-read', | |
'Content-Type': 'image/', | |
}, | |
Expires: 3600, | |
Conditions: [ | |
['content-length-range', 0, 10 * 1024 * 1024], | |
['starts-with', '$Content-Type', 'image/'], | |
{ acl: 'public-read' }, | |
], | |
}); | |
const hiddenFields = Object.entries(fields) | |
.map(([k,v]) => `<div>${k}: <input name="${k}" value="${v}" /></div>`) | |
.join('\n'); | |
const html = `<!DOCTYPE html> | |
<html> | |
<head> | |
</head> | |
<body> | |
<form method="POST" action="${url}" enctype="multipart/form-data"> | |
${hiddenFields} | |
<div><input type="file" name="file" /></div> | |
<input type="submit" /> | |
</form> | |
</body> | |
</html>`; | |
fs.writeFileSync('test.html', html); | |
console.log(` | |
now open test.html in your browser and only select a file for last field. | |
no other field is allowed to be changed. | |
`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment