AWS SDK Python

使用 AWS 官方 SDK aws-sdk-python

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import boto3
from boto3.s3.transfer import S3Transfer, TransferConfig

# some configs
s3endpoint = 'https://api-s3.qiniu.com'
s3region = 'cn-east-1'
s3bucket = '<your-qiniu-bucket>'
s3object = u's3 + 中文/object.ext'
s3accessKeyId = '<your-qiniu-access-key-id>'
s3accessKeySecret = '<your-qiniu-access-secret-key>'


# use resource
s3resource = boto3.resource('s3',
    endpoint_url=s3endpoint,
    region_name=s3region,
    aws_access_key_id=s3accessKeyId,
    aws_secret_access_key=s3accessKeySecret)

## list buckets
for bucket in s3resource.buckets.all():
    print(bucket)

## upload file
data = open('/path/to/your/file', 'rb')
s3response = s3resource.Bucket(s3bucket).put_object(Key=s3object, Body=data)
print(s3response)

## download file
s3response = s3resource.Bucket(s3bucket).download_file(Key=s3object, Filename="/path/to/local/file")
print(s3response)


# use client
s3client = boto3.client('s3',
    endpoint_url=s3endpoint,
    region_name=s3region,
    aws_access_key_id=s3accessKeyId,
    aws_secret_access_key=s3accessKeySecret)

s3config = TransferConfig(
    multipart_threshold=8<<20, # 8M
    multipart_chunksize=4<<20 # 4M
)

## list buckets
s3response = s3client.list_buckets()
print(s3response)

## upload file
transfer = S3Transfer(s3client, s3config)
s3response = transfer.upload_file('/path/to/your/file', s3bucket, s3object)
print(s3response)

## download file
transfer = S3Transfer(s3client, s3config)
s3response = transfer.download_file(s3bucket, s3object, 'local.file')
print(s3response)

results matching ""

    No results matching ""