是否有任何API功能允许我们从另一个存储桶中的一个存储桶移动Google云端存储中的文件?
场景是我们希望Python将A桶中的读取文件移动到B桶。我知道gsutil可以做到这一点,但不确定Python是否可以支持。
感谢。
答案 0 :(得分:4)
使用google-api-python-client,storage.objects.copy页面上有一个示例。复制后,您可以使用storage.objects.delete删除来源。
destination_object_resource = {}
req = client.objects().copy(
sourceBucket=bucket1,
sourceObject=old_object,
destinationBucket=bucket2,
destinationObject=new_object,
body=destination_object_resource)
resp = req.execute()
print json.dumps(resp, indent=2)
client.objects().delete(
bucket=bucket1,
object=old_object).execute()
答案 1 :(得分:1)
您可以使用[1]中记录的GCS客户端库函数读取一个存储桶并写入另一个存储桶,然后删除源文件。
您甚至可以使用[2]中记录的GCS REST API。
链接:
[1] - https://developers.google.com/appengine/docs/python/googlecloudstorageclient/functions
[2] - https://developers.google.com/storage/docs/concepts-techniques#overview
答案 2 :(得分:0)
这是在同一存储桶中或其他存储桶中的目录之间移动Blob时使用的功能。
from google.cloud import storage
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="path_to_your_creds.json"
def mv_blob(bucket_name, blob_name, new_bucket_name, new_blob_name):
"""
Function for moving files between directories or buckets. it will use GCP's copy
function then delete the blob from the old location.
inputs
-----
bucket_name: name of bucket
blob_name: str, name of file
ex. 'data/some_location/file_name'
new_bucket_name: name of bucket (can be same as original if we're just moving around directories)
new_blob_name: str, name of file in new directory in target bucket
ex. 'data/destination/file_name'
"""
storage_client = storage.Client()
source_bucket = storage_client.get_bucket(bucket_name)
source_blob = source_bucket.blob(blob_name)
destination_bucket = storage_client.get_bucket(new_bucket_name)
# copy to new destination
new_blob = source_bucket.copy_blob(
source_blob, destination_bucket, new_blob_name)
# delete in old destination
source_blob.delete()
print(f'File moved from {source_blob} to {new_blob_name}')