【发布时间】:2018-10-11 20:18:54
【问题描述】:
每当我尝试使用支持网站上显示的方法获取云存储桶对象和输入时,我都会收到错误
google.api_core.exceptions.InvalidArgument: 400 GCS 对象 gcs_content_uri 中指定的不存在。
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'
requirements.txt
google-cloud-storage
google-cloud-language
google-api-python-client
grpcio
grpcio-tools
【问题讨论】:
-
我希望您缺少 .json 配置文件,该文件用于 storage_client = storage.Client() 连接到存储桶。下面的 json 文件结构 { "type": "service_account", "project_id": "...", "private_key_id": "...", "private_key": "...", "client_email": ". .”、“client_id”:“...”、“auth_uri”:“...”、“token_uri”:“...”、“auth_provider_x509_cert_url”:“...”、“client_x509_cert_url”:“.. ." } 这个文件应该附加到你运行上面代码的机器上。
标签: python google-cloud-platform google-cloud-functions google-cloud-storage google-cloud-language