【问题标题】:how to write 4K size file on remote server in python如何用python在远程服务器上写入4K大小的文件
【发布时间】:2015-05-25 22:04:16
【问题描述】:

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

如何在 python 中做到这一点?

我不知道该怎么做。

【问题讨论】:

  • 你的意思是,你的 EC2 实例上挂载了 s3?我不明白。另外,您是否有任何要使用的代码?如果是这样,请显示它以及您收到的错误。
  • 请提供更多详细信息,说明您正在尝试完成的工作以及到目前为止所做的工作。

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


【解决方案1】:

如果您通过 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。较大的文件可能需要拆分/分块。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-02
    • 1970-01-01
    相关资源
    最近更新 更多