Skip to content

Instantly share code, notes, and snippets.

@Maxou44
Last active June 11, 2019 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Maxou44/2b680df79116102c9a587eac69ddad60 to your computer and use it in GitHub Desktop.
Save Maxou44/2b680df79116102c9a587eac69ddad60 to your computer and use it in GitHub Desktop.
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