Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm attempting to run what seems like a simple script, but I continue to get an error. If anyone can point out what I'm missing I would be very grateful.

Please note that I know I shouldn't put the keys directly in the script and that I know it's not the best Python written, but this is just a test so I can learn how to do all of this.

The script:

    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()

The error:

    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>
like image 308
zrdunlap Avatar asked Oct 19 '22 12:10

zrdunlap


1 Answers

There are few issues in your code.

Use bucket name, not bucket object in copy-key arguments. And you switched source and destination key order.

copy_key(new_key_name, src_bucket_name, src_key_name)

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

should work.

like image 60
helloV Avatar answered Oct 21 '22 06:10

helloV