Gsutil copy/move 批处理文件
Gsutil copy/move files in batches
有没有办法使用gsutil命令批量复制或移动文件?
例如,如果我想将 100 个文件从给定文件夹复制到另一个文件夹。
另一种方法是使用 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()
这会将 GCS 存储桶 folder1
中的前 100 个文件移动到 folder2
。
试试这个:
gsutil ls gs://bucketA | head -n 100 | shuf | gsutil cp -m -I gs://bucketB
这将从 bucketA 中获取文件列表,取出前 100 个项目,使用 shuf
将它们随机化,然后将它们通过管道传输到 gsutil
以复制到 bucketB。 -I
标志从 stdin
.
读取文件列表
稍微修改一下,它会随机移动 100 个文件而不是前 100 个文件从 bucketA 到 bucketB:
gsutil ls gs://bucketA | shuf | head -n 100 | gsutil -m mv -I gs://bucketB
有没有办法使用gsutil命令批量复制或移动文件? 例如,如果我想将 100 个文件从给定文件夹复制到另一个文件夹。
另一种方法是使用 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()
这会将 GCS 存储桶 folder1
中的前 100 个文件移动到 folder2
。
试试这个:
gsutil ls gs://bucketA | head -n 100 | shuf | gsutil cp -m -I gs://bucketB
这将从 bucketA 中获取文件列表,取出前 100 个项目,使用 shuf
将它们随机化,然后将它们通过管道传输到 gsutil
以复制到 bucketB。 -I
标志从 stdin
.
稍微修改一下,它会随机移动 100 个文件而不是前 100 个文件从 bucketA 到 bucketB:
gsutil ls gs://bucketA | shuf | head -n 100 | gsutil -m mv -I gs://bucketB