Created
July 19, 2018 06:05
-
-
Save mako34/1be8c6a0616c9ee91b4775c581e459c2 to your computer and use it in GitHub Desktop.
S3 create presigned URL
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 AWS = require('aws-sdk'); | |
const fs = require('fs'); | |
const s3 = new AWS.S3({ | |
accessKeyId: 'AA', | |
secretAccessKey: 'BB+oeyjn8zGANuDyCIY', | |
region: 'ap-southeast-2' | |
// accessKeyId: process.env.AWS_ACCESS_KEY, | |
// secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY, | |
}); | |
//DIRECT UPLOAD | |
// const fileName = 'su.csv'; | |
// const uploadFile = () => { | |
// fs.readFile(fileName, (err, data) => { | |
// if (err) throw err; | |
// const params = { | |
// Bucket: 'sb-communicate-files-dev', | |
// Key: 'su2.csv', | |
// Body: data | |
// }; | |
// s3.upload(params, function(s3Err, data) { | |
// if (s3Err) throw s3Err | |
// console.log(`File uploaded successfully at ${data.Location}`) | |
// }); | |
// }); | |
// }; | |
// uploadFile(); | |
/// | |
const getSigne = () => { | |
console.log('empezo'); | |
const params = { | |
Bucket: 'sb-communicate-files-dev', | |
Key: 'd2.jpg', | |
ContentType: 'image/jpeg', | |
Expires: 100 | |
}; | |
s3.getSignedUrl('putObject', params, function(err, signedUrl) { | |
let response; | |
if (err) { | |
console.log('err'); | |
} else { | |
console.log('oks signedUrl:\n'+signedUrl); | |
} | |
}); | |
}; | |
getSigne(); | |
/// | |
// original lambda for presigned url | |
// const aws = require('aws-sdk'); | |
// module.exports = CreateRecord => { | |
// CreateRecord.controllers.createSignature = (event, context, callback) => { | |
// const s3 = new aws.S3({ | |
// signatureVersion: 'v4', | |
// }); | |
// const params = { | |
// Bucket: 'random-test-bucket002', | |
// Key: 'test-key', | |
// Expires: 100 | |
// }; | |
// s3.getSignedUrl('putObject', params, function(err, signedUrl) { | |
// let response; | |
// if (err) { | |
// response = { | |
// statusCode: 500, | |
// headers: { | |
// 'Access-Control-Allow-Origin': '*', | |
// }, | |
// body: JSON.stringify({ | |
// error: 'Did not receive signed url' | |
// }), | |
// }; | |
// } else { | |
// response = { | |
// statusCode: 200, | |
// headers: { | |
// 'Access-Control-Allow-Origin': '*', // Required for CORS support to work | |
// }, | |
// body: JSON.stringify({ | |
// message: `Url successfully created`, | |
// signedUrl, | |
// }) | |
// }; | |
// } | |
// callback(null, response); | |
// }); | |
// }; | |
// }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment