Files
atom/script/vsts/lib/upload-to-s3.js
Nathan Sobo 4bc43eb358 On Azure DevOps, upload Windows crash dumps to S3 on release branches
On release branches, we can't upload crash dumps because they will leak
secret environment variables. So instead we will upload them to our S3
bucket with 'private' ACL. They can then be manually retrieved via the
AWS CLI with our private credentials.
2019-04-18 17:04:49 -06:00

58 lines
1.5 KiB
JavaScript

'use strict'
const fs = require('fs')
const path = require('path')
const aws = require('aws-sdk')
module.exports = function (s3Key, s3Secret, s3Bucket, directory, assets, acl = 'public-read') {
const s3 = new aws.S3({
accessKeyId: s3Key,
secretAccessKey: s3Secret,
params: { Bucket: s3Bucket }
})
function listExistingAssetsForDirectory (directory) {
return s3.listObjectsV2({ Prefix: directory }).promise().then((res) => {
return res.Contents.map((obj) => { return { Key: obj.Key } })
})
}
function deleteExistingAssets (existingAssets) {
if (existingAssets.length > 0) {
return s3.deleteObjects({ Delete: { Objects: existingAssets } }).promise()
} else {
return Promise.resolve(true)
}
}
function uploadAssets (assets, directory) {
return assets.reduce(
function (promise, asset) {
return promise.then(() => uploadAsset(directory, asset))
}, Promise.resolve())
}
function uploadAsset (directory, assetPath) {
return new Promise((resolve, reject) => {
console.info(`Uploading ${assetPath}`)
const params = {
Key: `${directory}${path.basename(assetPath)}`,
ACL: acl,
Body: fs.createReadStream(assetPath)
}
s3.upload(params, error => {
if (error) {
reject(error)
} else {
resolve()
}
})
})
}
return listExistingAssetsForDirectory(directory)
.then(deleteExistingAssets)
.then(() => uploadAssets(assets, directory))
}