引言
在最近的测试工作中,接触到了S3,我们需要在S3上上传,下载文件从而对文件的数据进行验证,自己也踩了很多坑,查阅了很多文档,接下来把源码分享给大家,一定可用。
from boto3.session import Session
from botocore.exceptions import ClientError
class Test_demo():
def __init__(self):
access_key = "xxxxxxxxxxx"
secret_key = "xxxxxxxxx"
url = "http://172.xx.xx.xxx" # 也可以是自己节点的地址
session = Session(access_key, secret_key)
self.s3_client = session.client('s3', endpoint_url=url)
def download_file(self,bucketName,objectName,fileName):
"""
下载文件
:param bucketName: 桶的名称
:param objectName: 文件的路径
:param fileName: 下载完成的文件的名称----注意:下载之后的文件默认储存在自己的python工程路径下
:return:
"""
self.s3_client.download_file(bucketName,objectName,fileName)
def upload_file(self,file_name, bucket, object_name=None):
"""
上传文件
:param file_name: 需要上传的文件的名称
:param bucket: S3中桶的名称
:param object_name: 需要上传到的路径,例如file/localfile/test
:return:
"""
if object_name is None:
object_name = file_name
try:
self.s3_client.upload_file(file_name, bucket, object_name,ExtraArgs = { 'ACL' : 'public-read' } )
except ClientError as e:
logging.error(e)
return False
return True
def list_object(self, bucketName):
"""
列出当前桶下所有的文件
:param bucketName:
:return:
"""
file_list = []
response = self.s3_client.list_objects_v2(
Bucket=bucketName,
# MaxKeys=1000 # 返回数量,如果为空则为全部
)
file_desc = response['Contents']
for f in file_desc:
print('file_name:{},file_size:{}'.format(f['Key'], f['Size']))
file_list.append(f['Key'])
return file_list
1995




被折叠的 条评论
为什么被折叠?



