批量翻译的 IAM 权限问题:PermissionDenied:403 Cloud IAM 权限 'cloudtranslate.generalModels.batchPredict' 被拒绝

IAM permission issue with Batch Translation: PermissionDenied: 403 Cloud IAM permission 'cloudtranslate.generalModels.batchPredict' denied

这是我第一次运行宁google批量翻译,我一般运行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' 被拒绝。

在您的代码中,您引用了服务帐户密钥文件(不理想,但这不是目的)。此文件包含服务帐户的凭据(您可以在文件中看到它的电子邮件)。

因此,转到 Google CLoud 控制台,在 IAM 服务中,并在您的服务帐户上添加所需的角色。为此,请转到 this page 并搜索 cloudtranslate.generalModels.batchPredict.

然后,您可以看到可以授予包含所需权限的服务帐户的角色。

在 IAM 角色中添加 **roles/cloudtranslate.editor **