我见过几次类似的问题,并提出了一些建议的步骤来解决我认为已经遵循的问题。除了我在一个程序中执行所有操作外,代码完全是示例中的示例...将文件加载到GCS,然后尝试使用Vision处理文件。我在顶部设置了一个环境变量,我认为这对两个函数都适用...文件已成功上传到GCS浏览器并已确认URI。权限被拒绝错误令人困惑,因为凭据具有所有者权限...
有什么想法吗?客户端client.async_batch_annotate_files ..被调用后,该对象即消失
->操作= client.async_batch_annotate_files(requests = [async_request]) (Pdb) google.api_core.exceptions.PermissionDenied:403打开文件时出错:gs://test_documents/uploads/2c4cd57cea679abd7dde9b20023a6c2c.pdf。
from google.cloud import vision
from google.cloud import storage
from google.protobuf import json_format
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]=config.gstorage
mime_type = 'application/pdf'
batch_size = 2
doc = DocumentMaster.objects.get(document_id=14)
bname = 'test_documents'
fname = doc.document_hash_key+".pdf"
in_target = os.path.join("uploads",fname)
out_target = os.path.join("document_json",fname)
fullpath = os.path.join(bname,in_target)
fullpath2 = os.path.join(bname,out_target)
private_in = "gs://"+fullpath
private_out = "gs://test_documents/document_json"
CloudStorage = storage.Client()
StorageBucket = CloudStorage.get_bucket(bname)
blob = StorageBucket.blob(in_target)
blob.upload_from_filename(doc.document_original.path)
client = vision.ImageAnnotatorClient()
feature = vision.types.Feature(type=vision.enums.Feature.Type.DOCUMENT_TEXT_DETECTION)
gcs_source = vision.types.GcsSource(uri=private_in)
input_config = vision.types.InputConfig(gcs_source=gcs_source, mime_type=mime_type)
gcs_destination = vision.types.GcsDestination(uri=private_out)
output_config = vision.types.OutputConfig(gcs_destination=gcs_destination, batch_size=batch_size)
async_request = vision.types.AsyncAnnotateFileRequest(features=[feature], input_config=input_config,output_config=output_config)
operation = client.async_batch_annotate_files(requests=[async_request])
print('Waiting for the operation to finish.')
operation.result(timeout=30)
答案 0 :(得分:1)
结果我低估了GCS云存储中特定管理员的状况。虽然该项目具有服务帐户,但还需要明确授权它才能检索刚上传的文件...关闭