本文說明如何管理 Document AI 倉儲中的文件,包括建立、擷取、更新及刪除作業。
什麼是文件?
文件是 Document AI Warehouse 使用的資料模型,用於整理實體文件 (例如 PDF 或 TXT) 及其相關聯的屬性。您可以透過文件作業與 Document AI 倉儲互動。
支援的檔案類型
雖然 Document AI 倉儲的重點是文件,但也會用於管理相關聯的圖片 (例如在保險、工程、建築和研究等產業)。
- Ingest API 支援 PDF、TIFF、JPEG 和 PNG 圖片,以及任何屬性或預先擷取的文字。
- 上傳 UI 支援使用 Document AI OCR 和自訂處理器擷取 PDF。
- 檢視器 UI 支援以 PDF、文字和 Microsoft Office 檔案格式呈現。
事前準備
開始前,請先完成快速入門頁面。
如要建立文件,如果資料位於您自己的 Cloud Storage bucket,您必須授予 Document AI Warehouse 服務帳戶儲存空間物件檢視者權限,才能讀取資料。
每個文件都由結構定義指定,且屬於某個文件類型。文件結構定義會定義 Document AI 倉儲中的文件結構。 您必須先建立文件結構定義,才能建立文件。
建立文件
如要建立文件,您必須向 Document AI Warehouse 提供原始文件內容。如要提供原始文件位元組內容,可以設定 Document.inline_raw_document 或 Document.raw_document_path。
兩者差異如下:
Document.raw_document_path:建議採用這種做法。並使用要擷取的檔案的 Cloud Storage 路徑 (gs://bucket/object)。請注意,呼叫者必須具備這個物件的讀取權限,呼叫才能成功。Document.inline_raw_document:檔案的位元組/文字表示法,直接提供給端點。
如要建立文件,請按照下列步驟操作:
上傳 Cloud Storage 中的文件
您必須按照必要條件一節所述,授予 Document AI Warehouse 服務帳戶 Cloud Storage bucket 的存取權。
您必須按照操作說明,將檔案上傳至 Cloud Storage bucket。
REST
要求:
curl --location --request POST --url https://contentwarehouse.googleapis.com/v1/projects/PROJECT_NUMBER/locations/LOCATION/documents \
--header "Authorization: Bearer $(gcloud auth print-access-token)" \
--header "Content-Type: application/json; charset=utf-8" \
--data '{
"document": {
"display_name": "TestDoc3",
"document_schema_name": "projects/PROJECT_NUMBER/locations/LOCATION/documentSchemas/DOCUMENT_SCHEMA_ID",
"raw_document_path": "gs://BUCKET_URI/FILE_URI",
"properties": [
{
"name": "supplier_name",
"text_values": {
"values": "Stanford Plumbing & Heating"
}
},
{
"name": "total_amount",
"float_values": {
"values": "1091.81"
}
},
]
},
"requestMetadata":{
"userInfo":{
"id": "user:USER_EMAIL_ID"
}
}
}'從本機上傳
REST
要求:
curl --location --request POST --url https://contentwarehouse.googleapis.com/v1/projects/PROJECT_NUMBER/locations/LOCATION/documents/ \
--header "Authorization: Bearer $(gcloud auth print-access-token)" \
--header "Content-Type: application/json; charset=utf-8" \
--data '{
"document": {
"display_name": "TestDoc3",
"document_schema_name": "projects/PROJECT_NUMBER/locations/LOCATION/documentSchemas/DOCUMENT_SCHEMA_ID",
"inline_raw_document": "<bytes>",
"properties": [
{
"name": "supplier_name",
"text_values": {
"values": "Stanford Plumbing & Heating"
}
},
{
"name": "total_amount",
"float_values": {
"values": "1091.81"
}
},
]
},
"requestMetadata": {
"userInfo": {
"id": "user:USER_EMAIL_ID"
}
}
}'取得文件
由 document_id 提供:
REST
curl --request POST \
--header "Authorization: Bearer $(gcloud auth print-access-token)" \
--header "Content-Type: application/json; charset=UTF-8" -d '{
"requestMetadata":{
"userInfo":{
"id": "user:USER_EMAIL"
}
}
}' \
"https://contentwarehouse.googleapis.com/v1/projects/PROJECT_NUMBER/locations/LOCATION/documents/DOCUMENT_ID:get"Python
詳情請參閱 Document AI Warehouse Python API 參考文件。
如要向 Document AI Warehouse 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
Java
詳情請參閱 Document AI Warehouse Java API 參考文件。
如要向 Document AI Warehouse 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
Node.js
詳情請參閱 Document AI Warehouse Node.js API 參考文件。
如要向 Document AI Warehouse 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
由 reference_id 提供:
curl --request POST \
--header "Authorization: Bearer $(gcloud auth print-access-token)" \
--header "Content-Type: application/json; charset=UTF-8" -d '{
"requestMetadata":{
"userInfo":{
"id": "user:USER_EMAIL"
}
}
}' \
"https://contentwarehouse.googleapis.com/v1/projects/PROJECT_NUMBER/locations/LOCATION/documents/referenceId/REFERENCE_ID:get"更新文件
由 document_id 提供:
REST
posix-terminal
curl --location --request POST --url https://contentwarehouse.googleapis.com/v1/projects/PROJECT_NUMBER/locations/LOCATION/documents \
--header "Authorization: Bearer $(gcloud auth print-access-token)" \
--header "Content-Type: application/json; charset=utf-8" \
--data '{
"document": {
"display_name": "TestDoc3",
"document_schema_name": "projects/PROJECT_NUMBER/locations/LOCATION/documentSchemas/DOCUMENT_SCHEMA_ID",
"raw_document_path": "gs://BUCKET_URI/FILE_URI",
"properties": [
{
"name": "supplier_name",
"text_values": {
"values": "Stanford Plumbing & Heating"
}
},
{
"name": "total_amount",
"float_values": {
"values": "1091.81"
}
},
{
"name": "invoice_id",
"text_values": {
"values": "invoiceid"
}
},
]
},
"requestMetadata": {
"userInfo": {
"id": "user:USER_EMAIL"
}
}
}'
Python
詳情請參閱 Document AI Warehouse Python API 參考文件。
如要向 Document AI Warehouse 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
Java
詳情請參閱 Document AI Warehouse Java API 參考文件。
如要向 Document AI Warehouse 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
由 reference_id 提供:
curl --location --request POST --url https://contentwarehouse.googleapis.com/v1/projects/PROJECT_NUMBER/locations/LOCATION/documents \
--header "Authorization: Bearer $(gcloud auth print-access-token)" \
--header "Content-Type: application/json; charset=utf-8" \
--data '{
"document": {
"display_name": "TestDoc3",
"document_schema_name": "projects/PROJECT_NUMBER/locations/LOCATION/documentSchemas/referenceId/REFERENCE_ID",
"raw_document_path": "gs://BUCKET_URI/FILE_URI",
"properties": [
{
"name": "supplier_name",
"text_values": {
"values": "Stanford Plumbing & Heating"
}
},
{
"name": "total_amount",
"float_values": {
"values": "1091.81"
}
},
{
"name": "invoice_id",
"text_values": {
"values": "invoiceid"
}
},
]
},
"requestMetadata": {
"userInfo": {
"id": "user:USER_EMAIL"
}
}
}'刪除文件
REST
由 document_id 提供:
curl --request POST \
--header "Authorization: Bearer $(gcloud auth print-access-token)" \
--header "Content-Type: application/json; charset=UTF-8" -d '{
"requestMetadata":{
"userInfo":{
"id": "user:USER_EMAIL"
}
}
}' \
"https://contentwarehouse.googleapis.com/v1/projects/PROJECT_NUMBER/locations/LOCATION/documents/DOCUMENT_ID:delete"由 reference_id 提供:
curl --request POST \
--header "Authorization: Bearer $(gcloud auth print-access-token)" \
--header "Content-Type: application/json; charset=UTF-8" -d '{
"requestMetadata":{
"userInfo":{
"id": "user:USER_EMAIL"
}
}
}' \
"https://contentwarehouse.googleapis.com/v1/projects/PROJECT_NUMBER/locations/LOCATION/documents/referenceId/REFERENCE_ID":delete"後續步驟
- 請參閱「將文件整理到資料夾中」,瞭解如何將文件整理到資料夾中。