下载对象

简单下载

def get_object():
    try:
        resp = s3_client.get_object(
            Bucket='YOUR_BUCKET_NAME',
            Key='workflow.png'
        )
        body = resp["Body"]
        with open('workflow.png', 'wb') as f:
            for chunk in body:
                f.write(chunk)
    except (ClientError, IOError) as e:
        logging.error(e)

预签名简单下载

import requests
def download_object_by_presigned_url():
    bucket = 'YOUR_BUCKET_NAME'
    key = 'random_string4.txt'  # specify your key name
    try:
        url = s3_client.generate_presigned_url(
            'get_object',
            Params={'Bucket': bucket, 'Key': key},
            ExpiresIn=3600)

        # client get the url and download object by presigned url
        print(url)
        resp = requests.get(url, stream=True)
        # storage response content to file streamly
        with open(key, 'wb') as fd:
            for chunk in resp.iter_content(1024 * 64):
                fd.write(chunk)
    except ClientError as e:
        logging.error(e)