// Name: AWS S3 Utility // Description: Interact with AWS S3 buckets // Author: vinaayakha import { S3Client, ListBucketsCommand, ListObjectsCommand, GetObjectCommand, HeadObjectCommand, PutObjectCommand, DeleteObjectCommand, } from '@aws-sdk/client-s3' import { getSignedUrl } from '@aws-sdk/s3-request-presigner' import yaml from 'js-yaml' import jq from 'node-jq' /** * Prompts the user for an environment variable if not already set, or returns the existing value. * @param key The environment variable key. * @param promptMessage The message to display in the prompt if the variable is not set. * @returns The environment variable value. */ const envVar = async (key: string, promptMessage: string): Promise<string> => { const value = await env(key, promptMessage) if (!value) { throw new Error(`Environment variable ${key} is not set.`) } return value } // Initialize environment variables let accessKeyId: string let secretAccessKey: string let awsRegion: string try { accessKeyId = await envVar('AWS_ACCESS_KEY_ID', 'Enter your AWS Access Key ID') secretAccessKey = await envVar('AWS_SECRET_ACCESS_KEY', 'Enter your AWS Secret Access Key') awsRegion = await envVar('AWS_REGION', 'us-west-2') } catch (error) { console.error('Error initializing environment variables:', error) exit(1) } // Initialize AWS S3 client const s3 = new S3Client({ region: awsRegion, credentials: { accessKeyId, secretAccessKey, }, }) // SVG icons const icons = { bucketIcon: ` <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="20" height="20" fill="currentColor"> <path d="M12 2C10.895 2 10 2.895 10 4H4C3.447 4 3 4.447 3 5V7C3 7.553 3.447 8 4 8V20C4 21.104 4.896 22 6 22H18C19.104 22 20 21.104 20 20V8C20.553 8 21 7.553 21 7V5C21 4.447 20.553 4 20 4H14C14 2.895 13.105 2 12 2zM10 4H14C14.553 4 15 4.447 15 5H9C9 4.447 9.447 4 10 4zM5 7V5H19V7H5zM6 20V8H18V20H6z"/> </svg> `, folderIcon: ` <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="20" height="20" fill="currentColor"> <path d="M10 4L12 6H20C21.104 6 22 6.896 22 8V18C22 19.104 21.104 20 20 20H4C2.896 20 2 19.104 2 18V6C2 4.896 2.896 4 4 4H10zM4 8V18H20V8H4z"/> </svg> `, fileIcon: ` <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="20" height="20" fill="currentColor"> <path d="M14 2H6C4.896 2 4 2.896 4 4V20C4 21.104 4.896 22 6 22H18C19.104 22 20 21.104 20 20V8L14 2zM14 4L18 8H14V4zM6 4H12V9H18V20H6V4z"/> </svg> `, }