boto3 简单使用

使用 boto3 下载文件

1
pip install boto3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from boto3.session import Session
from botocore.config import Config
import os # 使用代理
os.environ["HTTP_PROXY"] = "http://proxy:9999"
os.environ["HTTPS_PROXY"] = "https://proxy:9999"

# Client初始化
access_key = "username"
secret_key = "password"
url = "http://localhost:9000"

session = Session(access_key, secret_key)
session = Session(access_key, secret_key)
s3_client = session.client('s3', endpoint_url=url)
# Client初始化结束
# 列出该用户拥有的桶
print([bucket['Name'] for bucket in s3_client.list_buckets()['Buckets']])

# 列举文件
resp = s3_client.list_objects(Bucket="data", Delimiter='/', Prefix='spider/')
for path in resp.get("CommonPrefixes"):
print(path.get("Prefix"))

# 下载文件
resp = s3_client.get_object(Bucket="data", Key="2.pdf")
with open("aa.pdf", "wb") as f:
f.write(resp["Body"].read())