【问题标题】:Transfer files from one folder to another in amazon s3 using python boto使用 python boto 在 amazon s3 中将文件从一个文件夹传输到另一个文件夹
【发布时间】:2018-06-01 01:47:16
【问题描述】:

我正在尝试将文件从一个文件夹传输到同一个 s3 存储桶中的另一个文件夹,但出现错误。我在下面编写了代码来将文件传输到一个文件夹到另一个文件夹。

import boto
from boto.s3.connection import S3Connection
import boto.s3
import sys
from boto.s3.key import Key


conn = S3Connection('access key', 'secret key')
bucket = conn.get_bucket('bucket-name')
for file in bucket.list("2/", "/"):
    k = Key(bucket)
    print(k)
    k.key = '3'
    k.set_contents_from_filename(file)

我收到以下错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-34-4f81a952b1f1> in <module>()
     15     print(k)
     16     k.key = '3'
---> 17     k.set_contents_from_filename(file)
     18 
     19 

/Users/anaconda/lib/python3.6/site-packages/boto/s3/key.py in set_contents_from_filename(self, filename, headers, replace, cb, num_cb, policy, md5, reduced_redundancy, encrypt_key)
   1356         :return: The number of bytes written to the key.
   1357         """
-> 1358         with open(filename, 'rb') as fp:
   1359             return self.set_contents_from_file(fp, headers, replace, cb,
   1360                                                num_cb, policy, md5,

TypeError: expected str, bytes or os.PathLike object, not Key

是否有任何有效的方法可以使用 python boto/boto3 将文件从一个文件夹传输到同一个 s3 存储桶中的另一个文件夹。我正在对少量文件进行测试。实际上我有 60gb 的数据,我必须分批传输 1000 个文件。

谁能帮我解决这个问题?

谢谢

【问题讨论】:

    标签: python-3.x amazon-s3 boto boto3


    【解决方案1】:

    我建议为此使用 boto3。 您可以轻松地将 s3 对象从一个文件夹复制到另一个文件夹。

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

    您可以在循环中使用上述代码,并将它们的键替换为您的键。 复制对象后,您可以使用相同的键从源文件夹中删除对象。

    【讨论】:

    • 不要认为这行得通。当您将另一个存储桶作为bucket/folder 时,您将获得ParamValidationError
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-02
    • 2022-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多