赞
踩
Java 版的参考:https://blog.csdn.net/qq_33811662/article/details/80710318
使用 python 向 s3 上传文件,首先需要导入 aws 官方提供的 sdk,即是 boto3
pip3 install boto3
然后需要有一个 aws 的 s3 权限的一个 IAM 帐号,需要得到 access_key 和 secret_key
from boto3.session import Session
aws_key = “xxxxxxxx”# 【你的 aws_access_key】
aws_secret = “xxxxxxxx” # 【你的 aws_secret_key】
session = Session(aws_access_key_id=aws_key,
aws_secret_access_key=aws_secret,
region_name=”us-east-1”) # 此处根据自己的 s3 地区位置改变
s3 = session.resource(“s3”)
client = session.client(“s3”)
bucket = “xxxxxxxx” # 【你 bucket 的名字】 # 首先需要保证 s3 上已经存在该存储桶,否则报错
upload_data = open(“c:/test.txt”, “rb”)
upload_key = “test”
file_obj = s3.Bucket(bucket).put_object(Key=upload_key, Body=upload_data)
print(file_obj)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。