This commit is contained in:
熊潇 2025-05-21 01:09:54 +08:00
parent 49fc2445d8
commit d99aafb79a
5 changed files with 1247 additions and 703 deletions

View File

@ -9,12 +9,13 @@
"keywords": [], "keywords": [],
"author": "abearxiong <xiongxiao@xiongxiao.me> (https://www.xiongxiao.me)", "author": "abearxiong <xiongxiao@xiongxiao.me> (https://www.xiongxiao.me)",
"license": "MIT", "license": "MIT",
"packageManager": "pnpm@10.6.2", "packageManager": "pnpm@10.11.0",
"type": "module", "type": "module",
"dependencies": { "dependencies": {
"@aws-sdk/client-s3": "^3.758.0", "@aws-sdk/client-s3": "^3.812.0",
"dotenv": "^16.4.7", "cos-nodejs-sdk-v5": "^2.14.7",
"express": "^4.21.2", "dotenv": "^16.5.0",
"express": "^5.1.0",
"minio": "^8.0.5" "minio": "^8.0.5"
} }
} }

1896
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

22
src/cos.ts Normal file
View File

@ -0,0 +1,22 @@
import COS from 'cos-nodejs-sdk-v5';
import dotenv from 'dotenv';
dotenv.config();
const cos = new COS({
SecretId: process.env.ACCESS_KEY_ID, // 推荐使用环境变量获取;用户的 SecretId建议使用子账号密钥授权遵循最小权限指引降低使用风险。子账号密钥获取可参考https://cloud.tencent.com/document/product/598/37140
SecretKey: process.env.SECRET_ACCESS_KEY, // 推荐使用环境变量获取;用户的 SecretKey建议使用子账号密钥授权遵循最小权限指引降低使用风险。子账号密钥获取可参考https://cloud.tencent.com/document/product/598/37140
});
const bucket = process.env.BUCKET; // Bucket 格式test-1250000000
const region = process.env.REGION; // Bucket 所在地域ap-guangzhou
console.log('bucket', bucket, process.env.ACCESS_KEY_ID, process.env.SECRET_ACCESS_KEY, region);
cos.getBucket(
{
Bucket: bucket! /* 必须 */,
Region: region! /* 必须 */,
Prefix: 'test2/' /* 非必须 */,
},
function (err, data) {
console.log(err || data.Contents);
},
);

View File

@ -4,13 +4,14 @@ dotenv.config();
// Create a new MinIO client // Create a new MinIO client
const minioClient = new Minio.Client({ const minioClient = new Minio.Client({
endPoint: 'kevisual-1252152609.cos.ap-shanghai.myqcloud.com', // Replace with your MinIO server endpoint endPoint: process.env.env.ENDPOINT, // Replace with your MinIO server endpoint
// port: 9000, // Replace with your MinIO server port // port: 9000, // Replace with your MinIO server port
useSSL: true, // Set to false if not using SSL useSSL: true, // Set to false if not using SSL
accessKey: process.env.ACCESS_KEY_ID, // Replace with your access key accessKey: process.env.ACCESS_KEY_ID, // Replace with your access key
secretKey: process.env.SECRET_ACCESS_KEY, // Replace with your secret key secretKey: process.env.SECRET_ACCESS_KEY, // Replace with your secret key
region: 'ap-shanghai', region: process.env.REGION,
}); });
const bucketName = process.env.BUCKET_NAME;
// Function to get a file from MinIO // Function to get a file from MinIO
async function getFile(bucketName, objectName, downloadPath) { async function getFile(bucketName, objectName, downloadPath) {
@ -23,7 +24,7 @@ async function getFile(bucketName, objectName, downloadPath) {
// Example usage // Example usage
// getFile('my-bucket', 'my-object.txt', '/path/to/download/my-object.txt'); // getFile('my-bucket', 'my-object.txt', '/path/to/download/my-object.txt');
getFile('kevisual', 'stars.jpg', './stars.jpg'); getFile(bucketName, 'stars.jpg', './stars.jpg');
async function getMinioList(bucketName, prefix, recursive) { async function getMinioList(bucketName, prefix, recursive) {
return await new Promise((resolve, reject) => { return await new Promise((resolve, reject) => {
@ -31,7 +32,7 @@ async function getMinioList(bucketName, prefix, recursive) {
let hasError = false; let hasError = false;
minioClient minioClient
// .listObjectsV2(bucketName, prefix, recursive) // .listObjectsV2(bucketName, prefix, recursive)
.listObjectsV2(bucketName, 'kevisual/') .listObjectsV2(bucketName, bucketName + '/')
.on('data', (data) => { .on('data', (data) => {
res.push(data); res.push(data);
}) })
@ -48,10 +49,9 @@ async function getMinioList(bucketName, prefix, recursive) {
} }
}); });
}); });
}; }
async function main() { async function main() {
const res = await getMinioList('kevisual', 'kevisual', false); const res = await getMinioList(bucketName, bucketName, false);
console.log(res); console.log(res);
} }
// main(); main();

View File

@ -1,17 +1,18 @@
const { S3Client, PutObjectCommand, GetObjectCommand, ListObjectsCommand, HeadObjectCommand } = require("@aws-sdk/client-s3"); const { S3Client, PutObjectCommand, GetObjectCommand, ListObjectsCommand, HeadObjectCommand } = require("@aws-sdk/client-s3");
const dotenv = require('dotenv'); const dotenv = require('dotenv');
dotenv.config(); dotenv.config();
console.log(process.env);
// 腾讯云 COS 配置 // 腾讯云 COS 配置
const cosConfig = { const cosConfig = {
endpoint: "https://kevisual-1252152609.cos.ap-shanghai.myqcloud.com", // 替换为你的 COS 端点 endpoint: process.env.ENDPOINT, // 替换为你的 COS 端点
forcePathStyle: true, // 对于 COS 必须设置为 true forcePathStyle: true, // 对于 COS 必须设置为 true
credentials: { credentials: {
accessKeyId: process.env.ACCESS_KEY_ID, accessKeyId: process.env.ACCESS_KEY_ID,
secretAccessKey: process.env.SECRET_ACCESS_KEY, secretAccessKey: process.env.SECRET_ACCESS_KEY,
}, },
region: "ap-shanghai", // 替换为你的 COS 区域 region: process.env.REGION, // 替换为你的 COS 区域
}; };
const bucket = 'kevisual'; const bucket = process.env.BUCKET_NAME;
const s3Client = new S3Client(cosConfig); const s3Client = new S3Client(cosConfig);
const getBucketFiles = async () => { const getBucketFiles = async () => {
const command = new ListObjectsCommand({ const command = new ListObjectsCommand({