Last active
June 11, 2019 13:38
-
-
Save Maxou44/2b680df79116102c9a587eac69ddad60 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 fetch = require('node-fetch'); | |
const fs = require('fs'); | |
const path = './file.txt'; | |
const filename = 'MyFile.txt'; | |
const bucket = 'processing-storage'; | |
const s3 = new AWS.S3({ | |
endpoint: new AWS.Endpoint('https://s3.fr-par.scw.cloud'), | |
accessKeyId: 'SCW*****************', | |
secretAccessKey: '00160773-****-****-****-************', | |
region: 'fr-par', | |
signatureVersion: 'v4' | |
}); | |
const url = s3.getSignedUrl('putObject', { | |
Bucket: bucket, | |
Key: filename, | |
Expires: 60, | |
//ACL: 'public-read' | |
}); | |
const content = fs.createReadStream(path); | |
const size = (fs.statSync(path).size || 0); | |
fetch(url, { | |
method: 'PUT', | |
body: content, | |
headers: { | |
'Content-Type': 'application/octet-stream', | |
'Content-Length': size | |
}, | |
}).then((res) => { | |
if (res.status === 200) | |
return console.log('Upload OK', url.split('?')[0]); | |
res.text().then((err) => { | |
console.log('Upload failed', err); | |
}) | |
}).catch((err) => { | |
console.log('Upload failed', err); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment