Created
March 1, 2017 18:37
-
-
Save lasergoat/78ba41e58db34ba5b234606a0edd2e7e to your computer and use it in GitHub Desktop.
upload to s3 then move
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
// https://github.com/andrewrk/node-s3-client | |
import path from 'path'; | |
import s3 from 's3'; | |
require('dotenv').config({'path': path.resolve('./.env')}); | |
const debug = require('debug')('emaf'); | |
const client = s3.createClient({ | |
maxAsyncS3: 20, // this is the default | |
s3RetryCount: 3, // this is the default | |
s3RetryDelay: 1000, // this is the default | |
multipartUploadThreshold: 20971520, // this is the default (20 MB) | |
multipartUploadSize: 15728640, // this is the default (15 MB) | |
s3Options: { | |
accessKeyId: process.env.S3_KEY, | |
secretAccessKey: process.env.S3_SECRET, | |
region: process.env.S3_REGION, | |
// endpoint: 's3.yourdomain.com', | |
// sslEnabled: false | |
// any other options are passed to new AWS.S3() | |
// See: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Config.html#constructor-property | |
}, | |
}); | |
export const upload = (filePath) => { | |
const fileNameParse = path.parse(filePath); | |
debug(`uploading file to s3: ${filePath}`); | |
const params = { | |
localFile: filePath, | |
s3Params: { | |
Key: path.join(process.env.S3_PATH, fileNameParse.base), | |
Bucket: process.env.S3_BUCKET, | |
// Key: "some/remote/file", | |
// other options supported by putObject, except Body and ContentLength. | |
// See: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property | |
}, | |
}; | |
return new Promise((resolve, reject) => { | |
const uploader = client.uploadFile(params); | |
uploader.on('error', function(err) { | |
debug(err); | |
reject(err); | |
}); | |
// uploader.on('progress', function() { | |
// console.log( | |
// "progress", | |
// uploader.progressMd5Amount, | |
// uploader.progressAmount, | |
// uploader.progressTotal | |
// ); | |
// }); | |
uploader.on('end', function() { | |
debug(`finished uploading file to s3: ${filePath}`); | |
// must return the original Name for chaining | |
resolve(filePath); | |
}); | |
}) | |
} |
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
... | |
.then((files) => { | |
// upload files to s3 | |
// files is an array because | |
// of the Promise.all in the download block above ^^ | |
return Promise.all( | |
files.map((file) => s3.upload(file)) | |
) | |
}) | |
.then((files) => { | |
// move files to final directory | |
// files is an array because | |
// of the Promise.all in the s3 block above ^^ | |
return Promise.all( | |
files.map((file) => { | |
// find file name | |
const nameParse = path.parse(file); | |
const finalLocation = path.join( | |
cwd, | |
FINAL_DIRECTORY, | |
nameParse.base | |
); | |
debug(`Moving From Temp: ${file}`); | |
debug(`To: ${finalLocation}`); | |
return fsp.rename( | |
file, | |
finalLocation | |
).then(() => { | |
debug(`Moved To: ${finalLocation}`); | |
return file; | |
}) | |
}) | |
) | |
}) | |
.then((data) => { | |
debug(data); | |
return ssh.end(); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment