代码之家  ›  专栏  ›  技术社区  ›  DataGuy

将一个S3存储桶中的一个文件复制到另一个S3储存桶中的密钥

  •  -1
  • DataGuy  · 技术社区  · 1 年前

    我使用的是boto3,有两个s3存储桶:

    source s3 bucket: 'final-files'
    destination s3 bucket: 'exchange-files'
    prefix for destination s3 bucket: 'timed/0001'
    key: final_file[-1] #this is assigned earlier and is a file name
    

    我需要将一个文件从源bucket复制到目标bucket中的文件夹,我不确定如何将前缀添加到目标;这是我的代码:

    #create a source dictionary that specifies bucket name and key name of the object to be copied
    copy_source = {
    'Bucket': 'final-files',
    'Key': final_file[-1] 
    }
    bucket = s3.Bucket('exchange-files')
    prefix="timed/0001"
    bucket.copy(copy_source, prefix + key)
    
    # Printing the Information That the File Is Copied.
    print('Single File is copied')
    

    这是我得到的错误:

    "errorMessage": "expected string or bytes-like object, got 's3.Bucket'"
    

    我错过了什么?

    1 回复  |  直到 1 年前
        1
  •  0
  •   Vlam    1 年前

    请尝试以下代码,看看它是否有效。代码基于的示例 https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3/client/copy.html .

    import boto3
    
    s3 = boto3.resource('s3')
    copy_source = {
        'Bucket': 'final-files',
        'Key': final_file[-1]
    }
    prefix="timed/0001"
    s3.meta.client.copy(copy_source, 'exchange-files', prefix + final_file[-1])