是否有任何 API 函数允许我们将 Google Cloud Storage 中的文件从一个存储桶移动到另一个存储桶?
场景是我们希望 Python 将读取的 A 桶中的文件移动到 B 桶中。我知道 gsutil 可以做到这一点,但不确定 Python 是否支持。
谢谢。
原文由 user3769827 发布,翻译遵循 CC BY-SA 4.0 许可协议
是否有任何 API 函数允许我们将 Google Cloud Storage 中的文件从一个存储桶移动到另一个存储桶?
场景是我们希望 Python 将读取的 A 桶中的文件移动到 B 桶中。我知道 gsutil 可以做到这一点,但不确定 Python 是否支持。
谢谢。
原文由 user3769827 发布,翻译遵循 CC BY-SA 4.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}')
原文由 dmlee8 发布,翻译遵循 CC BY-SA 4.0 许可协议
2 回答5.2k 阅读✓ 已解决
2 回答1.1k 阅读✓ 已解决
4 回答1.5k 阅读✓ 已解决
3 回答1.3k 阅读✓ 已解决
3 回答1.3k 阅读✓ 已解决
2 回答899 阅读✓ 已解决
1 回答1.8k 阅读✓ 已解决
使用 google-api-python-client , storage.objects.copy 页面上有一个示例。复制后,您可以使用 storage.objects.delete 删除源。