根据https://googleapis.github.io/google-cloud-python/latest/speech/index.html,我正在尝试使用Google语音转文本服务 我创建了项目,将音频上传到gs:云,添加了权限,下载了名为My First Project-7bb85a480131.json的json文件。 https://console.cloud.google.com/storage/browser/mybucket?project=my-project
这是我的文件:
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="/home/joo/Документы/LocalRepository/robotze/My First Project-7bb85a480131.json"
from google.cloud import speech
client = speech.SpeechClient()
audio = speech.types.RecognitionAudio(
uri='gs://zaudio/audio.mp3')
config = speech.types.RecognitionConfig(
encoding=speech.enums.RecognitionConfig.AudioEncoding.LINEAR16,
language_code='ru-RU',
sample_rate_hertz=44100)
operation = client.long_running_recognize(config=config, audio=audio)
op_result = operation.result()
for result in op_result.results:
for alternative in result.alternatives:
print('=' * 20)
print(alternative.transcript)
print(alternative.confidence)
google.api_core.exceptions.PermissionDenied:403 my-service-account@my-project.iam.gserviceaccount.com没有storage.objects.get访问mybucket / audio.mp3。
/home/joo/anaconda3/bin/python /home/joo/Документы/LocalRepository/robotze/speech-to-text-googlecloud.py
Traceback (most recent call last):
File "/home/joo/anaconda3/lib/python3.6/site-packages/google/api_core/grpc_helpers.py", line 57, in error_remapped_callable
return callable_(*args, **kwargs)
File "/home/joo/anaconda3/lib/python3.6/site-packages/grpc/_channel.py", line 565, in __call__
return _end_unary_response_blocking(state, call, False, None)
File "/home/joo/anaconda3/lib/python3.6/site-packages/grpc/_channel.py", line 467, in _end_unary_response_blocking
raise _Rendezvous(state, None, None, deadline)
grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with:
status = StatusCode.PERMISSION_DENIED
details = "my-service-account@my-project.iam.gserviceaccount.com does not have storage.objects.get access to mybucket/audio.mp3."
debug_error_string = "{"created":"@1565253582.126380437","description":"Error received from peer ipv4:74.125.131.95:443","file":"src/core/lib/surface/call.cc","file_line":1052,"grpc_message":"my-service-account@my-project.iam.gserviceaccount.com does not have storage.objects.get access to mybucket/audio.mp3.","grpc_status":7}"
>
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/joo/Документы/LocalRepository/robotze/speech-to-text-googlecloud.py", line 46, in <module>
operation = client.long_running_recognize(config=config, audio=audio)
File "/home/joo/anaconda3/lib/python3.6/site-packages/google/cloud/speech_v1/gapic/speech_client.py", line 341, in long_running_recognize
request, retry=retry, timeout=timeout, metadata=metadata
File "/home/joo/anaconda3/lib/python3.6/site-packages/google/api_core/gapic_v1/method.py", line 143, in __call__
return wrapped_func(*args, **kwargs)
File "/home/joo/anaconda3/lib/python3.6/site-packages/google/api_core/retry.py", line 273, in retry_wrapped_func
on_error=on_error,
File "/home/joo/anaconda3/lib/python3.6/site-packages/google/api_core/retry.py", line 182, in retry_target
return target()
File "/home/joo/anaconda3/lib/python3.6/site-packages/google/api_core/timeout.py", line 214, in func_with_timeout
return func(*args, **kwargs)
File "/home/joo/anaconda3/lib/python3.6/site-packages/google/api_core/grpc_helpers.py", line 59, in error_remapped_callable
six.raise_from(exceptions.from_grpc_error(exc), exc)
File "<string>", line 3, in raise_from
google.api_core.exceptions.PermissionDenied: 403 my-service-account@my-project.iam.gserviceaccount.com does not have storage.objects.get access to mybucket/audio.mp3.
Process finished with exit code 1
我尝试过的操作:gcloud auth应用程序默认登录-在浏览器中登录有效,但仍然出现403错误
答案 0 :(得分:1)
根据我在您的日志中看到的信息,您可以在代码中对您的服务帐户进行身份验证(当前正在通过以下方式进行身份验证:starting-account-*******-239919.iam.gserviceaccount.com) ,但是该服务帐户对对象“ zaudio / audio.mp3”没有“ storage.objects.get”权限。
因此您可以:
A.-给该服务帐户适当的权限(可能是该存储桶中的角色“ storage.objectViewer”就足够了,但是您也可以将其设置为角色“ storage.admin”,这样它可以拥有更多权限控制该存储桶和其他存储桶。
B.-使用具有适当权限的其他服务帐户进行身份验证。
答案 1 :(得分:0)
我解决了
“google.api_core.exceptions.PermissionDenied: 403 my-service-account@my-project.iam.gserviceaccount.com does not have storage.objects.get access to mybucket/audio.mp3.” issue.
要解决此问题:转到您的存储桶,单击三个点,选择“编辑权限”,将实体设置为“用户”,将名称设置为您的电子邮件(在这种情况下,为my-service-account@my-project.iam .gserviceaccount.com),访问权限:“阅读器”。保存并重试。这样可以解决此问题。无论是否创建了存储桶,都必须执行此步骤以显式设置权限。希望这是有用的。