![](/img/trans.png)
[英]PERMISSION_DENIED: Cloud IAM permission 'cloudtranslate.generalModels.predict' denied
[英]IAM permission issue with Batch Translation: PermissionDenied: 403 Cloud IAM permission 'cloudtranslate.generalModels.batchPredict' denied
這是我第一次運行谷歌批量翻譯,我通常運行 API 翻譯,但這次我的文件太大,不能這樣翻譯。
我嘗試運行以下腳本,但出現以下權限錯誤,我不確定如何繼續。 任何幫助深表感謝..
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 CLoud 控制台,並在您的服務帳戶中添加所需的角色。 為此,請轉到此頁面並搜索cloudtranslate.generalModels.batchPredict
。
然后,您可以看到在包含所需權限的服務帳戶上授予的可能角色。
在 IAM 角色中添加 **roles/cloudtranslate.editor **
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.