70 lines
1.9 KiB
JavaScript
70 lines
1.9 KiB
JavaScript
const { S3 } = require('@aws-sdk/client-s3');
|
|
|
|
async function testWithDefaultCreds() {
|
|
console.log('🔍 测试MinIO默认凭据...\n');
|
|
|
|
const configs = [
|
|
{
|
|
name: 'MinIO 默认凭据',
|
|
config: {
|
|
endpoint: 'http://localhost:9000',
|
|
region: 'us-east-1',
|
|
credentials: {
|
|
accessKeyId: 'minioadmin',
|
|
secretAccessKey: 'minioadmin',
|
|
},
|
|
forcePathStyle: true,
|
|
},
|
|
},
|
|
{
|
|
name: '你的自定义凭据',
|
|
config: {
|
|
endpoint: 'http://localhost:9000',
|
|
region: 'us-east-1',
|
|
credentials: {
|
|
accessKeyId: '7Nt7OyHkwIoo3zvSKdnc',
|
|
secretAccessKey: 'EZ0cyrjJAsabTLNSqWcU47LURMppBW2kka3LuXzb',
|
|
},
|
|
forcePathStyle: true,
|
|
},
|
|
},
|
|
];
|
|
|
|
for (const { name, config } of configs) {
|
|
console.log(`\n📱 测试 ${name}:`);
|
|
console.log(` Access Key: ${config.credentials.accessKeyId}`);
|
|
console.log(` Secret Key: ${config.credentials.secretAccessKey.substring(0, 8)}...`);
|
|
|
|
const s3Client = new S3(config);
|
|
|
|
try {
|
|
// 测试列出buckets
|
|
const result = await s3Client.listBuckets();
|
|
console.log(` ✅ 连接成功!`);
|
|
console.log(` 📂 现有buckets:`, result.Buckets?.map((b) => b.Name) || []);
|
|
|
|
// 测试创建bucket
|
|
const bucketName = 'test123';
|
|
try {
|
|
await s3Client.headBucket({ Bucket: bucketName });
|
|
console.log(` ✅ Bucket "${bucketName}" 已存在`);
|
|
} catch (error) {
|
|
if (error.name === 'NotFound') {
|
|
console.log(` 📦 创建bucket "${bucketName}"...`);
|
|
await s3Client.createBucket({ Bucket: bucketName });
|
|
console.log(` ✅ Bucket "${bucketName}" 创建成功`);
|
|
} else {
|
|
throw error;
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.log(` ❌ 连接失败:`, error.message);
|
|
if (error.$metadata?.httpStatusCode) {
|
|
console.log(` 📊 HTTP状态码:`, error.$metadata.httpStatusCode);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
testWithDefaultCreds().catch(console.error);
|