feat: create minio bucket with policy

This commit is contained in:
karishmas6
2024-10-30 03:58:02 +05:30
parent 6e62ae536b
commit 0d4680d328

View File

@@ -21,6 +21,38 @@ minioClient.bucketExists('maxun-test')
console.error('Error connecting to MinIO:', err);
})
async function createBucketWithPolicy(bucketName: string, policy?: 'public-read' | 'private') {
try {
const bucketExists = await minioClient.bucketExists(bucketName);
if (!bucketExists) {
await minioClient.makeBucket(bucketName);
console.log(`Bucket ${bucketName} created successfully.`);
if (policy === 'public-read') {
// Define a public-read policy
const policyJSON = {
Version: "2012-10-17",
Statement: [
{
Effect: "Allow",
Principal: "*",
Action: ["s3:GetObject"],
Resource: [`arn:aws:s3:::${bucketName}/*`]
}
]
};
await minioClient.setBucketPolicy(bucketName, JSON.stringify(policyJSON));
console.log(`Public-read policy applied to bucket ${bucketName}.`);
}
} else {
console.log(`Bucket ${bucketName} already exists.`);
}
} catch (error) {
console.error('Error in bucket creation or policy application:', error);
}
}
class BinaryOutputService {
private bucketName: string;