如何在python中的远程服务器上写4K大小的文件

时间:2015-03-22 23:59:04

标签: python-2.7 amazon-ec2 amazon-s3 boto fabric

我正在尝试在安装了S3的EC2实例上写入50个4KB文件。

我怎么能在python中这样做?

我不知道如何处理这个问题。

1 个答案:

答案 0 :(得分:0)

如果您通过FUSE或其他方法安装了S3对象存储桶以将S3对象空间作为伪文件系统,则可以像在Python中一样编写文件。

with open('/path/to/s3/mount', 'wb') as dafile:
    dafile.write('contents')

如果您尝试从EC2实例中将对象放入S3中​​,那么您将需要按照boto文档来了解如何执行此操作。

让你开始:

create a /etc/boto.cfg or ~/.boto file like the boto howto says

from boto.s3.connection import S3Connection

conn = S3Connection()
# if you want, you can conn = S3Connection('key_id_here, 'secret_here')
bucket = conn.get_bucket('your_bucket_to_store_files')
for file in fifty_file_names:
    bucket.new_key(file).set_contents_from_file('/local/path/to/{}'.format(file))

这假设您正在执行相当小的文件,就像您说的那样50k。可能需要拆分/分块较大的文件。