ai-business-write/generate_download_urls.py

125 lines
3.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
为指定的文件路径生成 MinIO 预签名下载 URL
"""
import sys
import io
from minio import Minio
from datetime import timedelta
# 设置输出编码为UTF-8避免Windows控制台编码问题
if sys.platform == 'win32':
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8')
# MinIO连接配置
MINIO_CONFIG = {
'endpoint': 'minio.datacubeworld.com:9000',
'access_key': 'JOLXFXny3avFSzB0uRA5',
'secret_key': 'G1BR8jStNfovkfH5ou39EmPl34E4l7dGrnd3Cz0I',
'secure': True
}
BUCKET_NAME = 'finyx'
# 文件相对路径列表
FILE_PATHS = [
'/615873064429507639/20251211101046/1_张三.docx',
'/615873064429507639/20251211101046/1_张三.docx'
]
def generate_download_urls():
"""为文件路径列表生成下载 URL"""
print("="*80)
print("生成 MinIO 下载链接")
print("="*80)
try:
# 创建MinIO客户端
client = Minio(
MINIO_CONFIG['endpoint'],
access_key=MINIO_CONFIG['access_key'],
secret_key=MINIO_CONFIG['secret_key'],
secure=MINIO_CONFIG['secure']
)
print(f"\n存储桶: {BUCKET_NAME}")
print(f"端点: {MINIO_CONFIG['endpoint']}")
print(f"使用HTTPS: {MINIO_CONFIG['secure']}\n")
results = []
for file_path in FILE_PATHS:
# 去掉开头的斜杠,得到对象名称
object_name = file_path.lstrip('/')
print("-"*80)
print(f"文件: {file_path}")
print(f"对象名称: {object_name}")
try:
# 检查文件是否存在
stat = client.stat_object(BUCKET_NAME, object_name)
print(f"[OK] 文件存在")
print(f" 文件大小: {stat.size:,} 字节")
print(f" 最后修改: {stat.last_modified}")
# 生成预签名URL7天有效期
url = client.presigned_get_object(
BUCKET_NAME,
object_name,
expires=timedelta(days=7)
)
print(f"[OK] 预签名URL生成成功7天有效")
print(f"\n下载链接:")
print(f"{url}\n")
results.append({
'file_path': file_path,
'object_name': object_name,
'url': url,
'size': stat.size,
'exists': True
})
except Exception as e:
print(f"[ERROR] 错误: {e}\n")
results.append({
'file_path': file_path,
'object_name': object_name,
'url': None,
'exists': False,
'error': str(e)
})
# 输出汇总
print("\n" + "="*80)
print("下载链接汇总")
print("="*80)
for i, result in enumerate(results, 1):
print(f"\n{i}. {result['file_path']}")
if result['exists']:
print(f" [OK] 文件存在")
print(f" 下载链接: {result['url']}")
else:
print(f" [ERROR] 文件不存在或无法访问")
if 'error' in result:
print(f" 错误: {result['error']}")
print("\n" + "="*80)
print("完成")
print("="*80)
return results
except Exception as e:
print(f"\n[ERROR] 连接MinIO失败: {e}")
import traceback
traceback.print_exc()
return None
if __name__ == '__main__':
generate_download_urls()