2017-02-17 76 views
0

我正在使用分段上傳來處理特定的大對象/文件。這工作正常使用此線程(https://github.com/boto/boto3/issues/50)。但是,當我試圖使用列表中的list_parts部分在博託命令(以驗證某些點),它給了我一個錯誤:使用boto3與python列表中的部分分段上傳

這裏是代碼:

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)我想我錯過了一些東西。但我沒有看到什麼..

+0

你希望看到什麼?我懷疑在分段上傳完成後,'部分'消失,因爲它們被加入到最終對象中。嘗試在完成上傳之前運行'list_parts()'命令,它可能會顯示您想要的內容。 –

+0

@Johnson Rotenstein:它工作。我想做一些誠信雙重檢查... – MouIdri

回答

1

感謝約翰羅森斯坦,它的工作,下面是醜陋的(我知道,但這是在清理之前)的一段代碼,確實創造了多部分,然後列出部分,然後完成上傳。

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