每当我试图获取云存储桶对象并使用支持站点上显示的方法进行输入时,我都会得到错误。
google.api_core.exceptions.InvalidArgument: 400 gcs_content_uri中指定的GCS对象不存在。
gcs引用在打印时如下所示:
gs://lang-docs/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'requirements.txt
google-cloud-storage
google-cloud-language
google-api-python-client
grpcio
grpcio-tools发布于 2018-10-15 19:41:01
name实例的google.cloud.storage.blob.Blob属性应该是字符串,因此根本不需要执行.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'https://stackoverflow.com/questions/52768296
复制相似问题