Python上传下载S3文件

使用 Python 操作 S3 存储可以通过 boto3 库实现。

安装 boto3

在开始之前,确保安装了 boto3 库:

pip install boto3

连接到 S3

使用 AWS 的 Access Key 和 Secret Key 连接到 S3:

    s3 = boto3.client('s3', 
                      endpoint_url=host, 
                      aws_access_key_id=access_key, 
                      aws_secret_access_key=secret_key)

将 access_key 和 secret_key 替换为实际的 AWS 凭证,host代表S3服务的地址

上传文件到 S3

使用 upload_file 方法上传文件:

s3.upload_file('local_file_path', 'bucket_name', 's3_file_path')

  • local_file_path: 本地文件路径。

  • bucket_name: S3 存储桶名称。

  • s3_file_path: 文件在 S3 中的路径。

示例:

import boto3

def upload_file_to_s3(host, access_key, secret_key, bucket_name, file_name, dst_file):
    """
    从S3存储桶下载文件到本地指定路径
    参数:
        host (str): S3服务端点URL
        access_key (str): AWS访问密钥ID
        secret_key (str): AWS秘密访问密钥
        bucket_name (str): 上传的存储桶名称
        file_name (str): 要上传的文件名
        dst_file (str): 存储桶中的路径
    """
    download_path = '/root/jh/'
    s3 = boto3.client('s3', 
                      endpoint_url=host, 
                      aws_access_key_id=access_key, 
                      aws_secret_access_key=secret_key)
    try:
        s3.download_file(bucket_name, file_name, local_name)
        s3.upload_file(file_name, bucket_name, dst_file)
        print(f'文件 {file_name} 上传成功')

    except NoCredentialsError:
        print('凭证不可用。')
    except PartialCredentialsError:
        print('提供的凭证不完整。')
    except Exception as e:
        print(f'发生错误: {e}')

if __name__ == "__main__":
    upload_file_to_s3(
        'url', 
        'access_key',
        'secret_key"',
        'bucket_name',
        '/path/file_name',
        '/s3_path/file_name'
    )

从S3下载文件

使用 download_file 方法上传文件:

s3.download_file('bucket_name', 's3_file_path', 'local_file_path')

  • bucket_name: S3 存储桶名称。

  • s3_file_path: 文件在 S3 中的路径名称。

  • local_file_path: 下载保存路径名称。

示例:

import boto3

def download_file_from_s3(host, access_key, secret_key, bucket_name, file_name, dst_path):
    """
    从S3存储桶下载文件到本地指定路径
    参数:
        host (str): S3服务端点URL
        access_key (str): AWS访问密钥ID
        secret_key (str): AWS秘密访问密钥
        bucket_name (str): 要下载的存储桶名称
        file_name (str): 要下载的特定文件名
        dst_path (str): 文件要保存的本地路径
    """
    s3 = boto3.client('s3', 
                      endpoint_url=host, 
                      aws_access_key_id=access_key, 
                      aws_secret_access_key=secret_key)
    try:
        local_name= dst_path + file_name
        s3.download_file(bucket_name, file_name, local_name)
        print(f'文件 {file_name} 下载成功')

    except NoCredentialsError:
        print('凭证不可用。')
    except PartialCredentialsError:
        print('提供的凭证不完整。')
    except Exception as e:
        print(f'发生错误: {e}')

if __name__ == "__main__":
    download_file_from_s3(
        'url', 
        'access_key',
        'secret_key"',
        'bucket_name',
        '/s3_path/file_name',
        '/local_path/'
    )

查看桶和桶中的文件

         #获取桶列表
         responses = s3.list_buckets()
         buckets = [bucket['Name'] for bucket in responses['Buckets']]
         print('All of Buckets:', buckets)
         #获取某个桶的所有文件
         response = s3.list_objects_v2(Bucket='bucket_name')
         if 'Contents' in response:
             for obj in response['Contents']:
                 key = obj['Key']
                 timestep = obj['LastModified'] 
                 print(key, timestep)       

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值