檔案輸入方式

本指南說明向 Gemini API 提出要求時,可採用哪些方式加入圖片、音訊、影片和文件等媒體檔案。所有 Gemini API 端點都支援這些新方法,包括 Batch、Interactions 和 Live API。選擇合適的方法取決於檔案大小、資料儲存位置,以及您預計使用檔案的頻率。

如要將檔案做為輸入內容,最簡單的方法是讀取本機檔案,然後將檔案納入提示詞。以下範例說明如何讀取本機 PDF 檔案。透過這個方法上傳的 PDF 檔案大小不得超過 50 MB。如需檔案輸入類型和限制的完整清單,請參閱「輸入法比較表」。

Python

from google import genai
import pathlib

client = genai.Client()

filepath = pathlib.Path('my_local_file.pdf')

prompt = "Summarize this document"
interaction = client.interactions.create(
    model="gemini-3-flash-preview",
    input=[
        {"type": "text", "text": prompt},
        {"type": "document", "data": filepath.read_bytes(), "mime_type": "application/pdf"}
    ]
)
# Print the model's text response
for step in interaction.steps:
    if step.type == "model_output":
        for content_block in step.content:
            if content_block.type == "text":
                print(content_block.text)

JavaScript

import { GoogleGenAI } from "@google/genai";
import * as fs from 'node:fs';

const client = new GoogleGenAI({});
const prompt = "Summarize this document";

async function main() {
    const filePath = 'my_local_file.pdf';

    const interaction = await client.interactions.create({
        model: "gemini-3-flash-preview",
        input: [
            { type: "text", text: prompt },
            {
                type: "document",
                data: fs.readFileSync(filePath).toString("base64"),
                mimeType: "application/pdf"
            }
        ]
    });
    const modelStep = interaction.steps.find(s => s.type === 'model_output');
    if (modelStep) {
      for (const contentBlock of modelStep.content) {
        if (contentBlock.type === 'text') console.log(contentBlock.text);
      }
    }
}

main();

REST

# Encode the local file to base64
B64_CONTENT=$(base64 -w 0 my_local_file.pdf)

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "gemini-3-flash-preview",
    "input": [
      {"type": "text", "text": "Summarize this document"},
      {
        "type": "document",
        "data": "'${B64_CONTENT}'",
        "mimeType": "application/pdf"
      }
    ]
  }'

輸入法比較

下表比較各種輸入方式的檔案限制和最佳用途。請注意,檔案大小上限可能會因檔案類型和用於處理檔案的模型或分詞器而異。

方法 適用情境 檔案大小上限 持續性
內嵌資料 快速測試、小型檔案、即時應用程式。 每個要求或酬載 100 MB
(PDF 檔案為 50 MB)
無 (隨每個要求傳送)
檔案 API 上傳 大型檔案、多次使用的檔案。 每個檔案 2 GB,
每個專案最多 20 GB
48 小時
File API GCS URI 註冊 已存放在 Google Cloud Storage 的大型檔案、多次使用的檔案。 每個檔案 2 GB,沒有總儲存空間限制 無 (系統會根據要求擷取)。一次註冊最多可存取 30 天。
外部網址 公用資料或雲端儲存空間 (AWS、Azure、GCS) 中的資料,不必重新上傳。 每個要求/酬載 100 MB 無 (每次要求都會擷取)

內嵌資料

如果是較小的檔案 (小於 100 MB,PDF 檔案則小於 50 MB),您可以直接在要求酬載中傳遞資料。這是最簡單的方法,適用於快速測試或處理即時暫時性資料的應用程式。您可以提供 base64 編碼字串,或直接讀取本機檔案。

如需從本機檔案讀取的範例,請參閱本頁開頭的範例。

從網址擷取

您也可以從網址擷取檔案、轉換為位元組,然後納入輸入內容。

Python

from google import genai
import httpx

client = genai.Client()

doc_url = "https://discovery.ucl.ac.uk/id/eprint/10089234/1/343019_3_art_0_py4t4l_convrt.pdf"
doc_data = httpx.get(doc_url).content

prompt = "Summarize this document"

interaction = client.interactions.create(
    model="gemini-3-flash-preview",
    input=[
        {"type": "document", "data": doc_data, "mime_type": "application/pdf"},
        {"type": "text", "text": prompt}
    ]
)
# Print the model's text response
for step in interaction.steps:
    if step.type == "model_output":
        for content_block in step.content:
            if content_block.type == "text":
                print(content_block.text)

JavaScript

import { GoogleGenAI } from "@google/genai";

const client = new GoogleGenAI({});
const docUrl = 'https://discovery.ucl.ac.uk/id/eprint/10089234/1/343019_3_art_0_py4t4l_convrt.pdf';
const prompt = "Summarize this document";

async function main() {
    const pdfResp = await fetch(docUrl)
      .then((response) => response.arrayBuffer());

    const interaction = await client.interactions.create({
        model: "gemini-3-flash-preview",
        input: [
            { type: "text", text: prompt },
            {
                type: "document",
                data: Buffer.from(pdfResp).toString("base64"),
                mimeType: "application/pdf"
            }
        ]
    });
    const modelStep = interaction.steps.find(s => s.type === 'model_output');
    if (modelStep) {
      for (const contentBlock of modelStep.content) {
        if (contentBlock.type === 'text') console.log(contentBlock.text);
      }
    }
}

main();

REST

DOC_URL="https://discovery.ucl.ac.uk/id/eprint/10089234/1/343019_3_art_0_py4t4l_convrt.pdf"
PROMPT="Summarize this document"
DISPLAY_NAME="base64_pdf"

# Download the PDF
wget -O "${DISPLAY_NAME}.pdf" "${DOC_URL}"

# Check for FreeBSD base64 and set flags accordingly
if [[ "$(base64 --version 2>&1)" = *"FreeBSD"* ]]; then
  B64FLAGS="--input"
else
  B64FLAGS="-w0"
fi

# Base64 encode the PDF
ENCODED_PDF=$(base64 $B64FLAGS "${DISPLAY_NAME}.pdf")

# Generate content using interactions
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
    -H "x-goog-api-key: $GEMINI_API_KEY" \
    -H 'Content-Type: application/json' \
    -d '{
      "model": "gemini-3-flash-preview",
      "input": [
        {"type": "document", "data": "'$ENCODED_PDF'", "mimeType": "application/pdf"},
        {"type": "text", "text": "'$PROMPT'"}
      ]
    }' 2> /dev/null > response.json

cat response.json
echo

jq ".steps[] | select(.type == \"model_output\") | .content[] | select(.type == \"text\") | .text" response.json

Gemini File API

File API 適用於較大的檔案 (最多 2 GB),或您打算在多項要求中使用的檔案。

標準檔案上傳

將本機檔案上傳至 Gemini API。以這種方式上傳的檔案會暫時儲存 (48 小時),並經過處理,方便模型有效率地擷取內容。

Python

from google import genai

client = genai.Client()

# Upload the file
audio_file = client.files.upload(file="path/to/your/sample.mp3")
prompt = "Describe this audio clip"

# Use the uploaded file in an interaction
interaction = client.interactions.create(
    model="gemini-3-flash-preview",
    input=[
        {"type": "text", "text": prompt},
        {"type": "audio", "uri": audio_file.uri, "mime_type": audio_file.mime_type}
    ]
)
# Print the model's text response
for step in interaction.steps:
    if step.type == "model_output":
        for content_block in step.content:
            if content_block.type == "text":
                print(content_block.text)

JavaScript

import { GoogleGenAI } from "@google/genai";

const client = new GoogleGenAI({});
const prompt = "Describe this audio clip";

async function main() {
  const filePath = "path/to/your/sample.mp3";

  const myfile = await client.files.upload({
    file: filePath,
    config: { mimeType: "audio/mpeg" },
  });

  const interaction = await client.interactions.create({
    model: "gemini-3-flash-preview",
    input: [
        { type: "text", text: prompt },
        { type: "audio", uri: myfile.uri, mimeType: myfile.mimeType }
    ]
  });
  const modelStep = interaction.steps.find(s => s.type === 'model_output');
  if (modelStep) {
    for (const contentBlock of modelStep.content) {
      if (contentBlock.type === 'text') console.log(contentBlock.text);
    }
  }
}

await main();

REST

AUDIO_PATH="path/to/sample.mp3"
MIME_TYPE=$(file -b --mime-type "${AUDIO_PATH}")
NUM_BYTES=$(wc -c < "${AUDIO_PATH}")
DISPLAY_NAME=AUDIO

tmp_header_file=upload-header.tmp

# Initial resumable request defining metadata.
curl "https://generativelanguage.googleapis.com/upload/v1beta/files" \
  -D "${tmp_header_file}" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H "X-Goog-Upload-Protocol: resumable" \
  -H "X-Goog-Upload-Command: start" \
  -H "X-Goog-Upload-Header-Content-Length: ${NUM_BYTES}" \
  -H "X-Goog-Upload-Header-Content-Type: ${MIME_TYPE}" \
  -H "Content-Type: application/json" \
  -d "{'file': {'display_name': '${DISPLAY_NAME}'}}" 2> /dev/null

upload_url=$(grep -i "x-goog-upload-url: " "${tmp_header_file}" | cut -d" " -f2 | tr -d "\r")
rm "${tmp_header_file}"

# Upload the actual bytes.
curl "${upload_url}" \
  -H "Content-Length: ${NUM_BYTES}" \
  -H "X-Goog-Upload-Offset: 0" \
  -H "X-Goog-Upload-Command: upload, finalize" \
  --data-binary "@${AUDIO_PATH}" 2> /dev/null > file_info.json

file_uri=$(jq ".file.uri" file_info.json)

# Now use in an interaction
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
    -H "x-goog-api-key: $GEMINI_API_KEY" \
    -H 'Content-Type: application/json' \
    -d '{
      "model": "gemini-3-flash-preview",
      "input": [
        {"type": "text", "text": "Describe this audio clip"},
        {"type": "audio", "uri": '$file_uri', "mimeType": "'${MIME_TYPE}'"}
      ]
    }'

註冊 Google Cloud Storage 檔案

如果資料已儲存在 Google Cloud Storage,就不需要下載並重新上傳。您可以直接透過 File API 註冊。

  1. 授予服務代理每個 bucket 的存取權

    1. 在 Google Cloud 專案中啟用 Gemini API。

    2. 建立服務代理:

      gcloud beta services identity create --service=generativelanguage.googleapis.com --project=<your_project>

    3. 授予 Gemini API 服務代理讀取儲存空間 bucket 的權限

      使用者必須在要使用的特定儲存空間 bucket 中,將 Storage Object Viewer IAM 角色 指派給這個服務代理。

    這項存取權預設不會過期,但隨時可以變更。您也可以使用 Google Cloud Storage IAM SDK 指令授予權限。

  2. 驗證服務

    必要條件

    • 啟用 API
    • 建立具備適當權限的服務帳戶或代理程式。

    您必須先以具備 Storage 物件檢視者權限的服務身分進行驗證。這取決於檔案管理程式碼的執行環境。

    Google Cloud 以外

    如果您的程式碼是在 Google Cloud 以外的位置 (例如桌面) 執行,請按照下列步驟,從 Google Cloud 控制台下載帳戶憑證:

    1. 瀏覽至服務帳戶控制台
    2. 選取相關服務帳戶
    3. 選取「金鑰」分頁,然後依序選擇「新增金鑰」和「建立新的金鑰」
    4. 選擇「JSON」金鑰類型,並記下檔案下載到電腦的位置。

    詳情請參閱 Google Cloud 官方說明文件,瞭解如何管理服務帳戶金鑰

    然後使用下列指令進行驗證。這些指令假設您的服務帳戶檔案位於目前目錄中,且名為 service-account.json

    Python

    from google.oauth2.service_account import Credentials
    
    GCS_READ_SCOPES = [       
      'https://www.googleapis.com/auth/devstorage.read_only',
      'https://www.googleapis.com/auth/cloud-platform'
    ]
    
    SERVICE_ACCOUNT_FILE = 'service-account.json'
    
    credentials = Credentials.from_service_account_file(
        SERVICE_ACCOUNT_FILE,
        scopes=GCS_READ_SCOPES
    )
    

    JavaScript

    const { GoogleAuth } = require('google-auth-library');
    
    const GCS_READ_SCOPES = [
      'https://www.googleapis.com/auth/devstorage.read_only',
      'https://www.googleapis.com/auth/cloud-platform'
    ];
    
    const SERVICE_ACCOUNT_FILE = 'service-account.json';
    
    const auth = new GoogleAuth({
      keyFile: SERVICE_ACCOUNT_FILE,
      scopes: GCS_READ_SCOPES
    });
    

    CLI

    gcloud auth application-default login \
      --client-id-file=service-account.json \
      --scopes='https://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/devstorage.read_only'
    

    在 Google Cloud 中

    如果您直接在 Google Cloud 中執行 (例如使用 Cloud Run 函式Compute Engine 執行個體),您會擁有隱含憑證,但需要重新驗證,才能授予適當的範圍。

    Python

    這段程式碼預期服務會在可自動取得應用程式預設憑證的環境中執行,例如 Cloud Run 或 Compute Engine。

    import google.auth
    
    GCS_READ_SCOPES = [       
      'https://www.googleapis.com/auth/devstorage.read_only',
      'https://www.googleapis.com/auth/cloud-platform'
    ]
    
    credentials, project = google.auth.default(scopes=GCS_READ_SCOPES)
    

    JavaScript

    這段程式碼預期服務會在可自動取得應用程式預設憑證的環境中執行,例如 Cloud Run 或 Compute Engine。

    const { GoogleAuth } = require('google-auth-library');
    
    const auth = new GoogleAuth({
      scopes: [
        'https://www.googleapis.com/auth/devstorage.read_only',
        'https://www.googleapis.com/auth/cloud-platform'
      ]
    });
    

    CLI

    這項指令是互動式指令,對於 Compute Engine 等服務,您可以在設定層級將範圍附加至執行中的服務。如需範例,請參閱使用者自行管理的服務文件

    gcloud auth application-default login \
    --scopes="https://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/devstorage.read_only"
    
  3. 檔案註冊 (Files API)

    使用 Files API 註冊檔案,並產生可直接在 Gemini API 中使用的 Files API 路徑。

    Python

    from google import genai
    
    # Note that you must provide an API key in the GEMINI_API_KEY
    # environment variable, but it is unused for the registration endpoint.
    client = genai.Client(credentials=credentials)
    
    registered_gcs_files = client.files.register_files(
        uris=["gs://my_bucket/some_object.pdf", "gs://bucket2/object2.txt"]
    )
    prompt = "Summarize this file."
    
    # call interactions.create for each file
    for f in registered_gcs_files.files:
      print(f.name)
      interaction = client.interactions.create(
        model="gemini-3-flash-preview",
        input=[
          {"type": "text", "text": prompt},
          {"type": "document", "uri": f.uri, "mime_type": f.mime_type}
        ],
      )
      # Print the model's text response
      for step in interaction.steps:
          if step.type == "model_output":
              for content_block in step.content:
                  if content_block.type == "text":
                      print(content_block.text)
    

    JavaScript

    import { GoogleGenAI } from "@google/genai";
    
    const ai = new GoogleGenAI({ auth: auth });
    
    async function main() {
        const registeredGcsFiles = await ai.files.registerFiles({
            uris: ["gs://my_bucket/some_object.pdf", "gs://bucket2/object2.txt"]
        });
    
        const prompt = "Summarize this file.";
    
        for (const file of registeredGcsFiles.files) {
            console.log(file.name);
            const interaction = await ai.interactions.create({
                model: "gemini-3-flash-preview",
                input: [
                    { type: "text", text: prompt },
                    { type: "document", uri: file.uri, mimeType: file.mimeType }
                ]
            });
    
            const modelStep = interaction.steps.find(s => s.type === 'model_output');
            if (modelStep) {
                for (const contentBlock of modelStep.content) {
                    if (contentBlock.type === 'text') console.log(contentBlock.text);
                }
            }
        }
    }
    
    main();
    

    CLI

    access_token=$(gcloud auth application-default print-access-token)
    project_id=$(gcloud config get-value project)
    curl -X POST https://generativelanguage.googleapis.com/v1beta/files:register \
        -H 'Content-Type: application/json' \
        -H "Authorization: Bearer ${access_token}" \
        -H "x-goog-user-project: ${project_id}" \
        -d '{"uris": ["gs://bucket/object1", "gs://bucket/object2"]}'
    

外部 HTTP / 已簽章網址

您可以在要求中直接傳遞可公開存取的 HTTPS 網址或預先簽署的網址。Gemini API 會在處理期間安全地擷取內容。 如果檔案大小不超過 100 MB,且您不想重新上傳,這個方法就非常適合。

Python

from google import genai

uri = "https://ontheline.trincoll.edu/images/bookdown/sample-local-pdf.pdf"
prompt = "Summarize this file"

client = genai.Client()

interaction = client.interactions.create(
    model="gemini-3-flash-preview",
    input=[
        {"type": "document", "uri": uri, "mime_type": "application/pdf"},
        {"type": "text", "text": prompt}
    ]
)
# Print the model's text response
for step in interaction.steps:
    if step.type == "model_output":
        for content_block in step.content:
            if content_block.type == "text":
                print(content_block.text)

JavaScript

import { GoogleGenAI } from '@google/genai';

const client = new GoogleGenAI({});

const uri = "https://ontheline.trincoll.edu/images/bookdown/sample-local-pdf.pdf";

async function main() {
  const interaction = await client.interactions.create({
    model: 'gemini-3-flash-preview',
    input: [
      { type: "document", uri: uri, mimeType: "application/pdf" },
      { type: "text", text: "summarize this file" }
    ]
  });

  const modelStep = interaction.steps.find(s => s.type === 'model_output');
  if (modelStep) {
    for (const contentBlock of modelStep.content) {
      if (contentBlock.type === 'text') console.log(contentBlock.text);
    }
  }
}

main();

REST

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
      -H 'x-goog-api-key: $GEMINI_API_KEY' \
      -H 'Content-Type: application/json' \
      -d '{
          "model": "gemini-3-flash-preview",
          "input": [
            {"type": "text", "text": "Summarize this pdf"},
            {
              "type": "document",
              "uri": "https://ontheline.trincoll.edu/images/bookdown/sample-local-pdf.pdf",
              "mimeType": "application/pdf"
            }
          ]
        }'

無障礙設定

確認提供的網址不會導向需要登入或位於付費牆後的網頁。如果是私人資料庫,請務必建立具備正確存取權限和有效期限的經簽署網址。

安全檢查

系統會對網址執行內容審核檢查,確認網址符合安全和政策標準。如果網址未通過這項檢查,您會收到 url_retrieval_statusURL_RETRIEVAL_STATUS_UNSAFE

支援的內容類型

這份支援的檔案類型和限制清單僅為初步指引,並未涵蓋所有項目。支援的有效型別組合可能會變更,且會因使用的特定模型和權杖化工具版本而異。如果類型不受支援,系統會顯示錯誤訊息。此外,擷取這些檔案類型的內容時,僅支援可公開存取的網址。

文字檔案類型

  • text/html
  • text/css
  • text/plain
  • text/xml
  • text/csv
  • text/rtf
  • text/javascript

應用程式檔案類型

  • application/json
  • application/pdf

圖片檔案類型

  • image/bmp
  • image/jpeg
  • image/png
  • image/webp

最佳做法

  • 選擇合適的方法:針對小型暫時性檔案使用內嵌資料。如要處理較大或經常使用的檔案,請使用 File API。使用外部網址, 取得已在線上代管的資料。
  • 指定 MIME 類型:請務必為檔案資料提供正確的 MIME 類型,確保系統能正確處理。
  • 處理錯誤:在程式碼中實作錯誤處理機制,管理網路連線失敗、檔案存取問題或 API 錯誤等潛在問題。

限制

  • 檔案大小上限會因方法 (請參閱比較表) 和檔案類型而異。
  • 內嵌資料會增加要求酬載大小。
  • File API 上傳的檔案為暫時性質,會在 48 小時後失效。
  • 每個酬載的外部網址擷取作業上限為 100 MB,且支援特定內容類型。

後續步驟