Boto - 将文件上传到 Amazon S3 上的特定位置 [英] Boto - Uploading file to a specific location on Amazon S3

查看:23
本文介绍了Boto - 将文件上传到 Amazon S3 上的特定位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我正在使用的代码

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 上,我创建了目录",例如bucket/images/holiday".我知道这些只是虚拟目录.

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

我的问题是,我该如何修改这个上传到 S3 上的bucket/images/holiday 虚拟目录而不是bucket root?

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

推荐答案

您应该做的就是在上传之前将虚拟目录路径添加到键名之前.例如:

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.

这篇关于Boto - 将文件上传到 Amazon S3 上的特定位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆