【问题标题】:list parts in a multipart upload using boto3 with python使用 boto3 和 python 列出分段上传中的部分
【发布时间】:2017-02-17 18:37:02
【问题描述】:

我正在为特定的大型对象/文件使用分段上传。使用此线程 (https://github.com/boto/boto3/issues/50) 可以正常工作。但是,当我尝试使用 boto 中的 list_parts 命令列出零件时(以验证某些要点),它给了我一个错误:

这里是代码:

import os
import json
import sys
import boto3
from boto3 import client
from botocore.utils import fix_s3_host

def listbucketandobjects () :
    with open("credentials.json", 'r') as f:
        data = json.loads(f.read())
        bucket_target = data["aws"]["targetBucket"]
        s3ressource = client(
            service_name='s3', 
            endpoint_url= data["aws"]["hostEndPoint"],
            aws_access_key_id= data["aws"]["idKey"],
            aws_secret_access_key=data["aws"]["secretKey"],
            use_ssl=True,
            )           
        key = 'mp-test.txt'
        # Initiate the multipart upload and send the part(s)
        mpu = s3ressource.create_multipart_upload(Bucket=bucket_target, Key=key)        
        part1 = s3ressource.upload_part(Bucket=bucket_target, Key=key, PartNumber=1,
                                UploadId=mpu['UploadId'], Body='Hello, world!')
        # 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': part1['ETag']
                }
            ]
        }
        # Now the upload works!
        s3ressource.complete_multipart_upload(Bucket=bucket_target, Key=key, UploadId=mpu['UploadId'],
                                        MultipartUpload=part_info)

        for item in mpu['UploadId']:
            print (item)        

        for item in s3ressource.list_parts(Bucket=bucket_target,Key=key, UploadId=mpu['UploadId']):
            print(item)

listpartsobjects ()

这是错误:

for item in s3ressource.list_parts(Bucket=bucket_target,Key=key, UploadId=mpu['UploadId']):
File "/usr/local/lib/python2.7/dist-packages/botocore/client.py", line 253, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/usr/local/lib/python2.7/dist-packages/botocore/client.py", line 543, in _make_api_call
raise error_class(parsed_response, operation_name)
botocore.errorfactory.NoSuchKey: An error occurred (NoSuchKey) when calling the ListParts operation: The specified key does not exist.

但是当在这里查看 aws 页面时 (http://boto3.readthedocs.io/en/latest/reference/services/s3.html?highlight=list%20object) 我想我遗漏了一些东西。但我看不出是什么..

【问题讨论】:

  • 您希望看到什么?我的怀疑是,一旦分段上传完成,“部分”就会消失,因为它们已加入最终对象。在完成上传之前尝试运行list_parts() 命令,它可能会显示您想要的内容。
  • @John Rotenstein:它奏效了。我想做一些完整性检查......

标签: python list amazon-web-services amazon-s3 boto3


【解决方案1】:

感谢 John Rotenstein,它成功了,下面是一段丑陋的(我知道,但这是在清理之前)创建多部分然后列出部分然后完成上传的代码。

def listbucketandobjects () :
    with open("credentials.json", 'r') as f:
        data = json.loads(f.read())
        bucket_target = data["aws"]["targetBucket"]
        s3ressource = client(
            service_name='s3', 
            endpoint_url= data["aws"]["hostEndPoint"],
            aws_access_key_id= data["aws"]["idKey"],
            aws_secret_access_key=data["aws"]["secretKey"],
            use_ssl=True,
            )

        key = 'mp-test.txt'
        mpu = s3ressource.create_multipart_upload(Bucket=bucket_target, Key=key)
        part1 = s3ressource.upload_part(Bucket=bucket_target, Key=key, PartNumber=1,
                                UploadId=mpu['UploadId'], Body='Hello, world!')
        # 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': part1['ETag']
                }
            ]
        }
        IDofUploadedMPU=mpu['UploadId']
        print ('**********************PRINT THE  ULPOAD ID **********************')
        print IDofUploadedMPU
        print ('**********************PRINT THE COMPLETE LIST PARTS  **********************')
        jacko=s3ressource.list_parts(Bucket=bucket_target,Key=key,UploadId=IDofUploadedMPU)
        print (jacko)
        print ('**********************PRINT THE RECURSIVE COMPLETE LIST PARTS  **********************')
        for jack in s3ressource.list_parts(Bucket=bucket_target,Key=key, UploadId=IDofUploadedMPU)["Parts"]:
            print(jack)
        print ('********************** NOW UPLOADING **********************')
        s3ressource.complete_multipart_upload(Bucket=bucket_target, Key=key, UploadId=mpu['UploadId'],
                                        MultipartUpload=part_info)

listbucketandobjects ()

【讨论】:

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