Python moving/copying 个文件与 boto 在同一个 S3 存储桶中

Python moving/copying files within the same S3 Bucket with boto

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

请注意,我知道我不应该将密钥直接放在脚本中,而且我知道它不是最好的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')

应该可以。