吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1121|回复: 0
收起左侧

[Python 原创] 【原创源码】【python】python3访问S3存储封装package

[复制链接]
苍穹之麒麟 发表于 2020-7-27 14:55
[Python] 纯文本查看 复制代码
# -*- coding: utf-8 -*-

# __ author:Jack
# date: 2020-04-26

# https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html

import boto3
from boto3.s3.transfer import TransferConfig
from botocore.exceptions import ClientError

def get_s3_client(url, access_key, secret_key):
    return boto3.client('s3',
        endpoint_url = url,
        aws_access_key_id = access_key,
        aws_secret_access_key = secret_key)

def get_all_bucket(client):
    resp = client.list_buckets()
    return resp["Buckets"]

# 有bug
def get_all_s3_keys(client, bucket):
    keys = []
    kwargs = {'Bucket': bucket}
    while True:
        resp = client.list_objects_v2(**kwargs)
        for obj in resp['Contents']:
            keys.append(obj['Key'])
        try:
            kwargs['ContinuationToken'] = resp['NextContinuationToken']
        except KeyError as e:
            print(e)
            break
    return keys

def get_object_size(client, bucket, key):
    try:
        response = client.get_object(Bucket = bucket, Key = key)
        return response['ContentLength']
    except ClientError as e:
        errorCode = e.response['Error']['Code']
        print(errorCode)
    
    return None

if __name__=='__main__':
    client = get_s3_client('http://x.xx.x:81','code','code')
    #get_all_bucket(client)
    #print(get_all_s3_keys(client, 'bingodrive'))
    print(get_object_size(client,'xx','xx'))

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-26 05:19

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表