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

This is my first time running google batch translation, I usually run the API translation, but this time my file size is too large to be translated that way.

I tried to run the following script but I get the permission error below and I am not sure how to move forward. Any help is much appreciated..

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 permission 'cloudtranslate.generalModels.batchPredict' denied.



Solution 1:[1]

In your code you reference a service account key file (not ideal, but it's not the purpose). This file contain the credential of a service account (you can see the email of it in the file).

So, go to the Google CLoud console, in the IAM service, and add the required role on your service account. For this, go to this page and search for cloudtranslate.generalModels.batchPredict.

Then, you can see the possible role to grant on the service account that contain the required permission.

Solution 2:[2]

add **roles/cloudtranslate.editor in IAM role **

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 guillaume blaquiere
Solution 2 Satnam