|
| 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 | +} |
0 commit comments