【问题标题】:Gsutil copy/move files in batchesGsutil 批量复制/移动文件
【发布时间】:2020-02-26 20:07:35
【问题描述】:

有没有办法使用 gsutil 命令批量复制或移动文件? 例如,如果我想将 100 个文件从给定文件夹复制到另一个。

【问题讨论】:

    标签: google-cloud-storage gsutil


    【解决方案1】:

    试试这个:

    gsutil ls gs://bucketA | head -n 100 | shuf | gsutil cp -m -I gs://bucketB
    

    这将从存储桶A 中获取文件列表,获取前100 个项目,使用shuf 随机化它们,并将它们通过管道传输到gsutil 以复制到存储桶B。 -I 标志从stdin 读取文件列表。

    【讨论】:

    • 我认为这里的-m标志需要在cp命令之前。
    【解决方案2】:

    另一种方法是使用Client libraries。例如在 Python 中:

    from google.cloud import storage
    
    storage_client = storage.Client()
    
    bucket_name = 'my_bucket'
    bucket = storage_client.get_bucket(bucket_name)
    
    blobs_to_move = [blob for blob in bucket.list_blobs(prefix="folder1/")]
    
    with storage_client.batch():
        for blob in blobs_to_move[:100]:
            # copy to new destination
            new_blob = bucket.copy_blob(blob, bucket, "folder2/" + blob.name[8:])
            # delete in old destination
            blob.delete()   
    

    这会将前 100 个文件从 GCS 存储桶 my_bucket 中的 folder1 移动到 folder2

    【讨论】:

    • 不完全是。我正在寻找的是一种仅复制整个内容的子集的方法。例如,1000 个文件中只有 100 个。
    • 是的,随机查找命令在这里喜欢的东西 - find 。 -maxdepth 1 -type f |head -500|xargs
    • 您可以自己生成文件列表,并使用-I 标志将它们通过管道传输到gsutil。
    • @user101010 您也可以使用 Python 客户端库来实现。我已经更新了我的答案。
    【解决方案3】:

    稍作修改,它会随机将 100 个文件而不是前 100 个文件从存储桶 A 移动到存储桶 B:

    gsutil ls gs://bucketA | shuf | head -n 100 | gsutil -m mv  -I gs://bucketB
    

    【讨论】:

      猜你喜欢
      • 2016-03-30
      • 1970-01-01
      • 2014-07-15
      • 2015-07-08
      • 2013-02-03
      • 2021-11-25
      • 2015-02-07
      • 2011-10-17
      • 2017-12-16
      相关资源
      最近更新 更多