s3-cli-js
Version:
A TypeScript-based npm package that replaces AWS CLI for S3 operations using presigned URLs
76 lines • 2.99 kB
JavaScript
;
/**
* Make bucket command implementation
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeBucketCommand = makeBucketCommand;
const chalk_1 = __importDefault(require("chalk"));
const progress_1 = require("../utils/progress");
/**
* Create a new S3 bucket
*/
async function makeBucketCommand(client, bucketUri) {
try {
if (!(0, progress_1.isS3Uri)(bucketUri)) {
throw new Error('Bucket path must be an S3 URI (s3://bucket-name)');
}
const { bucket, key } = (0, progress_1.parseS3Uri)(bucketUri);
if (key !== '') {
throw new Error('Bucket URI should not contain a key path (s3://bucket-name only)');
}
// Validate bucket name
validateBucketName(bucket);
console.log(chalk_1.default.blue(`Creating bucket: ${bucket}`));
await client.createBucket(bucket);
console.log(chalk_1.default.green(`✓ Bucket created successfully: s3://${bucket}`));
}
catch (error) {
if (error.name === 'BucketAlreadyExists' || error.name === 'BucketAlreadyOwnedByYou') {
console.log(chalk_1.default.yellow(`Bucket already exists: s3://${(0, progress_1.parseS3Uri)(bucketUri).bucket}`));
}
else {
console.error(chalk_1.default.red(`Error: ${error.message}`));
process.exit(1);
}
}
}
/**
* Validate S3 bucket name according to AWS rules
*/
function validateBucketName(bucketName) {
// Basic validation rules for S3 bucket names
if (bucketName.length < 3 || bucketName.length > 63) {
throw new Error('Bucket name must be between 3 and 63 characters long');
}
if (!/^[a-z0-9.-]+$/.test(bucketName)) {
throw new Error('Bucket name can only contain lowercase letters, numbers, dots, and hyphens');
}
if (bucketName.startsWith('.') || bucketName.endsWith('.')) {
throw new Error('Bucket name cannot start or end with a dot');
}
if (bucketName.startsWith('-') || bucketName.endsWith('-')) {
throw new Error('Bucket name cannot start or end with a hyphen');
}
if (bucketName.includes('..')) {
throw new Error('Bucket name cannot contain consecutive dots');
}
if (/^\d+\.\d+\.\d+\.\d+$/.test(bucketName)) {
throw new Error('Bucket name cannot be formatted as an IP address');
}
// Check for invalid patterns
const invalidPatterns = [
/^xn--/, // Cannot start with xn--
/^sthree-/, // Cannot start with sthree-
/-s3alias$/, // Cannot end with -s3alias
/--ol-s3$/, // Cannot end with --ol-s3
];
for (const pattern of invalidPatterns) {
if (pattern.test(bucketName)) {
throw new Error(`Bucket name contains invalid pattern: ${bucketName}`);
}
}
}
//# sourceMappingURL=mb.js.map