Python 批翻译的IAM权限问题:PermissionDenied:403 Cloud IAM权限';cloudtranslate.generalModels.batchPredict';否认
这是我第一次运行google批处理翻译,我通常运行API翻译,但这次我的文件太大,无法以这种方式进行翻译 我试图运行以下脚本,但我得到下面的权限错误,我不知道如何前进。非常感谢您的帮助Python 批翻译的IAM权限问题:PermissionDenied:403 Cloud IAM权限';cloudtranslate.generalModels.batchPredict';否认,python,google-cloud-platform,translation,Python,Google Cloud Platform,Translation,这是我第一次运行google批处理翻译,我通常运行API翻译,但这次我的文件太大,无法以这种方式进行翻译 我试图运行以下脚本,但我得到下面的权限错误,我不知道如何前进。非常感谢您的帮助 from google.cloud import translate os.environ["GOOGLE_APPLICATION_CREDENTIALS"]= r"C:\Users\..my_path..\cred.json" # translate_client =
from google.cloud import translate
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]= r"C:\Users\..my_path..\cred.json"
# translate_client = translate.Client()
def batch_translate_text(
input_uri="gs://....",
output_uri="gs://....",
project_id="....",
timeout=180):
"""Translates a batch of texts on GCS and stores the result in a GCS location."""
client = translate.TranslationServiceClient()
location = "us-central1"
# Supported file types: https://cloud.google.com/translate/docs/supported-formats
gcs_source = {"input_uri": input_uri}
input_configs_element = {
"gcs_source": gcs_source,
"mime_type": "text/plain", # Can be "text/plain" or "text/html".
}
gcs_destination = {"output_uri_prefix": output_uri}
output_config = {"gcs_destination": gcs_destination}
parent = f"projects/{project_id}/locations/{location}"
# Supported language codes: https://cloud.google.com/translate/docs/language
operation = client.batch_translate_text(
request={
"parent": parent,
"source_language_code": "zh-CN",
"target_language_codes": ["en"], # Up to 10 language codes here.
"input_configs": [input_configs_element],
"output_config": output_config,
}
)
print("Waiting for operation to complete...")
response = operation.result(timeout)
print("Total Characters: {}".format(response.total_characters))
print("Translated Characters: {}".format(response.translated_characters))
batch_translate_text()
PermissionDenied:403 Cloud IAM权限“cloudtranslate.generalModels.batchPredict”被拒绝。在代码中引用服务帐户密钥文件(不理想,但不是目的)。此文件包含服务帐户的凭据(您可以在文件中看到该帐户的电子邮件) 因此,转到IAM服务中的Google云控制台,在您的服务帐户上添加所需的角色。为此,请转到并搜索
cloudtranslate.generalModels.batchPredict
然后,您可以看到在包含所需权限的服务帐户上授予的可能角色。在代码中,您引用了一个服务帐户密钥文件(不理想,但这不是目的)。此文件包含服务帐户的凭据(您可以在文件中看到该帐户的电子邮件) 因此,转到IAM服务中的Google云控制台,在您的服务帐户上添加所需的角色。为此,请转到并搜索
cloudtranslate.generalModels.batchPredict
然后,您可以在包含所需权限的服务帐户上查看要授予的可能角色