-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-s3-connection.js
More file actions
108 lines (91 loc) · 3.86 KB
/
test-s3-connection.js
File metadata and controls
108 lines (91 loc) · 3.86 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import dotenv from 'dotenv';
import { S3Client, ListObjectsV2Command, PutObjectCommand } from '@aws-sdk/client-s3';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
dotenv.config();
async function testS3Connection() {
console.log('=== S3 Connection Test ===');
// Check environment variables
console.log('\n1. Environment Variables:');
console.log('S3_ACCESS_KEY_ID:', process.env.S3_ACCESS_KEY_ID ? 'SET' : 'NOT SET');
console.log('S3_SECRET_ACCESS_KEY:', process.env.S3_SECRET_ACCESS_KEY ? 'SET' : 'NOT SET');
console.log('S3_BUCKET_NAME:', process.env.S3_BUCKET_NAME || 'NOT SET');
console.log('S3_REGION:', process.env.S3_REGION || 'NOT SET');
console.log('S3_ENDPOINT:', process.env.S3_ENDPOINT || 'NOT SET');
if (!process.env.S3_ACCESS_KEY_ID || !process.env.S3_SECRET_ACCESS_KEY || !process.env.S3_BUCKET_NAME) {
console.error('\n❌ Missing required environment variables!');
return;
}
const region = process.env.S3_REGION || 'us-west-002';
const endpoint = process.env.S3_ENDPOINT || `https://s3.${region}.backblazeb2.com`;
const bucketName = process.env.S3_BUCKET_NAME;
console.log('\n2. Configuration:');
console.log('Region:', region);
console.log('Endpoint:', endpoint);
console.log('Bucket:', bucketName);
// Create S3 client
const s3Client = new S3Client({
region: region,
endpoint: endpoint,
credentials: {
accessKeyId: process.env.S3_ACCESS_KEY_ID,
secretAccessKey: process.env.S3_SECRET_ACCESS_KEY,
},
forcePathStyle: true,
});
try {
// Test 1: List objects in bucket
console.log('\n3. Testing bucket access...');
const listCommand = new ListObjectsV2Command({
Bucket: bucketName,
MaxKeys: 1
});
const listResult = await s3Client.send(listCommand);
console.log('✅ Successfully listed objects in bucket');
console.log('Objects found:', listResult.Contents?.length || 0);
// Test 2: Generate presigned URL
console.log('\n4. Testing presigned URL generation...');
const testFileName = `test-${Date.now()}.txt`;
const putCommand = new PutObjectCommand({
Bucket: bucketName,
Key: testFileName,
ContentType: 'text/plain',
});
const presignedUrl = await getSignedUrl(s3Client, putCommand, {
expiresIn: 3600
});
console.log('✅ Successfully generated presigned URL');
console.log('Presigned URL:', presignedUrl.substring(0, 100) + '...');
// Test 3: Try to upload a test file
console.log('\n5. Testing actual upload...');
const testContent = 'This is a test file for S3 connection';
const uploadResponse = await fetch(presignedUrl, {
method: 'PUT',
headers: {
'Content-Type': 'text/plain',
},
body: testContent
});
if (uploadResponse.ok) {
console.log('✅ Successfully uploaded test file');
} else {
console.log('❌ Upload failed:', uploadResponse.status, uploadResponse.statusText);
const errorText = await uploadResponse.text();
console.log('Error details:', errorText);
}
} catch (error) {
console.error('\n❌ S3 connection failed:');
console.error('Error name:', error.name);
console.error('Error message:', error.message);
console.error('Error code:', error.$metadata?.httpStatusCode);
if (error.message.includes('InvalidAccessKeyId')) {
console.error('\n💡 This suggests your Access Key ID is incorrect');
} else if (error.message.includes('SignatureDoesNotMatch')) {
console.error('\n💡 This suggests your Secret Access Key is incorrect');
} else if (error.message.includes('NoSuchBucket')) {
console.error('\n💡 This suggests your bucket name is incorrect');
} else if (error.message.includes('AccessDenied')) {
console.error('\n💡 This suggests your key lacks the necessary permissions');
}
}
}
testS3Connection().catch(console.error);