處理文件

本快速入門課程將說明如何透過 Google Cloud 適用的 ABAP SDK 的 SAP BTP 版本,使用 Document AI API 的批次處理功能,從來源 Cloud Storage 值區處理文件 (月結單),並將已處理的文件 (JSON 檔案) 儲存在目標值區。

事前準備

執行本快速入門前,請確認您或管理員已完成下列先決條件:

  • 請確認您的 Google Cloud 專案已啟用 Document AI API。

    前往 API 程式庫

  • 在 Document AI Workbench 中,建立類型為 INVOICE_PROCESSOR 的處理器。詳情請參閱「建立及管理處理器」。

  • 在 Cloud Storage 中建立來源值區,用來儲存待處理的月結單,並將月結單放入這個值區。詳情請參閱「建立值區」。

  • 在 Cloud Storage 中建立目標值區,用於儲存已處理的檔案。

建立 ABAP 類別來處理文件

  1. 建立套件:

    1. 在 ADT 中前往「Project Explorer」。
    2. ZLOCAL 套件上按一下滑鼠右鍵,然後依序選取「New」>「ABAP Package」
    3. 請輸入下列包裹詳細資料:

      • 「Name」:輸入 ZABAPSDK_TEST
      • 說明:輸入 ABAP SDK Test Package
    4. 點選「下一步」

    5. 在「Select a Transport Request」對話方塊中,選取「Create a new request」核取方塊。

    6. 輸入傳輸要求的說明。

    7. 按一下「完成」

  2. 建立 ABAP 類別來呼叫 Document AI API:

    1. 在 ABAP 套件上按一下滑鼠右鍵,然後依序選取「New」>「ABAP Class」
    2. 請為 ABAP 類別輸入下列詳細資料:

      • 「Name」:輸入 ZGOOG_CL_QS_DOCUMENT_AI
      • 說明:輸入 Quick start for Document AI API
    3. 點選「下一步」

    4. 選取運送要求,然後按一下「完成」

  3. 在程式碼編輯器中,將預設程式碼替換為以下程式碼片段:

    " --------------------------------------------------------------------
    "  Copyright 2024 Google LLC                                         -
    "                                                                    -
    "  Licensed under the Apache License, Version 2.0 (the "License");   -
    "  you may not use this file except in compliance with the License.  -
    "  You may obtain a copy of the License at                           -
    "      https://www.apache.org/licenses/LICENSE-2.0                   -
    "  Unless required by applicable law or agreed to in writing,        -
    "  software distributed under the License is distributed on an       -
    "  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,      -
    "  either express or implied.                                        -
    "  See the License for the specific language governing permissions   -
    "  and limitations under the License.                                -
    " --------------------------------------------------------------------
    CLASS zcl_qs_process_documents DEFINITION
      PUBLIC FINAL
      CREATE PUBLIC.
    
      PUBLIC SECTION.
        INTERFACES if_oo_adt_classrun.
    ENDCLASS.
    
    
    
    CLASS ZCL_QS_PROCESS_DOCUMENTS IMPLEMENTATION.
    
    
      METHOD if_oo_adt_classrun~main.
        DATA lv_p_projects_id   TYPE string.
        DATA lv_p_locations_id  TYPE string.
        DATA lv_p_processors_id TYPE string.
        DATA ls_input           TYPE /goog/cl_documentai_v1=>ty_017.
        DATA lo_docai           TYPE REF TO /goog/cl_documentai_v1.
    
        TRY.
    
            " Open HTTP connection
            " The client key DEMO_DOC_PROCESSING is an example, replace this with actual value
            lo_docai = NEW #( iv_key_name = 'DEMO_DOC_PROCESSING' ).
    
            " Populate relevant parameters to be passed to API
            lv_p_projects_id  = 'PROJECT_ID'.
            lv_p_locations_id = 'LOCATION_ID'.
            lv_p_processors_id = 'PROCESSOR_ID'.
            ls_input-input_documents-gcs_prefix-gcs_uri_prefix = 'SOURCE_BUCKET_URI'.
            ls_input-document_output_config-gcs_output_config-gcs_uri = 'TARGET_BUCKET_URI'.
    
            " Call API method
            lo_docai->batch_process_processors( EXPORTING iv_p_projects_id   = lv_p_projects_id
                                                          iv_p_locations_id  = lv_p_locations_id
                                                          iv_p_processors_id = lv_p_processors_id
                                                          is_input           = ls_input
                                                IMPORTING
                                                          es_output          = DATA(ls_output)
                                                          ev_ret_code        = DATA(lv_ret_code)
                                                          ev_err_text        = DATA(lv_err_text)
                                                          es_err_resp        = DATA(ls_err_resp) ).
    
            IF lo_docai->is_success( lv_ret_code ) = abap_true.
              out->write( |API call successful| ).
            ELSE.
              out->write( |Error occurred during API call| ).
              out->write( lv_err_text ).
            ENDIF.
    
            " Close HTTP connection
            lo_docai->close( ).
    
          CATCH /goog/cx_sdk INTO DATA(lo_exception).
            " Handle exception here
        ENDTRY.
      ENDMETHOD.
    ENDCLASS.
    

    更改下列內容:

    • DEMO_DOC_PROCESSING:用戶端鍵名稱。
    • PROJECT_ID:已啟用 Document AI API 的 Google Cloud 專案 ID。
    • LOCATION_ID:處理器的位置。
    • PROCESSOR_ID:處理器的 ID。
    • SOURCE_BUCKET_URI:Cloud Storage 值區資料夾的 URI,其中儲存了用於處理的來源文件。
    • TARGET_BUCKET_URI:Cloud Storage 值區的 URI,用於儲存已處理的文件 (JSON 檔案)。
  4. 儲存並啟用變更。

  5. 執行應用程式:

    1. 選取 ABAP 類別 ZGOOG_CL_QS_DOCUMENT_AI
    2. 依序點選「執行」>「以身分執行」>「ABAP 應用程式 (控制台)」。或者按下 F9
  6. 如要驗證結果,請按照下列步驟操作:

    1. 在 Google Cloud 控制台,前往「Cloud Storage Buckets」頁面。

      前往「Buckets」(值區) 頁面

    2. 開啟目標值區。處理過的文件會以 JSON 檔案的形式儲存。

後續步驟