-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-direct-upload.js
More file actions
84 lines (66 loc) · 2.72 KB
/
test-direct-upload.js
File metadata and controls
84 lines (66 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const { S3Client, PutObjectCommand, GetObjectCommand } = require('@aws-sdk/client-s3');
const CONFIG = {
CLOUDFLARE_R2_ACCOUNT_ID: "451ec293530ef60a07471a235cf03396",
CLOUDFLARE_R2_ACCESS_KEY_ID: "58d9c313ac4111c31648282612d3fa67",
CLOUDFLARE_R2_SECRET_ACCESS_KEY: "363ac8c44ed8a1193d622966c603cdcc61820695fe6c1c529c2b38d1a819cc2b",
R2_BUCKET_NAME: "veo3-videos"
};
console.log('🎯 R2直接上传测试');
console.log('=' .repeat(40));
console.log('📋 配置:');
console.log(`密钥ID长度: ${CONFIG.CLOUDFLARE_R2_ACCESS_KEY_ID.length} (应该是32)`);
console.log(`密钥长度: ${CONFIG.CLOUDFLARE_R2_SECRET_ACCESS_KEY.length} (应该是64)`);
const endpoint = `https://${CONFIG.CLOUDFLARE_R2_ACCOUNT_ID}.r2.cloudflarestorage.com`;
const r2Client = new S3Client({
region: 'auto',
endpoint: endpoint,
credentials: {
accessKeyId: CONFIG.CLOUDFLARE_R2_ACCESS_KEY_ID,
secretAccessKey: CONFIG.CLOUDFLARE_R2_SECRET_ACCESS_KEY,
},
forcePathStyle: true,
});
async function directUploadTest() {
try {
console.log('\n📤 直接测试上传文件...');
const testKey = `test-direct-${Date.now()}.txt`;
const testContent = 'Hello R2! 直接上传测试';
const uploadCommand = new PutObjectCommand({
Bucket: CONFIG.R2_BUCKET_NAME,
Key: testKey,
Body: testContent,
ContentType: 'text/plain'
});
const uploadResult = await r2Client.send(uploadCommand);
console.log('✅ 上传成功!', uploadResult.ETag);
// 尝试读取验证
console.log('\n📥 验证文件读取...');
const getCommand = new GetObjectCommand({
Bucket: CONFIG.R2_BUCKET_NAME,
Key: testKey
});
const getResult = await r2Client.send(getCommand);
console.log(`✅ 读取成功! 大小: ${getResult.ContentLength} bytes`);
const publicUrl = `${endpoint}/${CONFIG.R2_BUCKET_NAME}/${testKey}`;
console.log(`🔗 文件URL: ${publicUrl}`);
console.log('\n🎉 R2配置完全正确! 所有操作成功!');
return true;
} catch (error) {
console.error(`\n❌ 上传失败: ${error.message}`);
// 详细的错误诊断
if (error.code) {
console.log(`错误代码: ${error.code}`);
}
if (error.message.includes('NoSuchBucket')) {
console.log('💡 桶不存在。请确认桶名: veo3-videos');
} else if (error.message.includes('InvalidAccessKeyId')) {
console.log('💡 Access Key ID 错误');
} else if (error.message.includes('SignatureDoesNotMatch')) {
console.log('💡 Secret Access Key 错误');
} else if (error.message.includes('Unauthorized')) {
console.log('💡 权限不足。API令牌可能没有写入权限');
}
return false;
}
}
directUploadTest();