feat: properly apply minio public-read policy
This commit is contained in:
@@ -21,38 +21,39 @@ minioClient.bucketExists('maxun-test')
|
|||||||
console.error('Error connecting to MinIO:', err);
|
console.error('Error connecting to MinIO:', err);
|
||||||
})
|
})
|
||||||
|
|
||||||
async function createBucketWithPolicy(bucketName: string, policy?: 'public-read' | 'private') {
|
async function createBucketWithPolicy(bucketName: string, policy = 'public-read') {
|
||||||
try {
|
try {
|
||||||
const bucketExists = await minioClient.bucketExists(bucketName);
|
const bucketExists = await minioClient.bucketExists(bucketName);
|
||||||
if (!bucketExists) {
|
if (!bucketExists) {
|
||||||
await minioClient.makeBucket(bucketName);
|
await minioClient.makeBucket(bucketName);
|
||||||
console.log(`Bucket ${bucketName} created successfully.`);
|
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 {
|
} else {
|
||||||
console.log(`Bucket ${bucketName} already exists.`);
|
console.log(`Bucket ${bucketName} already exists.`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (policy === 'public-read') {
|
||||||
|
// Apply public-read policy after confirming the bucket exists
|
||||||
|
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}.`);
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error in bucket creation or policy application:', error);
|
console.error('Error in bucket creation or policy application:', error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class BinaryOutputService {
|
class BinaryOutputService {
|
||||||
private bucketName: string;
|
private bucketName: string;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user