164 lines
4.0 KiB
JavaScript
164 lines
4.0 KiB
JavaScript
const https = require('https');
|
|
const http = require('http');
|
|
const crypto = require('crypto');
|
|
|
|
// MinIO配置
|
|
const config = {
|
|
endpoint: 'localhost:9000',
|
|
accessKeyId: '7Nt7OyHkwIoo3zvSKdnc',
|
|
secretAccessKey: 'EZ0cyrjJAsabTLNSqWcU47LURMppBW2kka3LuXzb',
|
|
bucket: 'test123',
|
|
};
|
|
|
|
// 生成AWS签名v4
|
|
function generateSignature(method, path, headers, body, date) {
|
|
const region = 'us-east-1';
|
|
const service = 's3';
|
|
|
|
// 创建规范请求
|
|
const canonicalRequest = [
|
|
method,
|
|
path,
|
|
'', // query string
|
|
Object.keys(headers)
|
|
.sort()
|
|
.map((key) => `${key.toLowerCase()}:${headers[key]}`)
|
|
.join('\n'),
|
|
'',
|
|
Object.keys(headers)
|
|
.sort()
|
|
.map((key) => key.toLowerCase())
|
|
.join(';'),
|
|
crypto.createHash('sha256').update(body).digest('hex'),
|
|
].join('\n');
|
|
|
|
// 创建字符串待签名
|
|
const stringToSign = [
|
|
'AWS4-HMAC-SHA256',
|
|
date.toISOString().replace(/[:\-]|\.\d{3}/g, ''),
|
|
date.toISOString().substr(0, 10).replace(/-/g, '') + '/' + region + '/' + service + '/aws4_request',
|
|
crypto.createHash('sha256').update(canonicalRequest).digest('hex'),
|
|
].join('\n');
|
|
|
|
// 计算签名
|
|
const kDate = crypto
|
|
.createHmac('sha256', 'AWS4' + config.secretAccessKey)
|
|
.update(date.toISOString().substr(0, 10).replace(/-/g, ''))
|
|
.digest();
|
|
const kRegion = crypto.createHmac('sha256', kDate).update(region).digest();
|
|
const kService = crypto.createHmac('sha256', kRegion).update(service).digest();
|
|
const kSigning = crypto.createHmac('sha256', kService).update('aws4_request').digest();
|
|
const signature = crypto.createHmac('sha256', kSigning).update(stringToSign).digest('hex');
|
|
|
|
return signature;
|
|
}
|
|
|
|
// 测试基本连接
|
|
async function testConnection() {
|
|
console.log('🔍 测试MinIO基本连接...\n');
|
|
|
|
const options = {
|
|
hostname: 'localhost',
|
|
port: 9000,
|
|
path: '/',
|
|
method: 'GET',
|
|
};
|
|
|
|
return new Promise((resolve, reject) => {
|
|
const req = http.request(options, (res) => {
|
|
console.log(`状态码: ${res.statusCode}`);
|
|
console.log(`响应头:`, res.headers);
|
|
|
|
let data = '';
|
|
res.on('data', (chunk) => (data += chunk));
|
|
res.on('end', () => {
|
|
console.log('响应内容:', data);
|
|
resolve({ statusCode: res.statusCode, data });
|
|
});
|
|
});
|
|
|
|
req.on('error', reject);
|
|
req.end();
|
|
});
|
|
}
|
|
|
|
// 测试bucket列表
|
|
async function testListBuckets() {
|
|
console.log('\n📂 测试列出bucket...\n');
|
|
|
|
const date = new Date();
|
|
const headers = {
|
|
Host: config.endpoint,
|
|
'X-Amz-Date': date.toISOString().replace(/[:\-]|\.\d{3}/g, ''),
|
|
Authorization: `AWS4-HMAC-SHA256 Credential=${config.accessKeyId}/${date.toISOString().substr(0, 10).replace(/-/g, '')}/us-east-1/s3/aws4_request, SignedHeaders=host;x-amz-date, Signature=placeholder`,
|
|
};
|
|
|
|
const options = {
|
|
hostname: 'localhost',
|
|
port: 9000,
|
|
path: '/',
|
|
method: 'GET',
|
|
headers: headers,
|
|
};
|
|
|
|
return new Promise((resolve, reject) => {
|
|
const req = http.request(options, (res) => {
|
|
console.log(`状态码: ${res.statusCode}`);
|
|
console.log(`响应头:`, res.headers);
|
|
|
|
let data = '';
|
|
res.on('data', (chunk) => (data += chunk));
|
|
res.on('end', () => {
|
|
console.log('响应内容:', data);
|
|
resolve({ statusCode: res.statusCode, data });
|
|
});
|
|
});
|
|
|
|
req.on('error', reject);
|
|
req.end();
|
|
});
|
|
}
|
|
|
|
// 测试创建bucket
|
|
async function testCreateBucket() {
|
|
console.log(`\n🪣 测试创建bucket: ${config.bucket}...\n`);
|
|
|
|
const options = {
|
|
hostname: 'localhost',
|
|
port: 9000,
|
|
path: `/${config.bucket}`,
|
|
method: 'PUT',
|
|
};
|
|
|
|
return new Promise((resolve, reject) => {
|
|
const req = http.request(options, (res) => {
|
|
console.log(`状态码: ${res.statusCode}`);
|
|
console.log(`响应头:`, res.headers);
|
|
|
|
let data = '';
|
|
res.on('data', (chunk) => (data += chunk));
|
|
res.on('end', () => {
|
|
console.log('响应内容:', data);
|
|
resolve({ statusCode: res.statusCode, data });
|
|
});
|
|
});
|
|
|
|
req.on('error', reject);
|
|
req.end();
|
|
});
|
|
}
|
|
|
|
async function main() {
|
|
try {
|
|
await testConnection();
|
|
await testListBuckets();
|
|
await testCreateBucket();
|
|
|
|
console.log('\n✅ 测试完成!');
|
|
} catch (error) {
|
|
console.error('❌ 测试失败:', error.message);
|
|
}
|
|
}
|
|
|
|
main();
|