我正在尝试使用 Google 语音转文本服务,根据 https://googleapis.github.io/google-cloud-python/latest/speech/index.html 我已经创建了项目,将音频上传到 gs:cloud,添加了权限,下载了名为 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 [电子邮件受保护] 没有 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 = "[email protected] 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":"[email protected] 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 [email protected] does not have storage.objects.get access to mybucket/audio.mp3.
Process finished with exit code 1
我尝试过的:gcloud auth应用程序默认登录-在浏览器中登录可以工作,但仍然出现403错误
从我在您的日志中看到的内容,您可以在代码中对您的服务帐户进行身份验证(您当前正在使用以下身份验证:starting-account-********-239919.iam.gserviceaccount.com),但是,该服务帐户对对象“zaudio/audio.mp3”没有“storage.objects.get”权限。
所以你可以:
A.- 为该服务帐户授予适当的权限(可能该存储桶内的角色“storage.objectViewer”就足够了,但您也可以使用角色“storage.admin”对其进行设置,以便它可以更好地控制那个桶和其他)。
B.- 使用具有适当权限的其他服务帐户进行身份验证。
我解决了以下问题:
“google.api_core.exceptions.PermissionDenied: 403 [email protected] does not have storage.objects.get access to mybucket/audio.mp3.”
要解决此问题:
保存并重试。这应该可以解决这个问题。无论您是否创建了存储桶,您都必须执行此步骤来显式设置权限。希望这有用。