gpt4 book ai didi

python - 无法从云函数正确访问GCS对象

转载 作者:太空宇宙 更新时间:2023-11-03 21:42:15 29 4
gpt4 key购买 nike

每当我尝试使用支持网站上显示的方法获取云存储桶对象和输入时,都会收到错误

google.api_core.exceptions.InvalidArgument: 400 The GCS object specified in gcs_content_uri does not exist.

打印时 gcs 引用如下所示:

gs://lang-docs-in/b'doc1.txt'

我已经尝试了一切方法来让它工作:编码、解码等,似乎花了几个小时,但无济于事。有什么想法吗?

main.py

import sys
from google.cloud import language
from google.cloud import storage

storage_client = storage.Client()

DOCUMENT_BUCKET = 'lang-docs-out'

def process_document(data, context):
# Get file attrs
bucket = storage_client.get_bucket(data['bucket'])
blob = bucket.get_blob(data['name'])
# send to NLP API
gcs_obj = 'gs://{}/{}'.format(bucket.name, blob.name.decode('utf-8'))
print('LOOK HERE')
print(gcs_obj)
parsed_doc = analyze_document(bucket, blob)
# Upload the resampled image to the other bucket
bucket = storage_client.get_bucket(DOCUMENT_BUCKET)
newblob = bucket.blob('parsed-' + data['name'])
newblob.upload_from_string(parsed_doc)

def analyze_document(bucket, blob):
language_client = language.LanguageServiceClient()
gcs_obj = 'gs://{}/{}'.format(bucket.name, blob.name.decode('utf-8'))
print(gcs_obj)
document = language.types.Document(gcs_content_uri=gcs_obj, language='en', type='PLAIN_TEXT')
response = language_client.analyze_syntax(document=document, encoding_type= get_native_encoding_type())
return response

def get_native_encoding_type():
"""Returns the encoding type that matches Python's native strings."""
if sys.maxunicode == 65535:
return 'UTF16'
else:
return 'UTF32'

需求.txt

google-cloud-storage
google-cloud-language
google-api-python-client
grpcio
grpcio-tools

最佳答案

google.cloud.storage.blob.Blobname 属性实例应该是一个字符串,因此您根本不需要执行 .decode()

您似乎确实有一个名为“b'doc1.txt'”的文件,该文件是由于将文件添加到GCS的任何问题而创建的,而不是由于您的云功能而创建的,例如:

>>> blob.name
"b'doc1.txt'"
>>> type(blob.name)
<class 'str'>

而不是:

>>> blob.name
b'doc1.txt'
>>> type(blob.name)
<class 'bytes'>

这真的很难区分,因为它们在打印时看起来是一样的:

>>> print(b'hi')
b'hi'
>>> print("b'hi'")
b'hi'

关于python - 无法从云函数正确访问GCS对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52768296/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com