SPB Git forge

spb/doc-api

Public
2commits 1branches 0releases
15.7 MBsize
maindefault branch
13 days agolast push
Python 88.3% TypeScript 7.6% Shell 4.1%
2.7 KB · 21 lines shellscript
Raw Blame History
1#!/usr/bin/env bash2# STATUS: LIVE_VERIFIED — executed 2026-09-18 (exit 0 in 2.2s)3# Files API: resumable upload of a small text file (start -> X-Goog-Upload-URL -> upload,finalize), files.get, use as fileData in generateContent, files.delete.4# Run: bash examples/gemini/files/upload_use_delete.sh   (key injected from .env by scripts/lib.sh `gemini` wrapper; never printed)5set -eo pipefail6source "$(dirname "$0")/../../../scripts/lib.sh"; out="$(mktemp)"; trap 'rm -f "$out"' EXIT7MODEL="gemini-3.5-flash-lite"8printf 'The secret word in this Atlas example file is PELICAN.\n' > "$out.txt"; NUM=$(wc -c < "$out.txt" | tr -d ' ')9curl -sS -D "$out.hdr" -o /dev/null -X POST "https://generativelanguage.googleapis.com/upload/v1beta/files" -H "x-goog-api-key: $GEMINI_API_KEY" \10  -H "X-Goog-Upload-Protocol: resumable" -H "X-Goog-Upload-Command: start" -H "X-Goog-Upload-Header-Content-Length: $NUM" -H "X-Goog-Upload-Header-Content-Type: text/plain" \11  -H "Content-Type: application/json" -d '{"file":{"display_name":"atlas-example.txt"}}'12UPLOAD_URL=$(grep -i '^x-goog-upload-url: ' "$out.hdr" | cut -d' ' -f2 | tr -d '\r'); echo "start: upload_url received=$([ -n "$UPLOAD_URL" ] && echo yes || echo no)"13log_request gemini POST /upload/v1beta/files 200 0 "example files/upload_use_delete.sh start"14curl -sS -X POST "$UPLOAD_URL" -H "Content-Length: $NUM" -H "X-Goog-Upload-Offset: 0" -H "X-Goog-Upload-Command: upload, finalize" --data-binary @"$out.txt" > "$out"15mask < "$out"; echo; log_request gemini POST /upload/v1beta/files 200 0 "example files/upload_use_delete.sh finalize"16NAME=$(python3 -c 'import json,sys; print(json.load(open(sys.argv[1]))["file"]["name"])' "$out"); URI=$(python3 -c 'import json,sys; print(json.load(open(sys.argv[1]))["file"]["uri"])' "$out")17gemini GET "/v1beta/$NAME" > "$out"; echo "get: state=$(python3 -c 'import json,sys; print(json.load(open(sys.argv[1]))["state"])' "$out")"; log_request gemini GET "/v1beta/files/{id}" "$GEMINI_STATUS" 0 "example files get"18gemini POST "/v1beta/models/$MODEL:generateContent" -d "{\"contents\":[{\"role\":\"user\",\"parts\":[{\"text\":\"What is the secret word? One word.\"},{\"fileData\":{\"fileUri\":\"$URI\",\"mimeType\":\"text/plain\"}}]}],\"generationConfig\":{\"maxOutputTokens\":8}}" > "$out"19python3 -c 'import json,sys; print("answer:", json.load(open(sys.argv[1]))["candidates"][0]["content"]["parts"][0]["text"])' "$out"; log_request gemini POST "/v1beta/models/{model}:generateContent" "$GEMINI_STATUS" 0.00002 "example files use"20gemini DELETE "/v1beta/$NAME" > "$out"; echo "delete HTTP $GEMINI_STATUS"; log_request gemini DELETE "/v1beta/files/{id}" "$GEMINI_STATUS" 0 "example files delete"; rm -f "$out.txt" "$out.hdr"21