cos-test/src/minio/index.cjs
2025-03-17 22:18:09 +08:00

58 lines
1.7 KiB
JavaScript

const Minio = require('minio');
const dotenv = require('dotenv');
dotenv.config();
// Create a new MinIO client
const minioClient = new Minio.Client({
endPoint: 'kevisual-1252152609.cos.ap-shanghai.myqcloud.com', // Replace with your MinIO server endpoint
// port: 9000, // Replace with your MinIO server port
useSSL: true, // Set to false if not using SSL
accessKey: process.env.ACCESS_KEY_ID, // Replace with your access key
secretKey: process.env.SECRET_ACCESS_KEY, // Replace with your secret key
region: 'ap-shanghai',
});
// Function to get a file from MinIO
async function getFile(bucketName, objectName, downloadPath) {
// const res = await minioClient.fGetObject(bucketName, objectName, downloadPath);
const res = await minioClient.getObject(bucketName, objectName, downloadPath);
console.log(res);
return res;
}
// Example usage
// getFile('my-bucket', 'my-object.txt', '/path/to/download/my-object.txt');
getFile('kevisual', 'stars.jpg', './stars.jpg');
async function getMinioList(bucketName, prefix, recursive) {
return await new Promise((resolve, reject) => {
let res = [];
let hasError = false;
minioClient
// .listObjectsV2(bucketName, prefix, recursive)
.listObjectsV2(bucketName, 'kevisual/')
.on('data', (data) => {
res.push(data);
})
.on('error', (err) => {
console.error('minio error', prefix, err);
hasError = true;
})
.on('end', () => {
if (hasError) {
reject();
return;
} else {
resolve(res);
}
});
});
};
async function main() {
const res = await getMinioList('kevisual', 'kevisual', false);
console.log(res);
}
// main();