Skip to content

Commit 8e7b3fd

Browse files
qiaodevcopybara-github
authored andcommitted
feat: Support files delete, get, list, download/
PiperOrigin-RevId: 744834118
1 parent 65b7c1c commit 8e7b3fd

File tree

12 files changed

+943
-77
lines changed

12 files changed

+943
-77
lines changed

api_client.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"log"
2626
"math"
2727
"net/http"
28+
// "net/http/httputil"
2829
"net/url"
2930
"os"
3031
"runtime"
@@ -70,6 +71,19 @@ func sendRequest(ctx context.Context, ac *apiClient, path string, method string,
7071
return deserializeUnaryResponse(resp)
7172
}
7273

74+
func downloadFile(ctx context.Context, ac *apiClient, path string, httpOptions *HTTPOptions) ([]byte, error) {
75+
req, err := buildRequest(ctx, ac, path, nil, http.MethodGet, httpOptions)
76+
if err != nil {
77+
return nil, err
78+
}
79+
80+
resp, err := doRequest(ac, req)
81+
if err != nil {
82+
return nil, err
83+
}
84+
return io.ReadAll(resp.Body)
85+
}
86+
7387
func mapToStruct[R any](input map[string]any, output *R) error {
7488
b := new(bytes.Buffer)
7589
err := json.NewEncoder(b).Encode(input)

client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ type Client struct {
3737
Caches *Caches
3838
// Chats provides util functions for creating a new chat session.
3939
Chats *Chats
40-
// Operations provides access to long-running operations.
41-
Operations *Operations
4240
// Files provides access to the Files service.
4341
Files *Files
42+
// Operations provides access to long-running operations.
43+
Operations *Operations
4444
}
4545

4646
// Backend is the GenAI backend to use for the client.

examples/files/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ example using the following commands.
2222
```
2323
$ go get google.golang.org/genai
2424
$ cd `go list -f '{{.Dir}}' google.golang.org/genai/examples/files`
25-
$ go run files.go
25+
$ go run list_download.go
2626
```

examples/files/list_download.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
//go:build ignore_vet
16+
17+
package main
18+
19+
import (
20+
"context"
21+
"flag"
22+
"fmt"
23+
"log"
24+
25+
"google.golang.org/genai"
26+
)
27+
28+
var model = flag.String("model", "gemini-2.0-flash", "the model name, e.g. gemini-2.0-flash")
29+
30+
func run(ctx context.Context) {
31+
client, err := genai.NewClient(ctx, nil)
32+
if err != nil {
33+
log.Fatal(err)
34+
}
35+
if client.ClientConfig().Backend == genai.BackendVertexAI {
36+
fmt.Println("Calling VertexAI Backend...")
37+
} else {
38+
fmt.Println("Calling GeminiAPI Backend...")
39+
}
40+
41+
// Create a new Chat.
42+
for file, err := range client.Files.All(ctx) {
43+
if err != nil {
44+
log.Fatal(err)
45+
}
46+
data, err := client.Files.Download(ctx, file, nil)
47+
if err != nil {
48+
log.Printf("Download %s failed: %w\n", file.Name, err)
49+
} else {
50+
fmt.Printf("Downloaded %s. Data size: %d\n", file.Name, len(data))
51+
}
52+
}
53+
}
54+
55+
func main() {
56+
ctx := context.Background()
57+
flag.Parse()
58+
run(ctx)
59+
}

examples/models/generate_videos/videos.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ func run(ctx context.Context) {
3939
} else {
4040
fmt.Println("Calling GeminiAI GenerateVideo API...")
4141
}
42-
// Pass in basic config
43-
var config *genai.GenerateVideosConfig = &genai.GenerateVideosConfig{
44-
OutputGCSURI: "gs://unified-genai-tests/tmp/genai/video/outputs",
42+
var config genai.GenerateVideosConfig
43+
if client.ClientConfig().Backend == genai.BackendVertexAI {
44+
config.OutputGCSURI = "gs://unified-genai-tests/tmp/genai/video/outputs"
4545
}
4646
// Call the GenerateVideo method.
47-
operation, err := client.Models.GenerateVideos(ctx, *model, "A neon hologram of a cat driving at top speed", nil, config)
47+
operation, err := client.Models.GenerateVideos(ctx, *model, "A neon hologram of a cat driving at top speed", nil, &config)
4848
if err != nil {
4949
log.Fatal(err)
5050
}
@@ -65,6 +65,18 @@ func run(ctx context.Context) {
6565
}
6666
// Log the output.
6767
fmt.Println(string(response))
68+
69+
// Download the video file.
70+
if client.ClientConfig().Backend != genai.BackendVertexAI {
71+
for _, v := range operation.Response.GeneratedVideos {
72+
data, err := client.Files.Download(ctx, genai.NewDownloadURIFromGeneratedVideo(v), nil)
73+
if err != nil {
74+
log.Println(err)
75+
continue
76+
}
77+
fmt.Printf("Video file %s downloaded. Data size: %d. \n", v.Video.URI, len(data))
78+
}
79+
}
6880
}
6981

7082
func main() {

0 commit comments

Comments
 (0)