128 lines
3.8 KiB
JavaScript
128 lines
3.8 KiB
JavaScript
// 在项目内运行,可以使用现有的AWS SDK依赖
|
||
process.chdir('./packages/storage');
|
||
|
||
async function testWithCorrectCreds() {
|
||
console.log('🔍 使用正确的MinIO凭据测试...\n');
|
||
|
||
// 动态导入AWS SDK
|
||
const { S3 } = await import('@aws-sdk/client-s3');
|
||
|
||
const config = {
|
||
endpoint: 'http://localhost:9000',
|
||
region: 'us-east-1',
|
||
credentials: {
|
||
accessKeyId: 'nice1234', // Docker环境变量设置的凭据
|
||
secretAccessKey: 'nice1234',
|
||
},
|
||
forcePathStyle: true,
|
||
};
|
||
|
||
console.log('配置信息:');
|
||
console.log('- Endpoint:', config.endpoint);
|
||
console.log('- Region:', config.region);
|
||
console.log('- Access Key:', config.credentials.accessKeyId);
|
||
console.log('- Force Path Style:', config.forcePathStyle);
|
||
console.log();
|
||
|
||
const s3Client = new S3(config);
|
||
|
||
try {
|
||
// 1. 测试基本连接
|
||
console.log('📡 测试基本连接...');
|
||
const buckets = await s3Client.listBuckets();
|
||
console.log('✅ 连接成功!');
|
||
console.log('📂 现有存储桶:', buckets.Buckets?.map((b) => b.Name) || []);
|
||
console.log();
|
||
|
||
// 2. 检查test123存储桶
|
||
const bucketName = 'test123';
|
||
console.log(`🪣 检查存储桶 "${bucketName}"...`);
|
||
|
||
try {
|
||
await s3Client.headBucket({ Bucket: bucketName });
|
||
console.log(`✅ 存储桶 "${bucketName}" 存在`);
|
||
} catch (error) {
|
||
if (error.name === 'NotFound') {
|
||
console.log(`❌ 存储桶 "${bucketName}" 不存在,正在创建...`);
|
||
try {
|
||
await s3Client.createBucket({ Bucket: bucketName });
|
||
console.log(`✅ 存储桶 "${bucketName}" 创建成功`);
|
||
} catch (createError) {
|
||
console.log(`❌ 创建存储桶失败:`, createError.message);
|
||
return;
|
||
}
|
||
} else {
|
||
console.log(`❌ 检查存储桶失败:`, error.message);
|
||
return;
|
||
}
|
||
}
|
||
|
||
// 3. 测试简单上传
|
||
console.log('\n📤 测试简单上传...');
|
||
const testKey = 'test-file.txt';
|
||
const testContent = 'Hello MinIO from correct credentials!';
|
||
|
||
try {
|
||
await s3Client.putObject({
|
||
Bucket: bucketName,
|
||
Key: testKey,
|
||
Body: testContent,
|
||
});
|
||
console.log(`✅ 简单上传成功: ${testKey}`);
|
||
} catch (error) {
|
||
console.log(`❌ 简单上传失败:`, error.message);
|
||
console.log('错误详情:', error);
|
||
return;
|
||
}
|
||
|
||
// 4. 测试分片上传初始化
|
||
console.log('\n🔄 测试分片上传初始化...');
|
||
const multipartKey = 'test-multipart.txt';
|
||
|
||
try {
|
||
const multipartUpload = await s3Client.createMultipartUpload({
|
||
Bucket: bucketName,
|
||
Key: multipartKey,
|
||
});
|
||
console.log(`✅ 分片上传初始化成功: ${multipartUpload.UploadId}`);
|
||
|
||
// 立即取消这个分片上传
|
||
await s3Client.abortMultipartUpload({
|
||
Bucket: bucketName,
|
||
Key: multipartKey,
|
||
UploadId: multipartUpload.UploadId,
|
||
});
|
||
console.log('✅ 分片上传取消成功');
|
||
} catch (error) {
|
||
console.log(`❌ 分片上传初始化失败:`, error.message);
|
||
console.log('错误详情:', error);
|
||
if (error.$metadata) {
|
||
console.log('HTTP状态码:', error.$metadata.httpStatusCode);
|
||
}
|
||
return;
|
||
}
|
||
|
||
console.log('\n🎉 所有测试通过!MinIO配置正确。');
|
||
console.log('\n📝 下一步: 更新你的.env文件使用以下配置:');
|
||
console.log('STORAGE_TYPE=s3');
|
||
console.log('S3_ENDPOINT=http://localhost:9000');
|
||
console.log('S3_REGION=us-east-1');
|
||
console.log('S3_BUCKET=test123');
|
||
console.log('S3_ACCESS_KEY_ID=nice1234');
|
||
console.log('S3_SECRET_ACCESS_KEY=nice1234');
|
||
console.log('S3_FORCE_PATH_STYLE=true');
|
||
} catch (error) {
|
||
console.log('❌ 连接失败:', error.message);
|
||
console.log('错误详情:', error);
|
||
|
||
if (error.message.includes('ECONNREFUSED')) {
|
||
console.log('\n💡 提示:');
|
||
console.log('- 确保MinIO正在端口9000运行');
|
||
console.log('- 检查docker容器状态: docker ps');
|
||
console.log('- 重启MinIO: docker restart minio-container-name');
|
||
}
|
||
}
|
||
}
|
||
|
||
testWithCorrectCreds().catch(console.error);
|