完成任务16:编写 04_长期发展/环境保护.md
This commit is contained in:
137
skills/minio-active/scripts/list_public_urls.py
Normal file
137
skills/minio-active/scripts/list_public_urls.py
Normal file
@@ -0,0 +1,137 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
列出 MinIO 存储桶中所有对象的公网访问地址
|
||||
|
||||
环境变量:
|
||||
MINIO_ENDPOINT: MinIO 服务地址 (例如: http://192.168.1.1:9000)
|
||||
MINIO_ACCESS_KEY: 访问密钥
|
||||
MINIO_SECRET_KEY: 密钥
|
||||
MINIO_PUBLIC_URL: 公网访问域名 (可选,默认使用 MINIO_ENDPOINT)
|
||||
|
||||
用法:
|
||||
python3 list_public_urls.py [bucket_name] [--prefix path/]
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
from urllib.parse import quote
|
||||
|
||||
def main():
|
||||
# 获取环境变量
|
||||
endpoint = os.environ.get('MINIO_ENDPOINT', '')
|
||||
access_key = os.environ.get('MINIO_ACCESS_KEY', '')
|
||||
secret_key = os.environ.get('MINIO_SECRET_KEY', '')
|
||||
public_url = os.environ.get('MINIO_PUBLIC_URL', endpoint).rstrip('/')
|
||||
|
||||
# 检查环境变量
|
||||
if not all([endpoint, access_key, secret_key]):
|
||||
print("错误: 请确保以下环境变量已配置:")
|
||||
print(" - MINIO_ENDPOINT")
|
||||
print(" - MINIO_ACCESS_KEY")
|
||||
print(" - MINIO_SECRET_KEY")
|
||||
sys.exit(1)
|
||||
|
||||
# 解析命令行参数
|
||||
bucket_filter = None
|
||||
prefix_filter = ''
|
||||
|
||||
args = sys.argv[1:]
|
||||
i = 0
|
||||
while i < len(args):
|
||||
if args[i] == '--prefix' and i + 1 < len(args):
|
||||
prefix_filter = args[i + 1]
|
||||
i += 2
|
||||
elif not args[i].startswith('--'):
|
||||
bucket_filter = args[i]
|
||||
i += 1
|
||||
else:
|
||||
i += 1
|
||||
|
||||
try:
|
||||
import boto3
|
||||
from botocore.client import Config
|
||||
except ImportError:
|
||||
print("正在安装 boto3...")
|
||||
os.system("pip3 install boto3 -q")
|
||||
import boto3
|
||||
from botocore.client import Config
|
||||
|
||||
# 创建客户端
|
||||
s3 = boto3.client(
|
||||
's3',
|
||||
endpoint_url=endpoint,
|
||||
aws_access_key_id=access_key,
|
||||
aws_secret_access_key=secret_key,
|
||||
config=Config(signature_version='s3v4')
|
||||
)
|
||||
|
||||
print(f"公网访问前缀: {public_url}")
|
||||
print(f"MinIO 端点: {endpoint}")
|
||||
print()
|
||||
|
||||
total_objects = 0
|
||||
total_size = 0
|
||||
|
||||
# 获取桶列表
|
||||
buckets = s3.list_buckets()['Buckets']
|
||||
|
||||
for bucket in buckets:
|
||||
bucket_name = bucket['Name']
|
||||
|
||||
# 过滤桶
|
||||
if bucket_filter and bucket_name != bucket_filter:
|
||||
continue
|
||||
|
||||
bucket_objects = 0
|
||||
bucket_size = 0
|
||||
|
||||
print(f"=== 桶: {bucket_name} ===")
|
||||
|
||||
# 分页列出对象
|
||||
paginator = s3.get_paginator('list_objects_v2')
|
||||
pages = paginator.paginate(Bucket=bucket_name, Prefix=prefix_filter)
|
||||
|
||||
for page in pages:
|
||||
if 'Contents' not in page:
|
||||
continue
|
||||
|
||||
for obj in page['Contents']:
|
||||
key = obj['Key']
|
||||
size = obj['Size']
|
||||
bucket_objects += 1
|
||||
bucket_size += size
|
||||
|
||||
# URL 编码
|
||||
encoded_key = quote(key, safe='/')
|
||||
public_address = f"{public_url}/{bucket_name}/{encoded_key}"
|
||||
|
||||
# 格式化大小
|
||||
if size < 1024:
|
||||
size_str = f"{size} B"
|
||||
elif size < 1024 * 1024:
|
||||
size_str = f"{size / 1024:.1f} KB"
|
||||
elif size < 1024 * 1024 * 1024:
|
||||
size_str = f"{size / 1024 / 1024:.1f} MB"
|
||||
else:
|
||||
size_str = f"{size / 1024 / 1024 / 1024:.1f} GB"
|
||||
|
||||
print(f" [{size_str}] {key}")
|
||||
print(f" -> {public_address}")
|
||||
|
||||
if bucket_objects > 0:
|
||||
print(f"\n 小计: {bucket_objects} 个对象\n")
|
||||
|
||||
total_objects += bucket_objects
|
||||
total_size += bucket_size
|
||||
|
||||
# 总计
|
||||
print("=" * 50)
|
||||
if total_size < 1024 * 1024 * 1024:
|
||||
total_size_str = f"{total_size / 1024 / 1024:.2f} MB"
|
||||
else:
|
||||
total_size_str = f"{total_size / 1024 / 1024 / 1024:.2f} GB"
|
||||
|
||||
print(f"总计: {total_objects} 个对象, {total_size_str}")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user