saas-smith
Version:
CLI to forge new SaaS projects effortlessly from boilerplates
88 lines (76 loc) • 2.32 kB
JavaScript
// Suppress specific warning about AWS SDK v2 maintenance mode
const originalEmitWarning = process.emitWarning;
process.emitWarning = (warning, ...args) => {
if (
typeof warning === "string" &&
warning.includes("AWS SDK for JavaScript (v2) is in maintenance mode")
) {
return;
}
originalEmitWarning(warning, ...args);
};
import chalk from "chalk";
import AWS from "aws-sdk";
import path from "path";
import dotenv from "dotenv";
const rootDrive = path.parse(process.cwd()).root;
dotenv.config({ path: path.join(rootDrive, ".env") });
export async function setupAWSS3(
bucketName,
awsRegion,
awsAccessKeyId,
awsSecretAccessKey
) {
const bucketPolicy = JSON.stringify({
Version: "2012-10-17",
Statement: [
{
Sid: "PublicReadGetObject",
Effect: "Allow",
Principal: "*",
Action: "s3:GetObject",
Resource: `arn:aws:s3:::${bucketName}/Files/*`,
},
],
});
if (!awsAccessKeyId || !awsSecretAccessKey) {
console.error(
chalk.red("AWS credentials are not set in environment variables.")
);
return;
}
console.log(chalk.blue("Setting up AWS S3..."));
AWS.config.update({
accessKeyId: awsAccessKeyId,
secretAccessKey: awsSecretAccessKey,
region: awsRegion,
});
const s3 = new AWS.S3();
try {
await s3.createBucket({ Bucket: bucketName }).promise();
console.log(chalk.green(`Bucket ${bucketName} created successfully.`));
const publicAccessBlockParams = {
Bucket: bucketName,
PublicAccessBlockConfiguration: {
BlockPublicAcls: true,
IgnorePublicAcls: true,
BlockPublicPolicy: false,
RestrictPublicBuckets: false,
},
};
await s3.putPublicAccessBlock(publicAccessBlockParams).promise();
console.log(
chalk.green(`Public access block settings applied successfully.`)
);
const policyParams = {
Bucket: bucketName,
Policy: bucketPolicy,
};
await s3.putBucketPolicy(policyParams).promise();
console.log(chalk.green(`Bucket policy applied successfully.`));
} catch (error) {
console.error(
chalk.red(`Error creating bucket or applying policy: ${error.message}`)
);
}
}