You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
3.6 KiB
JavaScript

// Script to test S3 connection
import { S3Client, ListBucketsCommand } from "@aws-sdk/client-s3";
import dotenv from "dotenv";
import { fileURLToPath } from "node:url";
import { dirname, resolve } from "node:path";
// Get the directory name of the current module
const __dirname = dirname(fileURLToPath(import.meta.url));
// Load environment variables from .env file
dotenv.config({ path: resolve(__dirname, "../.env") });
// Check if required environment variables are set
const requiredEnvVars = [
"S3_ACCESS_KEY",
"S3_SECRET_KEY",
"S3_REGION",
"S3_BUCKET_NAME",
];
const missingEnvVars = requiredEnvVars.filter(
(varName) => !process.env[varName]
);
if (missingEnvVars.length > 0) {
console.error("❌ Missing required environment variables:");
for (const varName of missingEnvVars) {
console.error(` - ${varName}`);
}
console.error(
"\nPlease check your .env file and make sure all required variables are set."
);
process.exit(1);
}
// Initialize S3 client with environment variables
const s3ClientOptions = {
region: process.env.S3_REGION,
credentials: {
accessKeyId: process.env.S3_ACCESS_KEY,
secretAccessKey: process.env.S3_SECRET_KEY,
},
};
// Add endpoint URL for S3-compatible services like Backblaze B2
if (process.env.S3_ENDPOINT) {
console.log(`🔧 Using custom S3 endpoint: ${process.env.S3_ENDPOINT}`);
s3ClientOptions.endpoint = process.env.S3_ENDPOINT;
// For some S3-compatible services, we need to force path style addressing
if (process.env.S3_FORCE_PATH_STYLE === "true") {
console.log("🔧 Using path style addressing");
s3ClientOptions.forcePathStyle = true;
}
}
const s3Client = new S3Client(s3ClientOptions);
const bucketName = process.env.S3_BUCKET_NAME;
async function testS3Connection() {
console.log("🔍 Testing S3 connection...");
try {
// Test listing buckets to verify credentials
const listBucketsCommand = new ListBucketsCommand({});
const listBucketsResponse = await s3Client.send(listBucketsCommand);
console.log("✅ Successfully connected to S3!");
console.log(
`📋 Found ${
listBucketsResponse.Buckets?.length || 0
} buckets in your account.`
);
// Check if the specified bucket exists
const bucketExists = listBucketsResponse.Buckets?.some(
(bucket) => bucket.Name === bucketName
);
if (bucketExists) {
console.log(`✅ Bucket "${bucketName}" exists and is accessible.`);
} else {
console.error(`❌ Bucket "${bucketName}" was not found in your account.`);
console.error("Please check your S3_BUCKET_NAME environment variable.");
process.exit(1);
}
console.log("\n🎉 S3 connection test completed successfully!");
console.log("You can now run the application with: npm run dev");
} catch (error) {
console.error("❌ Failed to connect to S3:");
console.error(error.message);
if (error.Code === "InvalidAccessKeyId") {
console.error(
"\nThe access key ID you provided does not exist in our records."
);
console.error("Please check your S3_ACCESS_KEY environment variable.");
} else if (error.Code === "SignatureDoesNotMatch") {
console.error(
"\nThe request signature we calculated does not match the signature you provided."
);
console.error("Please check your S3_SECRET_KEY environment variable.");
} else if (error.Code === "AccessDenied") {
console.error(
"\nAccess denied. Your credentials may not have permission to list buckets."
);
console.error("Please check your IAM permissions.");
}
process.exit(1);
}
}
testS3Connection();