2016-02-10 74 views
0

我發現github腳本採用B文件夾上傳到S3到S3無法上傳使用博託

import boto 
import boto.s3 

import os.path 
import sys 

# Fill these in - you get them when you sign up for S3 
AWS_ACCESS_KEY_ID = '' 
AWS_ACCESS_KEY_SECRET = '' 
# Fill in info on data to upload 
# destination bucket name 
bucket_name = 'jwu-testbucket' 
# source directory 
sourceDir = 'testdata/' 
# destination directory name (on s3) 
destDir = '' 

#max size in bytes before uploading in parts. between 1 and 5 GB recommended 
MAX_SIZE = 20 * 1000 * 1000 
#size of parts when uploading in parts 
PART_SIZE = 6 * 1000 * 1000 

conn = boto.connect_s3(AWS_ACCESS_KEY_ID, AWS_ACCESS_KEY_SECRET) 

bucket = conn.create_bucket(bucket_name, 
     location=boto.s3.connection.Location.DEFAULT) 


uploadFileNames = [] 
for (sourceDir, dirname, filename) in os.walk(sourceDir): 
    uploadFileNames.extend(filename) 
    break 

def percent_cb(complete, total): 
    sys.stdout.write('.') 
    sys.stdout.flush() 

for filename in uploadFileNames: 
    sourcepath = os.path.join(sourceDir + filename) 
    destpath = os.path.join(destDir, filename) 
    print 'Uploading %s to Amazon S3 bucket %s' % \ 
      (sourcepath, bucket_name) 

    filesize = os.path.getsize(sourcepath) 
    if filesize > MAX_SIZE: 
     print "multipart upload" 
     mp = bucket.initiate_multipart_upload(destpath) 
     fp = open(sourcepath,'rb') 
     fp_num = 0 
     while (fp.tell() < filesize): 
      fp_num += 1 
      print "uploading part %i" %fp_num 
      mp.upload_part_from_file(fp, fp_num, cb=percent_cb, num_cb=10, size=PART_SIZE) 

     mp.complete_upload() 

    else: 
     print "singlepart upload" 
     k = boto.s3.key.Key(bucket) 
     k.key = destpath 
     k.set_contents_from_filename(sourcepath, 
       cb=percent_cb, num_cb=10) 

當運行它,我得到這個錯誤:

Traceback (most recent call last): 
    File "upload_to_s3.py", line 29, in <module> 
    bucket = conn.create_bucket(bucket_name, location=boto.s3.connection.Location.DEFAULT) 
    File "/usr/local/lib/python2.7/dist-packages/boto/s3/connection.py", line 613, in create_bucket 
    data=data) 
    File "/usr/local/lib/python2.7/dist-packages/boto/s3/connection.py", line 665, in make_request 
    retry_handler=retry_handler 
    File "/usr/local/lib/python2.7/dist-packages/boto/connection.py", line 1071, in make_request 
    retry_handler=retry_handler) 
    File "/usr/local/lib/python2.7/dist-packages/boto/connection.py", line 1030, in _mexe 
    raise ex 
socket.gaierror: [Errno -2] Name or service not known 

我改變了默認爲「S3-EU-西-1」不工作,我完全地去除DEFAULT使它像這樣:

bucket = conn.create_bucket(bucket_name) 

和我也有一個錯誤。

回答

0

如果將連接命令更改爲conn = boto.s3.connect_to_region('eu-west-1', ...),它會工作嗎?

boto.s3.connection.Location.EU而不是DEFAULT也許值得一試 - 我不知道你是如何使用「s3-eu-west-1」。 (作爲一個字符串?)

+0

仍然有相同的錯誤。 – 4m1nh4j1

+0

你甚至可以解決端點?試試'ping s3-eu-west-1.amazonaws.com'。 –

+0

'來自s3-eu-west-1.amazonaws.com的64字節(54.231.131.83):icmp_seq = 1 ttl = 44時間= 24.7ms' – 4m1nh4j1