【问题标题】:Multipart upload using boto3使用 boto3 进行分段上传
【发布时间】:2018-09-01 19:42:50
【问题描述】:

我在下面的文档中使用 boto3 进行分段上传,但无法执行相同的操作。 你能告诉我相同的概念和语法吗?

http://boto3.readthedocs.io/en/latest/reference/services/s3.html#S3.Client.create_multipart_upload

http://boto3.readthedocs.io/en/latest/reference/services/s3.html#S3.Client.complete_multipart_upload

【问题讨论】:

  • SO 不是一个教程网站。将您的问题限制在特定问题上,粘贴您的代码试验并在您被阻止的地方分享错误日志。阅读此page 以获得更好的理解。

标签: python amazon-web-services boto3


【解决方案1】:

来自this 文档:

使用传输管理器

boto3 提供用于管理各种类型传输的接口 S3。功能包括:

自动管理分段和非分段上传

为了确保分段上传仅在绝对情况下发生 必要时,您可以使用 multipart_threshold 配置 参数:

使用以下 python 代码将文件上传到 s3 并管理自动分段上传。

import argparse
import boto3
import botocore
import os
import pandas as pd
from boto3.s3.transfer import TransferConfig

def environment_set(access_key,secret_access_key):
    os.environ["AWS_ACCESS_KEY_ID"] = access_key
    os.environ["AWS_SECRET_ACCESS_KEY"] = secret_access_key

def s3_upload_file(args):     
    while True:
    try:
        s3 = boto3.resource('s3')
        
        GB = 1024 ** 3
        
            # Ensure that multipart uploads only happen if the size of a transfer
            # is larger than S3's size limit for nonmultipart uploads, which is 5 GB.
            config = TransferConfig(multipart_threshold=5 * GB)

        s3.meta.client.upload_file(args.path, args.bucket, os.path.basename(args.path),Config=config)
        print "S3 Uploading successful"
        break
    except botocore.exceptions.EndpointConnectionError:
        print "Network Error: Please Check your Internet Connection"
    except Exception, e:
        print e


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='UPLOAD A FILE TO PRE-EXISTING S3 BUCKET')
    parser.add_argument('path', metavar='PATH', type=str,
            help='Enter the Path to file to be uploaded to s3')
    parser.add_argument('bucket', metavar='BUCKET_NAME', type=str,
            help='Enter the name of the bucket to which file has to be uploaded')
    parser.add_argument('cred', metavar='CREDENTIALS', type=str,
            help='Enter the Path to credentials.csv, having AWS access key and secret access key')    
    args = parser.parse_args()
    df = pd.read_csv(args.cred, header=None)
    access_key = df.iloc[1,1]
    secret_access_key = df.iloc[1,2]
    environment_set(access_key,secret_access_key)
    s3_upload_file(args)

【讨论】:

    猜你喜欢
    • 2015-09-05
    • 1970-01-01
    • 1970-01-01
    • 2017-09-05
    • 2019-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多