attempting to make this a bit less error prone

Signed-off-by: Eli Uriegas <eliuriegas@fb.com>
This commit is contained in:
Eli Uriegas 2021-05-19 10:56:47 -07:00
parent 5a1313076c
commit 10ec34d92d
No known key found for this signature in database
GPG key ID: EE2C3B4893010973
2 changed files with 61 additions and 40 deletions

View file

@ -23,32 +23,30 @@ async function run(): Promise<void> {
core.debug(`Resolved path is ${resolvedPath}`)
const s3 = new AWS.S3()
const s3Prefix = `${github.context.repo.owner}/${github.context.repo.repo}/${github.context.runId}/${name}`
s3.listObjects({Bucket: s3Bucket, Prefix: s3Prefix}, function (err, data) {
if (!data.Contents || err) {
core.error(err)
return
const s3Params = {
Bucket: s3Bucket,
Prefix: s3Prefix
}
core.debug(JSON.stringify(s3Params))
s3.listObjects(s3Params, function (err, data) {
if (err) {
throw err
}
if (!data.Contents) {
throw new Error(`Could not find objects with ${s3Prefix}`)
}
for (const fileObject of data.Contents) {
if (!fileObject.Key) {
continue
}
const getObjectParams = {Bucket: s3Bucket, Key: fileObject.Key}
const localKey = fileObject.Key.replace(s3Prefix, '')
const writeStream = fs.createWriteStream(localKey)
core.info(`Started download: ${localKey}`)
core.debug(`S3 download uri: s3://${s3Bucket}/${fileObject.Key}`)
s3.getObject({Bucket: s3Bucket, Key: fileObject.Key}, function (
err,
fileContents
) {
if (err) {
core.error(`Error downloading ${localKey}`)
throw err
}
fs.writeFileSync(
path.join(resolvedPath, localKey),
fileContents.Body?.toString()
)
core.info(`Done: ${localKey}`)
})
const readStream = s3.getObject(getObjectParams).createReadStream()
readStream.pipe(writeStream)
core.info(`Finished download for ${localKey}`)
}
})
// output the directory that the artifact(s) was/were downloaded to