This commit is contained in:
熊潇 2025-03-17 22:18:09 +08:00
commit 49fc2445d8
8 changed files with 2274 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.env
node_modules

20
package.json Normal file
View 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

File diff suppressed because it is too large Load Diff

3
readme.md Normal file
View File

@ -0,0 +1,3 @@
# 测试cos的sdk
关于minio和aws s3和腾讯云cos的sdk测试

49
src/index.cjs Normal file
View 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
View 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
View 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();

BIN
stars.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 121 KiB