获取

npm install --save minio

const Minio = require('minio');
const fs = require('fs');

class BucketManager {
    constructor(minioAddress, minioAdmin, minioPassword) {
        this.minioClient = new Minio.Client({
            endPoint: minioAddress.split(':')[0],
            port: parseInt(minioAddress.split(':')[1] || "80"),
            useSSL: false,
            accessKey: minioAdmin,
            secretKey: minioPassword
        });
    }

    async createBucket(bucketName) {
        try {
            const exists = await this.minioClient.bucketExists(bucketName);
            if (exists) {
                console.log("该存储桶已经存在");
            } else {
                await this.minioClient.makeBucket(bucketName, 'us-east-1');
                console.log("存储桶创建成功");
            }
        } catch (err) {
            console.error(err);
        }
    }

    async listBuckets() {
        try {
            const buckets = await this.minioClient.listBuckets();
            buckets.forEach(bucket => console.log(bucket.name));
        } catch (err) {
            console.error(err);
        }
    }

    async removeBucket(bucketName) {
        try {
            await this.minioClient.removeBucket(bucketName);
            console.log(`删除存储桶 ${bucketName} 成功`);
        } catch (err) {
            console.error(err);
        }
    }

    async uploadFile(bucketName, objectName, filePath) {
        try {
            const exists = await this.minioClient.bucketExists(bucketName);
            if (!exists) {
                await this.minioClient.makeBucket(bucketName, 'us-east-1');
            }
            await this.minioClient.fPutObject(bucketName, objectName, filePath, {});
            console.log('File uploaded successfully');
        } catch (err) {
            console.error(err);
        }
    }

    async downloadFile(bucketName, objectName, filePath) {
        try {
            await this.minioClient.fGetObject(bucketName, objectName, filePath);
            console.log('File downloaded successfully');
        } catch (err) {
            console.error(err);
        }
    }

    async removeObject(bucketName, objectName) {
        try {
            await this.minioClient.removeObject(bucketName, objectName);
            console.log('Object removed successfully');
        } catch (err) {
            console.error(err);
        }
    }
}

module.exports = BucketManager;