|
| 1 | +import * as core from '@actions/core' |
| 2 | +import { AssumeRoleCommand, STSClient } from '@aws-sdk/client-sts' |
| 3 | +import { S3Client, S3ServiceException, paginateListObjectsV2, DeleteObjectsCommand, waitUntilObjectNotExists } from '@aws-sdk/client-s3' |
| 4 | +import { args } from './args.js' |
| 5 | + |
| 6 | +const stsClient = new STSClient({ region: args.region }) |
| 7 | +const s3Credentials = await stsClient.send(new AssumeRoleCommand({ |
| 8 | + RoleArn: args.role, |
| 9 | + RoleSessionName: 'deleteExperimentFromS3Session', |
| 10 | + DurationSeconds: 900 |
| 11 | +})) |
| 12 | + |
| 13 | +const s3Client = new S3Client({ |
| 14 | + region: args.region, |
| 15 | + credentials: { |
| 16 | + accessKeyId: s3Credentials.Credentials.AccessKeyId, |
| 17 | + secretAccessKey: s3Credentials.Credentials.SecretAccessKey, |
| 18 | + sessionToken: s3Credentials.Credentials.SessionToken |
| 19 | + } |
| 20 | +}) |
| 21 | + |
| 22 | +async function collectKeysToDelete(bucketName, bucketDir) { |
| 23 | + const keys = [] |
| 24 | + core.info("Bucket name: " + bucketName) |
| 25 | + core.info(`Looking up files with prefix '${bucketDir}'`) |
| 26 | + |
| 27 | + try { |
| 28 | + const params = { |
| 29 | + Bucket: bucketName, |
| 30 | + Prefix:bucketDir |
| 31 | + } |
| 32 | + const paginator = paginateListObjectsV2({ client: s3Client }, params) |
| 33 | + for await (const page of paginator) { |
| 34 | + page.Contents?.forEach(obj => { |
| 35 | + keys.push(obj.Key) |
| 36 | + }) |
| 37 | + } |
| 38 | + core.info(`Found ${keys.length} matching files`) |
| 39 | + core.info(keys.map(k => ` • ${k}`).join('\n')) |
| 40 | + return keys |
| 41 | + } catch (err) { |
| 42 | + if (err instanceof S3ServiceException) { |
| 43 | + core.setFailed( |
| 44 | + `Error from S3 while listing objects for "${bucketName}". ${err.name}: ${err.message}`, |
| 45 | + ) |
| 46 | + } else { |
| 47 | + core.setFailed(`Error while listing objects for "${bucketName}". ${err.name}: ${err.message}`) |
| 48 | + } |
| 49 | + process.exit(1) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +async function deleteFiles(bucketName, keys) { |
| 54 | + try { |
| 55 | + core.info('Deleting...') |
| 56 | + |
| 57 | + const { Deleted } = await s3Client.send( |
| 58 | + new DeleteObjectsCommand({ |
| 59 | + Bucket: bucketName, |
| 60 | + Delete: { |
| 61 | + Objects: keys.map((k) => ({ Key: k })), |
| 62 | + }, |
| 63 | + }), |
| 64 | + ) |
| 65 | + |
| 66 | + for (const key in keys) { |
| 67 | + await waitUntilObjectNotExists( |
| 68 | + { client: s3Client }, |
| 69 | + { Bucket: bucketName, Key: key }, |
| 70 | + ) |
| 71 | + } |
| 72 | + core.info( |
| 73 | + `Successfully deleted ${Deleted?.length || 0} objects.`, |
| 74 | + ) |
| 75 | + core.info(Deleted?.map((d) => ` • ${d.Key}`).join("\n")) |
| 76 | + } catch (err) { |
| 77 | + if (err instanceof S3ServiceException) { |
| 78 | + core.setFailed( |
| 79 | + `Error from S3 while deleting objects for "${bucketName}". ${err.name}: ${err.message}`, |
| 80 | + ) |
| 81 | + } else { |
| 82 | + core.setFailed(`Error while deleting objects for "${bucketName}". ${err.name}: ${err.message}`) |
| 83 | + } |
| 84 | + process.exit(1) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +if (args.dry) { |
| 89 | + core.info('This is a dry run.') |
| 90 | +} |
| 91 | +const keysToBeDeleted = await collectKeysToDelete(args.bucket, args.dir) |
| 92 | +if (!args.dry) { |
| 93 | + if (keysToBeDeleted.length > 0) { |
| 94 | + await deleteFiles(args.bucket, keysToBeDeleted) |
| 95 | + } else { |
| 96 | + core.info('No files found to delete') |
| 97 | + } |
| 98 | +} |
| 99 | +core.info('Completed successfully') |
0 commit comments