ble*_*man 7 python amazon-s3 boto3
试过这个:
import boto3
from boto3.s3.transfer import TransferConfig, S3Transfer
path = "/temp/"
fileName = "bigFile.gz" # this happens to be a 5.9 Gig file
client = boto3.client('s3', region)
config = TransferConfig(
multipart_threshold=4*1024, # number of bytes
max_concurrency=10,
num_download_attempts=10,
)
transfer = S3Transfer(client, config)
transfer.upload_file(path+fileName, 'bucket', 'key')
Run Code Online (Sandbox Code Playgroud)
结果:s3上的5.9 gig文件.似乎不包含多个部分.
我找到了这个例子,但part没有定义.
import boto3
bucket = 'bucket'
path = "/temp/"
fileName = "bigFile.gz"
key = 'key'
s3 = boto3.client('s3')
# Initiate the multipart upload and send the part(s)
mpu = s3.create_multipart_upload(Bucket=bucket, Key=key)
with open(path+fileName,'rb') as data:
part1 = s3.upload_part(Bucket=bucket
, Key=key
, PartNumber=1
, UploadId=mpu['UploadId']
, Body=data)
# Next, we need to gather information about each part to complete
# the upload. Needed are the part number and ETag.
part_info = {
'Parts': [
{
'PartNumber': 1,
'ETag': part['ETag']
}
]
}
# Now the upload works!
s3.complete_multipart_upload(Bucket=bucket
, Key=key
, UploadId=mpu['UploadId']
, MultipartUpload=part_info)
Run Code Online (Sandbox Code Playgroud)
问题:有没有人知道如何使用boto3进行分段上传?
为此,我建议您使用boto3.s3.transfer。下面是一个例子:
import boto3
def upload_file(filename):
session = boto3.Session()
s3_client = session.client("s3")
try:
print("Uploading file: {}".format(filename))
tc = boto3.s3.transfer.TransferConfig()
t = boto3.s3.transfer.S3Transfer(client=s3_client, config=tc)
t.upload_file(filename, "my-bucket-name", "name-in-s3.dat")
except Exception as e:
print("Error uploading: {}".format(e))
Run Code Online (Sandbox Code Playgroud)
您的代码已经正确。实际上,分段上传的一个最小示例如下所示:
import boto3
s3 = boto3.client('s3')
s3.upload_file('my_big_local_file.txt', 'some_bucket', 'some_key')
Run Code Online (Sandbox Code Playgroud)
您不需要明确要求分段上传,也不需要使用 boto3 中与分段上传相关的任何低级函数。只需调用upload_file,如果您的文件大小超过某个阈值(默认为 8MB),boto3 将自动使用分段上传。
您似乎对 S3 中的最终结果并不明显由多个部分组成这一事实感到困惑:
结果:s3 上的 5.9 演出文件。似乎没有包含多个部分。
......但这是预期的结果。分段上传 API 的全部意义在于让您通过多个 HTTP 请求上传单个文件,并在 S3 中以单个对象结束。
适用于 Python 的 AWS 开发工具包自动管理重试以及多部分和非多部分传输。
管理操作是通过使用非常适合大多数场景的合理默认设置来执行的。
因此,您需要做的只是设置所需的分段阈值,该阈值将指示 Python SDK 将自动处理分段上传的最小文件大小:
import boto3
from boto3.s3.transfer import TransferConfig
# Set the desired multipart threshold value (5GB)
GB = 1024 ** 3
config = TransferConfig(multipart_threshold=5*GB)
# Perform the transfer
s3 = boto3.client('s3')
s3.upload_file('FILE_NAME', 'BUCKET_NAME', 'OBJECT_NAME', Config=config)
Run Code Online (Sandbox Code Playgroud)
此外,您还可以通过设置使用多线程机制进行分段上传max_concurrency:
# To consume less downstream bandwidth, decrease the maximum concurrency
config = TransferConfig(max_concurrency=5)
# Download an S3 object
s3 = boto3.client('s3')
s3.download_file('BUCKET_NAME', 'OBJECT_NAME', 'FILE_NAME', Config=config)
Run Code Online (Sandbox Code Playgroud)
最后,如果您想在单线程中执行分段上传,只需设置use_threads=False:
# Disable thread use/transfer concurrency
config = TransferConfig(use_threads=False)
s3 = boto3.client('s3')
s3.download_file('BUCKET_NAME', 'OBJECT_NAME', 'FILE_NAME', Config=config)
Run Code Online (Sandbox Code Playgroud)
完整的源代码和解释:Python S3 Multipart File Upload with Metadata and Progress Indicator
在您的代码片段中,显然应该是part->part1在字典中。通常,您会有多个部分(否则为什么要使用分段上传),并且列表'Parts'将包含每个部分的一个元素。
您可能还对处理 S3 的新 Pythonic 接口感兴趣:http://s3fs.readthedocs.org/en/latest/
| 归档时间: |
|
| 查看次数: |
15461 次 |
| 最近记录: |