[使用Python 3使用Google Cloud Function重命名对象
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[使用Python 3使用Google Cloud Function重命名对象相关的知识,希望对你有一定的参考价值。
我正在从我的云函数运行以下代码:
from google.cloud import storage
def rename_blob(bucket_name, blob_name, new_name):
"""Renames a blob."""
bucket_name = "gk-12345"
blob_name = "gk.txt"
new_name = "gk-2343.txt"
storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(blob_name)
print(blob)
print(new_name)
new_blob = bucket.rename_blob(blob, new_name)
print("Blob has been renamed to ".format(blob.name, new_blob.name))
我遇到以下错误:
错误:功能终止。推荐的操作:检查日志以了解终止原因。详细信息:rename_blob()缺少2个必需项位置参数:“ blob_name”和“ new_name”
代码段来自https://cloud.google.com/storage/docs/renaming-copying-moving-objects#code-samples_2
有人可以帮忙。
谢谢!
编辑
我更改了代码:
from google.cloud import storage
import datetime
def rename_blob(bucket_name, blob_name=None, destination_bucket_name=None,destination_blob_name=None):
"""Copies a blob from one bucket to another with a new name."""
#time = datetime.datetime.now().strftime("%H%M%S")
#bucket_name = "gk-12345"
#blob_name = "gk.txt"
#destination_bucket_name = "gk-12345"
#destination_blob_name = blob_name+time
storage_client = storage.Client()
source_bucket = storage_client.bucket(bucket_name)
source_blob = source_bucket.blob(blob_name)
destination_bucket = storage_client.bucket(destination_bucket_name)
blob_copy = source_bucket.copy_blob(
source_blob, destination_bucket, destination_blob_name
)
print(
"Blob in bucket copied to blob in bucket .".format(
source_blob.name,
source_bucket.name,
blob_copy.name,
destination_bucket.name,
)
)
time = datetime.datetime.now().strftime("%H%M%S")
bucket_name = "gk-12345"
bName = "gk.txt"
dbName = "gk-12345"
dbNameTime = bName+time
rename_blob(bucket_name, blob_name=bName, destination_bucket_name=dbName,destination_blob_name=dbNameTime)
我现在遇到以下错误:
Traceback (most recent call last): File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 346, in run_http_function result = _function_handler.invoke_user_function(flask.request) File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 217, in invoke_user_function return call_user_function(request_or_event) File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 210, in call_user_function return self._user_function(request_or_event) File "/user_code/main.py", line 16, in rename_blob source_bucket = storage_client.bucket(bucket_name) File "/env/local/lib/python3.7/site-packages/google/cloud/storage/client.py", line 270, in bucket return Bucket(client=self, name=bucket_name, user_project=user_project) File "/env/local/lib/python3.7/site-packages/google/cloud/storage/bucket.py", line 512, in __init__ name = _validate_name(name) File "/env/local/lib/python3.7/site-packages/google/cloud/storage/_helpers.py", line 51, in _validate_name if not all([name[0].isalnum(), name[-1].isalnum()]): File "/env/local/lib/python3.7/site-packages/werkzeug/local.py", line 377, in <lambda> __getitem__ = lambda x, i: x._get_current_object()[i] TypeError: 'Request' object is not subscriptable
答案
问题出在函数的参数中。也许您在不传递所有3个参数的情况下调用函数。
另一答案
注意您的错误:
以上是关于[使用Python 3使用Google Cloud Function重命名对象的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Google Python Client for Cloud Functions 获取 Google Cloud Functions 列表?
使用 google-cloud-python API 访问 Dataproc 时出现无效区域错误
Google App Engine - 大查询 - Python 找不到库 google.cloud