54 lines
1.5 KiB
Python
54 lines
1.5 KiB
Python
|
|
#!/usr/bin/env python3
|
||
|
|
"""
|
||
|
|
简单的阿里云账号查询
|
||
|
|
"""
|
||
|
|
|
||
|
|
import os
|
||
|
|
from dotenv import load_dotenv
|
||
|
|
|
||
|
|
# 加载环境变量
|
||
|
|
load_dotenv()
|
||
|
|
|
||
|
|
def main():
|
||
|
|
access_key_id = os.getenv('ALIYUN_OSS_ACCESS_KEY_ID')
|
||
|
|
access_key_secret = os.getenv('ALIYUN_OSS_ACCESS_KEY_SECRET')
|
||
|
|
|
||
|
|
print(f"AccessKeyId: {access_key_id}")
|
||
|
|
print(f"AccessKeySecret: {access_key_secret[:8]}***")
|
||
|
|
|
||
|
|
try:
|
||
|
|
import oss2
|
||
|
|
|
||
|
|
# 尝试不同的 endpoint 来列出 buckets
|
||
|
|
endpoints = [
|
||
|
|
'https://oss-cn-hangzhou.aliyuncs.com',
|
||
|
|
'https://oss-cn-beijing.aliyuncs.com',
|
||
|
|
'https://oss-cn-qingdao.aliyuncs.com',
|
||
|
|
'https://oss-cn-shenzhen.aliyuncs.com'
|
||
|
|
]
|
||
|
|
|
||
|
|
auth = oss2.Auth(access_key_id, access_key_secret)
|
||
|
|
|
||
|
|
for endpoint in endpoints:
|
||
|
|
try:
|
||
|
|
print(f"\n尝试 endpoint: {endpoint}")
|
||
|
|
service = oss2.Service(auth, endpoint)
|
||
|
|
buckets = service.list_buckets()
|
||
|
|
|
||
|
|
print(f"✅ 成功连接!找到 {len(buckets.buckets)} 个 Bucket:")
|
||
|
|
for bucket in buckets.buckets:
|
||
|
|
print(f" - {bucket.name} (区域: {bucket.location})")
|
||
|
|
|
||
|
|
break
|
||
|
|
|
||
|
|
except Exception as e:
|
||
|
|
print(f"❌ 失败: {str(e)[:100]}...")
|
||
|
|
continue
|
||
|
|
|
||
|
|
except ImportError:
|
||
|
|
print("请安装: pip install oss2")
|
||
|
|
except Exception as e:
|
||
|
|
print(f"错误: {e}")
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
main()
|