博托 - 上传文件到Amazon S3上的特定位置上传文件、位置、Amazon

2023-09-11 23:38:48 作者:往往结局都是这样的

这是在code,我从

import sys
import boto
import boto.s3

# AWS ACCESS DETAILS
AWS_ACCESS_KEY_ID = ''
AWS_SECRET_ACCESS_KEY = ''

bucket_name = AWS_ACCESS_KEY_ID.lower() + '-mah-bucket' conn = boto.connect_s3(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
bucket = conn.create_bucket(bucket_name, location=boto.s3.connection.Location.DEFAULT)
uploadfile = sys.argv[1]

print 'Uploading %s to Amazon S3 bucket %s' % \
       (uploadfile, bucket_name)

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

from boto.s3.key import Key
k = Key(bucket)
k.key = 'my test file'
k.set_contents_from_filename(testfile, cb=percent_cb, num_cb=10)

在我的S3我已经创建了目录,这样的水桶/图片/假日。我知道这些只是虚拟目录。

On my S3 I have created "directories", like this "bucket/images/holiday". I know these are only virtual directories.

我的问题是,我怎么能修改此上传专门桶/图片/假期的虚拟目录在S3上,而不是斗根?

My question is, how can I modify this upload specifically to bucket/images/holiday virtual directory on S3 rather than the bucket root?

推荐答案

所有你应该做的是prePEND虚拟目录路径之前上传的键名。例如:

All you should have to do is prepend the virtual directory path to the key name prior to uploading. For example:

key_name = 'my test file'
path = 'images/holiday'
full_key_name = os.path.join(path, key_name)
k = bucket.new_key(full_key_name)
k.set_contents_from_filename(...)

您可能需要更改该位为你的应用程序,但希望这给你的基本思路。

You may have to change that a bit for your application but hopefully that gives you the basic idea.