使用带有python

时间:2017-02-17 08:47:51

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

我正在为特定的大型对象/文件使用分段上传。使用此线程(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)我觉得我错过了什么。但是我没看到......

1 个答案:

答案 0 :(得分:1)

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

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 ()