Skip to content

Commit d35d292

Browse files
MarkDaoustcopybara-github
authored andcommitted
feat: Add registerFiles for Java.
Register files is a GenAi Only feature (no Vertex). GenAi Doesn't support GoogleCredentials. The register files method needs a GoogleCredentials token, so the user has to pass GoogleCredentials to the `registerFiles` method. PiperOrigin-RevId: 862884363
1 parent 23a7913 commit d35d292

9 files changed

Lines changed: 1014 additions & 1 deletion

File tree

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.genai.examples;
18+
19+
import com.google.auth.oauth2.GoogleCredentials;
20+
import com.google.genai.Client;
21+
import com.google.genai.types.Content;
22+
import com.google.genai.types.File;
23+
import com.google.genai.types.GenerateContentResponse;
24+
import com.google.genai.types.Part;
25+
import com.google.genai.types.RegisterFilesResponse;
26+
import java.io.IOException;
27+
import java.util.Arrays;
28+
import java.util.List;
29+
30+
/**
31+
* An example of how to use the registerFiles method to register GCS files with the Gemini Developer
32+
* API.
33+
*/
34+
public final class RegisterFiles {
35+
public static void main(String[] args) throws IOException {
36+
// Instantiate the client. The client by default uses the Gemini Developer API.
37+
Client client = new Client();
38+
39+
if (client.vertexAI()) {
40+
System.out.println("registerFiles is only supported in the Gemini Developer client.");
41+
System.exit(0);
42+
}
43+
44+
// GoogleCredentials.getApplicationDefault() will use application default credentials.
45+
// Note: registerFiles is only supported by the Gemini Developer API (MLDev), not Vertex AI.
46+
GoogleCredentials credentials =
47+
GoogleCredentials.getApplicationDefault()
48+
.createScoped(
49+
Arrays.asList(
50+
"https://www.googleapis.com/auth/cloud-platform",
51+
"https://www.googleapis.com/auth/devstorage.read_only"));
52+
53+
List<String> uris = Arrays.asList("gs://tensorflow_docs/image.jpg");
54+
55+
RegisterFilesResponse response = client.files.registerFiles(credentials, uris, null);
56+
57+
List<File> files =
58+
response.files().orElseThrow(() -> new RuntimeException("No files returned"));
59+
File file = files.get(0);
60+
61+
System.out.println("Registered file: " + file.uri().get());
62+
63+
// Use the registered file in a generateContent call.
64+
Content content =
65+
Content.fromParts(
66+
Part.fromText("can you summarize this file?"),
67+
Part.fromUri(file.uri().get(), file.mimeType().get()));
68+
69+
GenerateContentResponse genResponse =
70+
client.models.generateContent("gemini-2.5-flash", content, null);
71+
72+
System.out.println("Response: " + genResponse.text());
73+
}
74+
75+
private RegisterFiles() {}
76+
}

src/main/java/com/google/genai/AsyncFiles.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818

1919
package com.google.genai;
2020

21+
import static com.google.common.base.Preconditions.checkNotNull;
22+
2123
import com.fasterxml.jackson.databind.JsonNode;
2224
import com.fasterxml.jackson.databind.node.ObjectNode;
25+
import com.google.auth.oauth2.GoogleCredentials;
2326
import com.google.genai.Common.BuiltRequest;
2427
import com.google.genai.errors.GenAiIOException;
2528
import com.google.genai.types.CreateFileConfig;
@@ -32,9 +35,12 @@
3235
import com.google.genai.types.GetFileConfig;
3336
import com.google.genai.types.ListFilesConfig;
3437
import com.google.genai.types.ListFilesResponse;
38+
import com.google.genai.types.RegisterFilesConfig;
39+
import com.google.genai.types.RegisterFilesResponse;
3540
import com.google.genai.types.UploadFileConfig;
3641
import com.google.genai.types.Video;
3742
import java.io.InputStream;
43+
import java.util.List;
3844
import java.util.concurrent.CompletableFuture;
3945
import java.util.function.Function;
4046

@@ -116,6 +122,20 @@ public CompletableFuture<DeleteFileResponse> delete(String name, DeleteFileConfi
116122
});
117123
}
118124

125+
CompletableFuture<RegisterFilesResponse> privateRegisterFiles(
126+
List<String> uris, RegisterFilesConfig config) {
127+
128+
BuiltRequest builtRequest = files.buildRequestForPrivateRegisterFiles(uris, config);
129+
return this.apiClient
130+
.asyncRequest("post", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())
131+
.thenApplyAsync(
132+
response -> {
133+
try (ApiResponse res = response) {
134+
return files.processResponseForPrivateRegisterFiles(res, config);
135+
}
136+
});
137+
}
138+
119139
/**
120140
* Asynchronously makes an API request to list the available files.
121141
*
@@ -149,6 +169,31 @@ public CompletableFuture<AsyncPager<File>> list(ListFilesConfig config) {
149169
request.apply(finalConfig)));
150170
}
151171

172+
/**
173+
* Asynchronously registers Google Cloud Storage files for use with the API.
174+
*
175+
* @param credentials The Google Cloud credentials to use for registering the files.
176+
* @param uris The list of GCS URIs to register.
177+
* @param config Optional configuration for the registration request.
178+
* @return A future that resolves to the response containing the registered files.
179+
*/
180+
public CompletableFuture<RegisterFilesResponse> registerFiles(
181+
GoogleCredentials credentials, List<String> uris, RegisterFilesConfig config) {
182+
if (this.apiClient.vertexAI()) {
183+
CompletableFuture<RegisterFilesResponse> future = new CompletableFuture<>();
184+
future.completeExceptionally(
185+
new UnsupportedOperationException(
186+
"This method is only supported in the Gemini Developer client."));
187+
return future;
188+
}
189+
checkNotNull(credentials, "credentials cannot be null");
190+
checkNotNull(uris, "uris cannot be null");
191+
192+
return CompletableFuture.supplyAsync(
193+
() -> files.internalPrepareRegisterFilesConfig(credentials, config))
194+
.thenCompose(updatedConfig -> privateRegisterFiles(uris, updatedConfig));
195+
}
196+
152197
/**
153198
* Asynchronously uploads a file to the GenAI API.
154199
*

0 commit comments

Comments
 (0)