Skip to content

Commit

Permalink
fix(scripts): make aws deploy scripts work with new sdk (#16586)
Browse files Browse the repository at this point in the history
closes AUTH-979
  • Loading branch information
shlokamin authored Oct 24, 2024
1 parent eb710c0 commit e32c0f3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 20 deletions.
35 changes: 23 additions & 12 deletions scripts/deploy/lib/copyObject.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const mime = require('mime')
const { CopyObjectCommand } = require('@aws-sdk/client-s3')

// TODO(mc, 2019-07-16): optimize cache values
const getCopyParams = obj => ({
Expand All @@ -12,7 +13,7 @@ const getCopyParams = obj => ({
/**
* Copy an object to an S3 bucket
*
* @param {S3} s3 - AWS.S3 instance
* @param {S3Client} s3 - AWS SDK v3 S3Client instance
* @param {S3Object} sourceObj - Object to copy
* @param {string} destBucket - Destination bucket
* @param {string} [destPath] - Destination bucket folder (root if unspecified)
Expand All @@ -21,10 +22,10 @@ const getCopyParams = obj => ({
*
* @typedef S3Object
* @property {string} Bucket - Object bucket
* @property {String} Prefix - Deploy folder in bucket
* @property {string} Prefix - Deploy folder in bucket
* @property {string} Key - Full key to object
*/
module.exports = function copyObject(
module.exports = async function copyObject(
s3,
sourceObj,
destBucket,
Expand All @@ -37,18 +38,28 @@ module.exports = function copyObject(
const copyParams = getCopyParams(sourceObj)

console.log(
`${dryrun ? 'DRYRUN: ' : ''}Copy
Source: ${copySource}
Dest: /${destBucket}/${destKey}
Params: ${JSON.stringify(copyParams)}\n`
`${
dryrun ? 'DRYRUN: ' : ''
}Copy\nSource: ${copySource}\nDest: /${destBucket}/${destKey}\nParams: ${JSON.stringify(
copyParams
)}\n`
)

if (dryrun) return Promise.resolve()

const copyObjectParams = Object.assign(
{ Bucket: destBucket, Key: destKey, CopySource: copySource },
copyParams
)
const copyObjectParams = {
Bucket: destBucket,
Key: destKey,
CopySource: copySource,
...copyParams,
}

return s3.copyObject(copyObjectParams).promise()
try {
const command = new CopyObjectCommand(copyObjectParams)
await s3.send(command)
console.log(`Successfully copied to /${destBucket}/${destKey}`)
} catch (err) {
console.error(`Error copying object: ${err.message}`)
throw err
}
}
27 changes: 19 additions & 8 deletions scripts/deploy/lib/removeObject.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
'use strict'

const { DeleteObjectCommand } = require('@aws-sdk/client-s3');

/**
* Remove an object from S3
*
* @param {AWS.S3} s3 - AWS.S3 instance
* @param {S3Client} s3 - S3Client instance
* @param {S3Object} obj - Object to remove
* @param {boolean} [dryrun] - Don't actually remove anything
* @returns {Promise} Promise that resolves when the removal is complete
Expand All @@ -13,13 +15,22 @@
* @property {String} Prefix - Deploy folder in bucket
* @property {string} Key - Full key to object
*/
module.exports = function removeObject(s3, obj, dryrun) {
module.exports = async function removeObject(s3, obj, dryrun) {
console.log(
`${dryrun ? 'DRYRUN: ' : ''}Remove
Source: /${obj.Bucket}/${obj.Key}\n`
)
`${dryrun ? 'DRYRUN: ' : ''}Remove\nSource: /${obj.Bucket}/${obj.Key}\n`
);

if (dryrun) return Promise.resolve();

if (dryrun) return Promise.resolve()
// Construct the deleteObject command with the bucket and key
const deleteParams = { Bucket: obj.Bucket, Key: obj.Key };

return s3.deleteObject({ Bucket: obj.Bucket, Key: obj.Key }).promise()
}
try {
// Use the send method with DeleteObjectCommand
const result = await s3.send(new DeleteObjectCommand(deleteParams));
return result;
} catch (error) {
console.error('Error removing object:', error);
throw error;
}
};

0 comments on commit e32c0f3

Please sign in to comment.