how to copy s3 object from one bucket to another using python boto3

后端 未结 3 1059
日久生厌
日久生厌 2020-12-16 10:51

I want to copy a file from one s3 bucket to another. I get the following error:

s3.meta.client.copy(source,dest)
TypeError: copy() takes at leas

相关标签:
3条回答
  • 2020-12-16 11:18

    Since you are using s3 service resource, why not use its own copy method all the way?

    #!/usr/bin/env python
    import boto3
    s3 = boto3.resource('s3')
    source= { 'Bucket' : 'bucketname1', 'Key': 'objectname'}
    dest = s3.Bucket('Bucketname2')
    dest.copy(source, 'backupfile')
    
    0 讨论(0)
  • 2020-12-16 11:22

    This is the syntax from docs:

    import boto3
    
    s3 = boto3.resource('s3')
    copy_source = {
        'Bucket': 'mybucket',
        'Key': 'mykey'
    }
    s3.meta.client.copy(copy_source, 'otherbucket', 'otherkey')
    
    0 讨论(0)
  • 2020-12-16 11:24

    You can try:

    import boto3
    s3 = boto3.resource('s3')
    copy_source = {
          'Bucket': 'mybucket',
          'Key': 'mykey'
        }
    bucket = s3.Bucket('otherbucket')
    bucket.copy(copy_source, 'otherkey')
    

    or

    import boto3
    s3 = boto3.resource('s3')
    copy_source = {
        'Bucket': 'mybucket',
        'Key': 'mykey'
     }
    s3.meta.client.copy(copy_source, 'otherbucket', 'otherkey')
    

    Note the difference in the parameters

    0 讨论(0)
提交回复
热议问题