UNPKG

s3-cli-js

Version:

A TypeScript-based npm package that replaces AWS CLI for S3 operations using presigned URLs

94 lines 3.49 kB
"use strict"; /** * List S3 buckets and objects command implementation */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.listCommand = listCommand; const chalk_1 = __importDefault(require("chalk")); const progress_1 = require("../utils/progress"); /** * List S3 buckets or objects */ async function listCommand(client, path, options = {}) { try { if (!path) { // List all buckets await listBuckets(client, options); } else if ((0, progress_1.isS3Uri)(path)) { // List objects in bucket const { bucket, key } = (0, progress_1.parseS3Uri)(path); await listObjects(client, bucket, key, options); } else { throw new Error('Path must be an S3 URI (s3://bucket/key) or empty to list buckets'); } } catch (error) { console.error(chalk_1.default.red(`Error: ${error.message}`)); process.exit(1); } } /** * List all buckets */ async function listBuckets(client, options) { console.log(chalk_1.default.blue('Listing S3 buckets...')); const buckets = await client.listBuckets(); if (buckets.length === 0) { console.log(chalk_1.default.yellow('No buckets found.')); return; } console.log(); for (const bucket of buckets) { const date = (0, progress_1.formatDate)(bucket.creationDate); console.log(`${chalk_1.default.green(date)} ${chalk_1.default.cyan(bucket.name)}`); } if (options.summarize) { console.log(); console.log(chalk_1.default.blue(`Total: ${buckets.length} bucket(s)`)); } } /** * List objects in a bucket */ async function listObjects(client, bucket, prefix, options) { console.log(chalk_1.default.blue(`Listing objects in s3://${bucket}/${prefix}`)); let totalObjects = 0; let totalSize = 0; let continuationToken; do { const result = await client.listObjects({ bucket, prefix: prefix || undefined, delimiter: options.recursive ? undefined : '/', continuationToken, }); // Display common prefixes (directories) for (const commonPrefix of result.commonPrefixes) { console.log(`${chalk_1.default.yellow('PRE')} ${chalk_1.default.cyan(commonPrefix)}`); } // Display objects for (const object of result.objects) { totalObjects++; totalSize += object.size; const date = (0, progress_1.formatDate)(object.lastModified); const size = options.humanReadable ? (0, progress_1.formatBytes)(object.size) : object.size.toString(); const sizeFormatted = size.padStart(12); console.log(`${chalk_1.default.green(date)} ${sizeFormatted} ${object.key}`); } continuationToken = result.nextContinuationToken; } while (continuationToken); if (options.summarize && totalObjects > 0) { console.log(); const totalSizeFormatted = options.humanReadable ? (0, progress_1.formatBytes)(totalSize) : totalSize.toString(); console.log(chalk_1.default.blue(`Total: ${totalObjects} object(s), ${totalSizeFormatted}`)); } if (totalObjects === 0) { console.log(chalk_1.default.yellow('No objects found.')); } } //# sourceMappingURL=ls.js.map