使用boto3完成multipart_upload?

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进行分段上传?

dea*_*ode 8

为此,我建议您使用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)

  • 这很有趣,但并非在所有情况下......假设,假设,您正在上传 487GB 并想要停止(或者它在 95 分钟后崩溃等)并且您想从停止点恢复,无论是 295GB ,387GB,随便什么。怎样才能从停下来的地方开始呢?如何从仍然需要上传的部分中识别出已经上传的部分(已经上传到S3)?您如何从该特定部分继续?低级分段上传:https://gist.github.com/teasherm/bb73f21ed2f3b46bc1c2ca48ec2c1cf5 (3认同)

Mar*_*ery 6

您的代码已经正确。实际上,分段上传的一个最小示例如下所示:

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 对象(json 字符串)而不将其保存到磁盘? (3认同)

Yur*_*uri 5

官方 boto3 文档所述

适用于 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


mdu*_*ant 0

在您的代码片段中,显然应该是part->part1在字典中。通常,您会有多个部分(否则为什么要使用分段上传),并且列表'Parts'将包含每个部分的一个元素。

您可能还对处理 S3 的新 Pythonic 接口感兴趣:http://s3fs.readthedocs.org/en/latest/