2011-03-25 45 views
0

以下代碼在運行script.py -f文件名時沒有任何錯誤但不起作用?我想象它的一部分是我的函數/變量命名方案,這可能會導致我的一些混淆。有任何想法嗎?Python選項和參數傳遞不正確

#! /usr/bin/env python 

import boto 
import os, sys, glob 
from optparse import OptionParser 
from boto.s3.key import Key 

BUCKET_NAME = '' 
AWS_ACCESS_KEY_ID = '' 
AWS_SECRET_ACCESS_KEY = '' 

# function to determine file argument 
def fname(arguments): 
    files = [] 
    for arg in arguments: 
     if '*' in arg or '?' in arg: 
      # contains a wildcard character 
      files.extend(glob.glob(arg)) 
     elif os.path.isdir(arg): 
      # is a dictionary 
      files.extend(glob.glob(os.path.join(arg, '*'))) 
     elif os.path.exists(arg): 
      # is a file 
      files.append(arg) 
     else: 
      # invalid? 
      print '%s invalid' % arg 
    return files 

# function to display progress tick marks 
def percent_cb(complete, total): 
    sys.stdout.write('.') 
    sys.stdout.flush() 

# upload files to bucket 
def upload(all_files, args, bucket): 
    all_files = fname(args) 
    complete = '' 
    total = '' 
    percent_cb(complete, total) 
    for filename in all_files: 
     #skip all directory entries which are not a file 
     if not os.path.isfile(filename): 
       continue 
     k = Key(bucket) 
     k.set_contents_from_filename(filename, cb=percent_cb, num_cb=20) 

# check if file exists locally, if not: download it 
def downnload(filename, keyString): 
    if not os.path.exists(filename+keyString): 
     l.get_contents_to_filename(filename+keyString) 

# List bucket contents 
def blist(bucket): 
    for b in rs: 
     print b.name 

def main(): 
    usage = "usage: %prog [options] -f arg" 
    parser = OptionParser(usage) 
    parser.add_option('-d', '--download', 
      action='store', dest='download', 
      default=None, help='download files') 
    parser.add_option('-f', '--file', 
      action='store', dest='filename', 
      default=None, help='upload file or wildcard') 
    parser.add_option('-l', '--list', 
      action='store', dest='blist', 
      default=None, help='list buckets or contents of specified bucket') 
    parser.add_option('-v', '--version', 
      action="store_true", dest="show_version", 
      default=False, help='displays the version number') 

    if len(sys.argv) == 1: 
     parser.print_help() 
     sys.exit() 
    (options, args) = parser.parse_args() 

    if options.show_version: 
     prog = os.path.basename(sys.argv[0]) 
     version_str = "1.0" 
     print "version is: %s %s" % (prog, version_str) 
     sys.exit(0) 

# connect to the bucket 
    conn = boto.connect_s3(AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY) 
    bucket = conn.get_bucket(BUCKET_NAME) 
    rs = conn.get_all_buckets() 

# bucket file list 
    bucket_list = bucket.list() 
    for l in bucket_list: 
     keyString = str(l.key) 

    all_files = '' 
    if options.filename is not None: 
    upload(all_files, args, bucket) 
    elif options.download is not None: 
     downnload(options.filename, keyString) 
    elif options.blist is not None: 
     blist(options.bucket) 
    else: 
     parser.print_help() 
     sys.exit(-1) 

if __name__ == '__main__': 
    main() 
+0

這是怎麼回事?爲什麼它不是你想要的? – unholysampler 2011-03-25 02:56:49

+0

upload()函數未正確上傳。如果我註釋掉最後一塊代碼,並且只留下**上傳(all_files,args,bucket)**,則該功能將正常工作。當然,我沒有指定功能的能力。 – Astron 2011-03-25 03:07:24

+0

哦,你的意思是boto的東西沒問題,但是解析參數有問題嗎? – 2011-03-25 03:10:14

回答

1

我不確定你在這裏訪問博託的方式。你只使用S3,對吧? boto S3 docs使用的界面與您正在使用的界面不同。下面是一些代碼摘錄了Python 2.5下工作:

from boto.s3.connection import S3Connection 
from boto.exception import S3ResponseError 

class TimeoutException(Exception): 
    pass 

... 

conn = S3Connection(access_key, secret_key) 
try: 
    bucket = get_bucket(conn, bucket_name) 
except TimeoutException: 
    sys.exit("Connection timed out; this usually means you're offline.") 
except S3ResponseError, exception_data: 
    sys.exit(exception_data.error_message) 

... 

key_name = os.path.basename(fname) 
if bucket.get_key(key_name): 
    print 'Already on S3, will not overwrite: ' + key_name 
    return 
key = bucket.new_key(key_name) 
key.set_contents_from_filename(fname) 

... 

def get_bucket(conn, bucket_name): 
    # If you try to get a bucket while offline, the function just 
    # hangs. This times it out after two seconds. 

    def timeout_handler(signum, frame): 
     raise TimeoutException() 

    old_handler = signal.signal(signal.SIGALRM, timeout_handler) 

    # start timer 
    signal.alarm(2) 
    try: 
     bucket = conn.get_bucket(bucket_name) 
    except TimeoutException: 
     bucket = None 
    finally: 
     signal.signal(signal.SIGALRM, old_handler) 

    signal.alarm(0) 
    if bucket: 
     return bucket 
    else: 
     raise TimeoutException() 
1

-f filename創建一個分配給options.filename,但你永遠不把它傳遞給upload功能:

if options.filename is not None: 
    upload(all_files, args, bucket) 
+0

聽起來很合理,但如果我省略if/elif語句並僅使用上載(all_files,args,bucket) 聲明,它爲什麼會起作用?我還認爲** all_files **將包含更正選項。文件名將包含正確的文件名。 – Astron 2011-03-25 14:02:38