直接用Aws   python  (S3  SDK)

上传下载

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @mail    : lshan523@163.com
# @Time    : 2023/5/24 17:35
# @Author  : Sea
# @File    : testUploadS3.py
# @history:
# ****************************
import logging
import re
import boto3

S3_CONF = {
    "ACCESS_KEY": "paZRwWpsuM3vkUuw",
    "SECRET_KEY": "WzLqVt5g9mtM33cQJyQApLOUivXahYeL",
    "ENDPOINT_URL": "http://192.168.18.199:9001",
    "BUCKET_NAME": "sea",
    "REGION_NAME": "cn-north-1"
}


class S3Helper(object):
    """
    需要下载boto3模块
    """

    def __init__(self):
        self.bucket_name = S3_CONF.get("BUCKET_NAME", None)
        self.access_key = S3_CONF.get("ACCESS_KEY", None)
        self.secret_key = S3_CONF.get("SECRET_KEY", None)
        self.endpoint_url = S3_CONF.get("ENDPOINT_URL", None)
        self.region_name = S3_CONF.get("REGION_NAME", None)
        self.client = boto3.client(service_name='s3', endpoint_url=self.endpoint_url, region_name=self.region_name,
                                   aws_access_key_id=self.access_key, aws_secret_access_key=self.secret_key)

    def download_file_s3(self, file_name, local_file, bucket_name=None):
        """
        从s3下载指定文件到本地
        需要本地运行程序的目录下新建一个local_file完整目录
        :param bucket_name: 桶名称
        :param file_name: 要下载的文件,所在路径
        :return: 下载完成返回True,下载出问题返回False,并打印错误
        """
        if bucket_name is None:
            bucket_name = self.bucket_name
        try:
            ret = self.client.download_file(bucket_name, file_name, local_file)
            logging.info(ret)
            return True
        except Exception as e:
            logging.error(f'download from s3 Error endpoint:{self.endpoint}, bucket_name:{bucket_name},'
                          f' file_name:{file_name},local_file:{local_file}, Exception:{e}')
            return False

    def get_list_s3(self, file_name, bucket_name=None):
        """
        用来列举出该目录下的所有文件
        :param bucket_name: 桶名称
        :param file_name: 要查询的文件夹
        :return: 该目录下所有文件列表
        """
        # 用来存放文件列表
        file_list = []

        if bucket_name is None:
            bucket_name = self.bucket_name
        response = self.client.list_objects_v2(
            Bucket=bucket_name,
            Delimiter='/',
            Prefix=file_name,
        )

        for file in response['Contents']:
            s = str(file['Key'])
            p = re.compile(r'.*/(.*)(\..*)')
            if p.search(s):
                s1 = p.search(s).group(1)
                s2 = p.search(s).group(2)
                result = s1 + s2
                file_list.append(result)
        return file_list

    def upload_file_s3(self, filepath, s3_key, bucket_name=None):
        """
        :param s3_key:
        :param bucket_name:
        :param filepath  /home/sea/xx.png
        :param s3_dir_and_name   /pic/xx.png
        """
        if not bucket_name:
            bucket_name = self.bucket_name
        try:
            self.client.upload_file(Filename=filepath, Bucket=bucket_name, Key=s3_key, Callback=self.s3back,
                                    ExtraArgs={'ACL': 'public-read'})
        except Exception as e:
            print('出错了:' + str(e))
            raise e
        return s3_key

    def s3back(self, res):
        pass
        print("【aws回调函数】", res)


if __name__ == '__main__':
    helper = S3Helper()
    helper.upload_file_s3(filepath="C:/Users/Sea/Downloads/LEX.xlsx", s3_key="xxxxxx.xlsx", bucket_name="sea")
    print("over ")

 

 

创建一个 bucket
if __name__ == '__main__':
    s3 = boto3.resource("s3", aws_access_key_id="paZRwWpsuM3vkUuw", aws_secret_access_key="WzLqVt5g9mtM33cQJyQApLOUivXahYeL", endpoint_url="http://192.168.18.199:9001")
    # 创建一个 bucket
    # bucket = s3.create_bucket(Bucket="my-bucket")
    # 获得所有的 buckets
    for bucket in s3.buckets.all():
        print(bucket.name)

 

posted on 2023-05-16 18:33  lshan  阅读(198)  评论(0编辑  收藏  举报