Skip to content

Instantly share code, notes, and snippets.

@BransonGitomeh
Created May 20, 2020 18:47
Show Gist options
  • Save BransonGitomeh/027df14e8465464e7a5a3a20b1634e9f to your computer and use it in GitHub Desktop.
Save BransonGitomeh/027df14e8465464e7a5a3a20b1634e9f to your computer and use it in GitHub Desktop.
s3 folder upload node js
ACCESS_KEY_ID=...
SECRET_ACCESS_KEY=...
AWS_REGION=...
S3_BUCKET=...
require('dotenv').config()
const uploader = require("./uploader")
const git = require("git-last-commit")
const { promisify } = require("util")
const fs = require("fs")
const getCommit = promisify(git.getLastCommit)
const readDirFiles = promisify(fs.readdir)
const main = async () => {
const { shortHash } = await getCommit()
console.log(shortHash)
// get the files in the upload dir
// HTML5
const dirs = await readDirFiles("../exports/HTML5")
console.log(dirs)
dirs.map(file=>uploader(`../exports/HTML5/${file}`,`${shortHash}/HTML5`,`${file}`))
}
main()
{
"name": "uploader",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"dev": "nodemon index.js"
},
"devDependencies": {
"nodemon": "^2.0.4"
},
"dependencies": {
"aws-sdk": "^2.680.0",
"bluebird": "^3.7.2",
"dotenv": "^8.2.0",
"git-last-commit": "^1.0.0"
}
}
/**
* This gist was inspired from https://gist.github.com/homam/8646090 which I wanted to work when uploading an image from
* a base64 string.
* Updated to use Promise (bluebird)
* Web: https://mayneweb.com
*
* @param {string} base64 Data
* @return {string} Image url
*/
const AWS = require('aws-sdk');
const bluebird = require('bluebird')
const fs = require("fs")
const { execSync } = require("child_process");
const gitCommand = "git rev-parse HEAD"
const getGitCommitHash = () => execSync(gitCommand).toString().trim()
const imageUpload = async (filePath, spacePath, fileName) => {
console.log({ filePath, spacePath,fileName })
// You can either "yarn add aws-sdk" or "npm i aws-sdk"
// Configure AWS with your access and secret key.
const { ACCESS_KEY_ID, SECRET_ACCESS_KEY, AWS_REGION, S3_BUCKET } = process.env;
// Configure AWS to use promise
AWS.config.setPromisesDependency(bluebird);
AWS.config.update({ accessKeyId: ACCESS_KEY_ID, secretAccessKey: SECRET_ACCESS_KEY, region: AWS_REGION });
// Create an s3 instance
// const s3 = new AWS.S3();
const spacesEndpoint = new AWS.Endpoint('nyc3.digitaloceanspaces.com');
const s3 = new AWS.S3({
endpoint: spacesEndpoint
});
// Ensure that you POST a base64 data to your server.
// Let's assume the variable "base64" is one.
const fileContent = fs.readFileSync(filePath);
// const base64Data = new Buffer.from(base64.replace(/^data:image\/\w+;base64,/, ""), 'base64');
// Getting the file type, ie: jpeg, png or gif
const type = filePath.split(".")[filePath.split(".").length - 1]
// console.log({type})
// Generally we'd have an userId associated with the image
// For this example, we'll simulate one
const userId = 1;
// With this setup, each time your user uploads an image, will be overwritten.
// To prevent this, use a different Key each time.
// This won't be needed if they're uploading their avatar, hence the filename, userAvatar.js.
const params = {
Bucket: S3_BUCKET + "/" + spacePath,
Key: `${fileName}`, // type is not required
Body: fileContent,
ACL: 'public-read',
// ContentEncoding: 'base64', // required
// ContentType: `image/${type}` // required. Notice the back ticks
}
// console.log(params)
// The upload() is used instead of putObject() as we'd need the location url and assign that to our user profile/database
// see: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#upload-property
let location = '';
let key = '';
try {
const { Location, Key } = await s3.upload(params).promise();
location = Location;
key = Key;
} catch (error) {
console.log(error)
}
// Save the Location (url) to your database and Key if needs be.
// As good developers, we should return the url and let other function do the saving to database etc
console.log(location, key);
return location;
// To delete, see: https://gist.github.com/SylarRuby/b3b1430ca633bc5ffec29bbcdac2bd52
}
module.exports = imageUpload;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment