Python 使用 boto 在同一个 S3 Bucket 中移动/复制文件

标签 python amazon-web-services amazon-s3 boto

我正在尝试运行一个看似简单的脚本,但我仍然遇到错误。如果有人能指出我遗漏了什么,我将不胜感激。

请注意,我知道我不应该将 key 直接放在脚本中,而且我知道它不是编写的最好的 Python,但这只是一个测试,因此我可以学习如何完成所有这些操作。

脚本:

    import boto

    def s3test():
        s3 = boto.connect_s3('MY_ACCESS_KEY', 'MY_SECRET_KEY')
        bucket = s3.get_bucket('the-bucket-name')
        bucket.copy_key('location1/item',bucket,'location2/item')

    if __name__ == "__main__":
        s3test()

错误:

    Traceback (most recent call last):
      File "script/path", line 9, in <module>
        s3test()
      File "script/path", line 6, in s3test
        bucket.copy_key('location1/item',bucket,'location2/item')
      File "C:\Python27\lib\site-packages\boto\s3\bucket.py", line 889, in copy_key                                          
        response.reason, body)
    S3ResponseError: S3ResponseError: 404 Not Found
        <Error>
            <Code>NoSuchBucket</Code>
            <Message>The specified bucket does not exist</Message>
            <BucketName>&lt;Bucket: the-test-bucket&gt;</BucketName>
            <RequestId>ABCDEFG12345</RequestId>
            <HostId>HTLIxTQI87qC56FG2c0y570E+Y2L56e7806OJhAXk2x5i7uzfd4XU/nhmjHVpLqz9</HostId>
        </Error>

最佳答案

您的代码中几乎没有问题。

copy-key 参数中使用bucket name,而不是bucket object。并且您切换了源键和目标键顺序。

copy_key(new_key_name, src_bucket_name, src_key_name)

bucket.copy_key('location2/item','the-bucket-name','location1/item')

应该可以。

关于Python 使用 boto 在同一个 S3 Bucket 中移动/复制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36338549/

相关文章:

python - 通过 multipart/form-data POST 请求发送内存中的字节(文件)。 Python

python - python while 循环 : even when sleeping 97% of the time. 的 CPU 使用率高为什么?

linux - 我是否需要 vagrant 在 Ubuntu 12.04 上安装 edx 平台才能通过 AWS 步骤

postgresql - AWS RDS 与 EC2 实例中的本地数据库

amazon-web-services - 如何在不使用生命周期规则的情况下将 S3 Standard 转换为 Glacier

python - 在Qt4上安装MayaVi会导致段错误

python - scikit SGDClassifierpartial_fit 不会增量学习。返回 “classes should include all valid labels"

ruby-on-rails - 跨前端服务器共享缓存文件的可扩展方式

amazon-web-services - 由于 S3Bucket 资源已存在,Cloudformation 模板失败

php - 基于S3的文件存储应用程序的数据库设计