temp
This commit is contained in:
commit
49fc2445d8
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
.env
|
||||
node_modules
|
20
package.json
Normal file
20
package.json
Normal file
@ -0,0 +1,20 @@
|
||||
{
|
||||
"name": "cos-test",
|
||||
"version": "0.0.1",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "abearxiong <xiongxiao@xiongxiao.me> (https://www.xiongxiao.me)",
|
||||
"license": "MIT",
|
||||
"packageManager": "pnpm@10.6.2",
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
"@aws-sdk/client-s3": "^3.758.0",
|
||||
"dotenv": "^16.4.7",
|
||||
"express": "^4.21.2",
|
||||
"minio": "^8.0.5"
|
||||
}
|
||||
}
|
2103
pnpm-lock.yaml
generated
Normal file
2103
pnpm-lock.yaml
generated
Normal file
File diff suppressed because it is too large
Load Diff
49
src/index.cjs
Normal file
49
src/index.cjs
Normal file
@ -0,0 +1,49 @@
|
||||
const { S3Client, ListObjectsV2Command, HeadObjectCommand } = require('@aws-sdk/client-s3');
|
||||
const dotenv = require('dotenv');
|
||||
dotenv.config();
|
||||
|
||||
const s3Client = new S3Client({
|
||||
endpoint: 'https://minio.xiongxiao.me', // MinIO 端点
|
||||
forcePathStyle: true, // 对于 MinIO 必须设置为 true
|
||||
credentials: {
|
||||
accessKeyId: process.env.ACCESS_KEY_ID,
|
||||
secretAccessKey: process.env.SECRET_ACCESS_KEY,
|
||||
},
|
||||
region: 'us-east-1', // 任意区域
|
||||
});
|
||||
|
||||
const bucket = 'resources';
|
||||
// 获取bucket下的所有文件
|
||||
const getBucketFiles = async () => {
|
||||
const command = new ListObjectsV2Command({
|
||||
Bucket: bucket,
|
||||
});
|
||||
const response = await s3Client.send(command);
|
||||
return response.Contents;
|
||||
};
|
||||
const getBucketStats = async () => {
|
||||
const command = new ListObjectsV2Command({
|
||||
Bucket: bucket,
|
||||
});
|
||||
const response = await s3Client.send(command);
|
||||
return response.Contents;
|
||||
};
|
||||
|
||||
const getFileStats = async (key) => {
|
||||
const command = new HeadObjectCommand({
|
||||
Bucket: bucket,
|
||||
Key: key,
|
||||
});
|
||||
const response = await s3Client.send(command);
|
||||
return response;
|
||||
};
|
||||
|
||||
const main = async () => {
|
||||
// const list = await getBucketFiles();
|
||||
// console.log(list);
|
||||
|
||||
// Get stats for a specific file
|
||||
const fileStats = await getFileStats('docs/assets/flow-01.png');
|
||||
console.log(fileStats);
|
||||
};
|
||||
main();
|
57
src/minio/index.cjs
Normal file
57
src/minio/index.cjs
Normal file
@ -0,0 +1,57 @@
|
||||
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();
|
||||
|
40
src/tencent.cjs
Normal file
40
src/tencent.cjs
Normal file
@ -0,0 +1,40 @@
|
||||
const { S3Client, PutObjectCommand, GetObjectCommand, ListObjectsCommand, HeadObjectCommand } = require("@aws-sdk/client-s3");
|
||||
const dotenv = require('dotenv');
|
||||
dotenv.config();
|
||||
// 腾讯云 COS 配置
|
||||
const cosConfig = {
|
||||
endpoint: "https://kevisual-1252152609.cos.ap-shanghai.myqcloud.com", // 替换为你的 COS 端点
|
||||
forcePathStyle: true, // 对于 COS 必须设置为 true
|
||||
credentials: {
|
||||
accessKeyId: process.env.ACCESS_KEY_ID,
|
||||
secretAccessKey: process.env.SECRET_ACCESS_KEY,
|
||||
},
|
||||
region: "ap-shanghai", // 替换为你的 COS 区域
|
||||
};
|
||||
const bucket = 'kevisual';
|
||||
const s3Client = new S3Client(cosConfig);
|
||||
const getBucketFiles = async () => {
|
||||
const command = new ListObjectsCommand({
|
||||
Bucket: bucket,
|
||||
});
|
||||
const response = await s3Client.send(command);
|
||||
console.log(response);
|
||||
return response.Contents;
|
||||
};
|
||||
const getFileStats = async (key) => {
|
||||
const command = new HeadObjectCommand({
|
||||
Bucket: bucket,
|
||||
Key: key,
|
||||
});
|
||||
const response = await s3Client.send(command);
|
||||
return response;
|
||||
};
|
||||
const main = async () => {
|
||||
const list = await getBucketFiles();
|
||||
console.log(list);
|
||||
|
||||
// Get stats for a specific file
|
||||
const fileStats = await getFileStats('stars.jpg');
|
||||
console.log(fileStats);
|
||||
};
|
||||
main();
|
Loading…
x
Reference in New Issue
Block a user