Skip to content

Commit 904b1aa

Browse files
yyyu-googlecopybara-github
authored andcommitted
feat: support Files List, Get, Upload, Download, Delete
PiperOrigin-RevId: 839889197
1 parent 1a18f8c commit 904b1aa

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+4034
-2
lines changed

DemoApp/Files/Files.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
using Google.GenAI;
18+
using Google.GenAI.Types;
19+
20+
public class Files {
21+
public static async Task SendRequestAsync() {
22+
string apiKey = System.Environment.GetEnvironmentVariable("GOOGLE_API_KEY");
23+
var geminiClient = new Client(apiKey: apiKey);
24+
try {
25+
var uploadResponse1 = await geminiClient.Files.UploadAsync(filePath: "./google.png");
26+
Console.WriteLine("Gemini API Files UploadAsync Response from file path for png:");
27+
Console.WriteLine(uploadResponse1);
28+
29+
var uploadResponse2 = await geminiClient.Files.UploadAsync(filePath: "./google.jpg");
30+
Console.WriteLine("Gemini API Files UploadAsync Response from file path for jpg:");
31+
Console.WriteLine(uploadResponse2);
32+
33+
var uploadResponse3 = await geminiClient.Files.UploadAsync(filePath: "./story.txt");
34+
Console.WriteLine("Gemini API Files UploadAsync Response from file path for txt:");
35+
Console.WriteLine(uploadResponse3);
36+
37+
var uploadResponse4 = await geminiClient.Files.UploadAsync(filePath: "./story.pdf");
38+
Console.WriteLine("Gemini API Files UploadAsync Response from file path for pdf:");
39+
Console.WriteLine(uploadResponse4);
40+
41+
var uploadResponse5 = await geminiClient.Files.UploadAsync(filePath: "./pixel.m4a");
42+
Console.WriteLine("Gemini API Files UploadAsync Response from file path for m4a:");
43+
Console.WriteLine(uploadResponse5);
44+
45+
byte[] fileBytes = await System.IO.File.ReadAllBytesAsync("./google.png");
46+
var uploadResponse6 = await geminiClient.Files.UploadAsync(
47+
bytes: fileBytes,
48+
fileName: "google.png"
49+
);
50+
Console.WriteLine("Gemini API Files UploadAsync Response from bytes:");
51+
Console.WriteLine(uploadResponse6);
52+
53+
54+
var pager = await geminiClient.Files.ListAsync();
55+
await foreach (var page in pager) {
56+
Console.WriteLine("Gemini API Files ListAsync Response page:");
57+
Console.WriteLine(page);
58+
}
59+
60+
string fileName = uploadResponse6.Name.ToString();
61+
Console.WriteLine($"File name: {fileName}");
62+
var getResponse = await geminiClient.Files.GetAsync(name: fileName);
63+
Console.WriteLine("Gemini API Files GetAsync Response GetAsync:");
64+
Console.WriteLine(getResponse);
65+
66+
var deleteResponse = await geminiClient.Files.DeleteAsync(name: fileName);
67+
Console.WriteLine("Gemini API Files DeleteAsync Response deleted file:");
68+
Console.WriteLine(deleteResponse);
69+
} catch (HttpRequestException ex) {
70+
Console.WriteLine($"An error occurred with Gemini API: {ex.ToString()}");
71+
}
72+
}
73+
}

DemoApp/Files/Files.csproj

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<Nullable>enable</Nullable>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<ProjectReference Include="..\..\Google.GenAI\Google.GenAI.csproj" />
13+
</ItemGroup>
14+
15+
</Project>

DemoApp/Files/Program.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
/// <summary>
18+
/// The main entry point for the application.
19+
/// </summary>
20+
public class Program
21+
{
22+
/// <summary>
23+
/// Runs the Files features of the SDK.
24+
/// </summary>
25+
/// <param name="args">Command-line arguments (not used).</param>
26+
public static async Task Main(string[] args)
27+
{
28+
try
29+
{
30+
await Files.SendRequestAsync();
31+
}
32+
catch (Exception ex)
33+
{
34+
Console.WriteLine($"A critical error occurred: {ex.Message}");
35+
}
36+
}
37+
}

DemoApp/Files/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
1. assume you are in the directory of this README file.
2+
2. set your Google AI Studio api key to env var `GOOGLE_API_KEY`.
3+
3. run `dotnet run`

DemoApp/Files/google.jpg

35.3 KB
Loading

DemoApp/Files/google.png

4.54 KB
Loading

0 commit comments

Comments
 (0)