s3-cli-js
Version:
A TypeScript-based npm package that replaces AWS CLI for S3 operations using presigned URLs
149 lines • 5.28 kB
JavaScript
;
/**
* Configuration utilities for AWS credentials and settings
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadAWSConfig = loadAWSConfig;
exports.validateConfig = validateConfig;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const os = __importStar(require("os"));
/**
* Load AWS configuration from environment variables or AWS credentials file
*/
function loadAWSConfig() {
// Try environment variables first
const envConfig = loadFromEnvironment();
if (envConfig) {
return envConfig;
}
// Try AWS credentials file
const fileConfig = loadFromCredentialsFile();
if (fileConfig) {
return fileConfig;
}
throw new Error('AWS credentials not found. Please set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables or configure AWS credentials file.');
}
/**
* Load configuration from environment variables
*/
function loadFromEnvironment() {
const accessKeyId = process.env.AWS_ACCESS_KEY_ID;
const secretAccessKey = process.env.AWS_SECRET_ACCESS_KEY;
const region = process.env.AWS_DEFAULT_REGION || process.env.AWS_REGION || 'us-east-1';
const sessionToken = process.env.AWS_SESSION_TOKEN;
const endpoint = process.env.AWS_ENDPOINT_URL;
if (!accessKeyId || !secretAccessKey) {
return null;
}
return {
accessKeyId,
secretAccessKey,
region,
sessionToken,
endpoint,
};
}
/**
* Load configuration from AWS credentials file
*/
function loadFromCredentialsFile(profile = 'default') {
const credentialsPath = path.join(os.homedir(), '.aws', 'credentials');
const configPath = path.join(os.homedir(), '.aws', 'config');
if (!fs.existsSync(credentialsPath)) {
return null;
}
try {
const credentials = parseIniFile(credentialsPath);
const config = fs.existsSync(configPath) ? parseIniFile(configPath) : {};
const profileSection = credentials[profile];
const configSection = config[`profile ${profile}`] || config[profile];
if (!profileSection || !profileSection.aws_access_key_id || !profileSection.aws_secret_access_key) {
return null;
}
return {
accessKeyId: profileSection.aws_access_key_id,
secretAccessKey: profileSection.aws_secret_access_key,
region: profileSection.region || configSection?.region || 'us-east-1',
sessionToken: profileSection.aws_session_token,
};
}
catch (error) {
return null;
}
}
/**
* Parse INI file format
*/
function parseIniFile(filePath) {
const content = fs.readFileSync(filePath, 'utf-8');
const lines = content.split('\n');
const result = {};
let currentSection = '';
for (const line of lines) {
const trimmedLine = line.trim();
if (!trimmedLine || trimmedLine.startsWith('#') || trimmedLine.startsWith(';')) {
continue;
}
if (trimmedLine.startsWith('[') && trimmedLine.endsWith(']')) {
currentSection = trimmedLine.slice(1, -1);
result[currentSection] = {};
continue;
}
const equalIndex = trimmedLine.indexOf('=');
if (equalIndex > 0 && currentSection) {
const key = trimmedLine.slice(0, equalIndex).trim();
const value = trimmedLine.slice(equalIndex + 1).trim();
result[currentSection][key] = value;
}
}
return result;
}
/**
* Validate S3 configuration
*/
function validateConfig(config) {
if (!config.accessKeyId) {
throw new Error('AWS Access Key ID is required');
}
if (!config.secretAccessKey) {
throw new Error('AWS Secret Access Key is required');
}
if (!config.region) {
throw new Error('AWS Region is required');
}
}
//# sourceMappingURL=config.js.map