如何使用 boto 将文件上传到 S3 存储桶中的目录 [英] How to upload a file to directory in S3 bucket using boto

查看:45
本文介绍了如何使用 boto 将文件上传到 S3 存储桶中的目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 python 复制 s3 存储桶中的文件.

I want to copy a file in s3 bucket using python.

例如:我有存储桶名称 = test.在存储桶中,我有 2 个文件夹,名称为dump"&输入".现在我想使用 python 将文件从本地目录复制到 S3转储"文件夹......有人可以帮我吗?

Ex : I have bucket name = test. And in the bucket, I have 2 folders name "dump" & "input". Now I want to copy a file from local directory to S3 "dump" folder using python... Can anyone help me?

推荐答案

试试这个...

import boto
import boto.s3
import sys
from boto.s3.key import Key

AWS_ACCESS_KEY_ID = ''
AWS_SECRET_ACCESS_KEY = ''

bucket_name = AWS_ACCESS_KEY_ID.lower() + '-dump'
conn = boto.connect_s3(AWS_ACCESS_KEY_ID,
        AWS_SECRET_ACCESS_KEY)


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

testfile = "replace this with an actual filename"
print 'Uploading %s to Amazon S3 bucket %s' % 
   (testfile, bucket_name)

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


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

[更新]我不是 pythonist,所以感谢你对导入语句的提醒.此外,我不建议将凭据放在您自己的源代码中.如果您在 AWS 内部运行它,请使用 IAM 凭证和实例配置文件 (http://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_switch-role-ec2_instance-profiles.html),并且要在您的开发/测试环境中保持相同的行为,请使用 AdRoll 中的 Hologram 之类的东西(https://github.com/AdRoll/hologram)

[UPDATE] I am not a pythonist, so thanks for the heads up about the import statements. Also, I'd not recommend placing credentials inside your own source code. If you are running this inside AWS use IAM Credentials with Instance Profiles (http://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_switch-role-ec2_instance-profiles.html), and to keep the same behaviour in your Dev/Test environment, use something like Hologram from AdRoll (https://github.com/AdRoll/hologram)

这篇关于如何使用 boto 将文件上传到 S3 存储桶中的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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