赞
踩
pip install boto3
import boto3
s3_endpoint = "http://*.*.*.*:*"
s3_access_key = "*"
s3_secret_key = "*"
def init_s3(end_point, access_key, secret_key):
return boto3.client(
's3',
aws_access_key_id=access_key,
aws_secret_access_key=secret_key,
use_ssl=True,
region_name='us-sast-1',
endpoint_url=end_point,
config=Config(s3={"addressing_style: "path"})
)
def get_file(s3, bucket_name, filename):
return s3.get_object(
Bucket=bucket_name,
Key=filename,
)
def put_file(s3, bucket_name, filename, upfile):
return s3.put_object(
Bucket=bucket_name,
Body=open(upfile, 'rb'),
Key=filename,
)
def have_bucket(s3, bucket_name):
buckets = s3.list_buckets()['Buckets']
for bucket in buckets:
if bucket_name == bucket['Name']:
return True
return False
s3 = init_s3(s3_endpoint, s3_access_key, s3_secret_key)
file_name = 'test.txt'
file_bucket_name = 'test-bucket'
if not have_bucket(s3, file_bucket_name):
raise Exception('Error')
# 下载S3上的文件
src_file = getfile(s3, file_bucket_name, file_name)[Body]
# 上传本地文件到S3
put_file(s3, file_bucket_name, 'test2.txt', 'D:\\test2.txt')
1、如果 s3_access_key 或 s3_secret_key 不对,会返回 403
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。