Skip to content

Commit fb1cb85

Browse files
matthew29tangcopybara-github
authored andcommitted
feat: Add DownloadAsync overloaded methods for Video and GeneratedVideo
PiperOrigin-RevId: 841835236
1 parent 66e7cd4 commit fb1cb85

4 files changed

Lines changed: 188 additions & 1 deletion

File tree

‎Google.GenAI.E2E.Tests/Files/FilesDownloadTest.cs‎

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,76 @@ public async Task DownloadFileGeminiTest()
153153
await geminiClient.Files.DownloadAsync(file: file);
154154
}
155155

156+
[TestMethod]
157+
public async Task DownloadVideoGeminiTest()
158+
{
159+
var video = new Google.GenAI.Types.Video {
160+
Uri = fileUri
161+
};
162+
var stream = await geminiClient.Files.DownloadAsync(
163+
video: video
164+
);
165+
using (MemoryStream memoryStream = new MemoryStream()) {
166+
await stream.CopyToAsync(memoryStream);
167+
memoryStream.Position = 0;
168+
byte[] bytes = memoryStream.ToArray();
169+
// Replay mode does not return any bytes when downloading a file.
170+
if (!TestServer.IsReplayMode) {
171+
Assert.IsTrue(bytes.Length > 0);
172+
}
173+
}
174+
}
175+
176+
[TestMethod]
177+
public async Task DownloadVideoVertexTest()
178+
{
179+
var video = new Google.GenAI.Types.Video {
180+
Uri = "fileName"
181+
};
182+
var ex = await Assert.ThrowsExceptionAsync<NotSupportedException>(async () =>
183+
{
184+
await vertexClient.Files.DownloadAsync(video: video);
185+
});
186+
187+
StringAssert.Contains(ex.Message, "This method is only supported in the Gemini Developer API client");
188+
}
189+
190+
[TestMethod]
191+
public async Task DownloadGeneratedVideoGeminiTest()
192+
{
193+
var video = new Google.GenAI.Types.Video {
194+
Uri = fileUri
195+
};
196+
var generatedVideo = new Google.GenAI.Types.GeneratedVideo { Video = video };
197+
var stream = await geminiClient.Files.DownloadAsync(
198+
generatedVideo: generatedVideo
199+
);
200+
using (MemoryStream memoryStream = new MemoryStream()) {
201+
await stream.CopyToAsync(memoryStream);
202+
memoryStream.Position = 0;
203+
byte[] bytes = memoryStream.ToArray();
204+
// Replay mode does not return any bytes when downloading a file.
205+
if (!TestServer.IsReplayMode) {
206+
Assert.IsTrue(bytes.Length > 0);
207+
}
208+
}
209+
}
210+
211+
[TestMethod]
212+
public async Task DownloadGeneratedVideoVertexTest()
213+
{
214+
var video = new Google.GenAI.Types.Video {
215+
Uri = "fileName"
216+
};
217+
var generatedVideo = new Google.GenAI.Types.GeneratedVideo { Video = video };
218+
var ex = await Assert.ThrowsExceptionAsync<NotSupportedException>(async () =>
219+
{
220+
await vertexClient.Files.DownloadAsync(generatedVideo: generatedVideo);
221+
});
222+
223+
StringAssert.Contains(ex.Message, "This method is only supported in the Gemini Developer API client");
224+
}
225+
156226
[TestMethod]
157227
public async Task DownloadToFileFileVertexTest()
158228
{
@@ -197,7 +267,6 @@ public async Task DownloadToFileVideoGeminiTest()
197267
await geminiClient.Files.DownloadToFileAsync(video: video, outputPath: "output.mp4");
198268
}
199269

200-
201270
[TestMethod]
202271
public async Task DownloadToFileGeneratedVideoVertexTest()
203272
{
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"recordID": "FilesDownloadTest.DownloadGeneratedVideoGeminiTest",
3+
"interactions": [
4+
{
5+
"request": {
6+
"method": "GET",
7+
"url": "/v1beta/files/p1crkz5nfru4:download?alt=media",
8+
"request": "GET /v1beta/files/p1crkz5nfru4:download?alt=media HTTP/1.1",
9+
"headers": {
10+
"Test-Name": "FilesDownloadTest.DownloadGeneratedVideoGeminiTest"
11+
},
12+
"bodySegments": [
13+
null
14+
],
15+
"previousRequest": "b4d6e60a9b97e7b98c63df9308728c5c88c0b40c398046772c63447b94608b4d",
16+
"serverAddress": "generativelanguage.googleapis.com",
17+
"port": 443,
18+
"protocol": "https"
19+
},
20+
"shaSum": "a46fd7f8428c8998d5a645b73a8d8393a9ba17acb18630a1090178582eae3e20",
21+
"response": {
22+
"statusCode": 200,
23+
"headers": {
24+
"Cache-Control": "private, max-age=0",
25+
"Content-Disposition": "attachment",
26+
"Content-Length": "9864924",
27+
"Content-Type": "video/mp4",
28+
"Date": "Fri, 05 Dec 2025 22:21:26 GMT",
29+
"Expires": "Fri, 05 Dec 2025 22:21:26 GMT",
30+
"Server": "UploadServer",
31+
"Vary": "Origin, X-Origin, Referer",
32+
"X-Goog-Hash": "crc32c=gJcFCw==",
33+
"X-Guploader-Uploadid": "AOCedOG8e6LUTt5BGm31TbV2qWIvYZslpSw6mNcUFfq295PD8hLP9EDFmddKmsbNOG6E-NbTNeOqtEE"
34+
}
35+
}
36+
}
37+
]
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"recordID": "FilesDownloadTest.DownloadVideoGeminiTest",
3+
"interactions": [
4+
{
5+
"request": {
6+
"method": "GET",
7+
"url": "/v1beta/files/p1crkz5nfru4:download?alt=media",
8+
"request": "GET /v1beta/files/p1crkz5nfru4:download?alt=media HTTP/1.1",
9+
"headers": {
10+
"Test-Name": "FilesDownloadTest.DownloadVideoGeminiTest"
11+
},
12+
"bodySegments": [
13+
null
14+
],
15+
"previousRequest": "b4d6e60a9b97e7b98c63df9308728c5c88c0b40c398046772c63447b94608b4d",
16+
"serverAddress": "generativelanguage.googleapis.com",
17+
"port": 443,
18+
"protocol": "https"
19+
},
20+
"shaSum": "6a111d3b0887adb629808c77f9e22365459162aae4401c157893763889a2a590",
21+
"response": {
22+
"statusCode": 200,
23+
"headers": {
24+
"Cache-Control": "private, max-age=0",
25+
"Content-Disposition": "attachment",
26+
"Content-Length": "9864924",
27+
"Content-Type": "video/mp4",
28+
"Date": "Fri, 05 Dec 2025 22:21:26 GMT",
29+
"Expires": "Fri, 05 Dec 2025 22:21:26 GMT",
30+
"Server": "UploadServer",
31+
"Vary": "Origin, X-Origin, Referer",
32+
"X-Goog-Hash": "crc32c=gJcFCw==",
33+
"X-Guploader-Uploadid": "AOCedOF9bCo59It8FHyVCFviBaceCZ-JjwXiQsy1OK2NGI9QZMg__yY9meh1fU8pnqQ1tjvxZXjWUmo"
34+
}
35+
}
36+
}
37+
]
38+
}

‎Google.GenAI/Files.cs‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,48 @@ public async Task<Stream> DownloadAsync(Google.GenAI.Types.File file,
511511
return await DownloadAsync(file.Name, config);
512512
}
513513

514+
/// <summary>
515+
/// Downloads a <see cref="Google.GenAI.Types.Video"/> object and returns it as a <see
516+
/// cref="Stream"/>. Caller is responsible for disposing the returned stream.
517+
/// </summary>
518+
/// <param name="file">The <see cref="Google.GenAI.Types.Video"/> object to download.</param>
519+
/// <param name="config">A <see cref="DownloadFileConfig"/> instance that specifies the optional
520+
/// configurations.</param> <returns>A <see cref="Task{Stream}"/> that represents the
521+
/// asynchronous operation. The task result contains a <see cref="Stream"/> with the file
522+
/// data.</returns>
523+
public async Task<Stream> DownloadAsync(Google.GenAI.Types.Video video,
524+
DownloadFileConfig? config = null) {
525+
if (this._apiClient.VertexAI) {
526+
throw new NotSupportedException(
527+
"This method is only supported in the Gemini Developer API client.");
528+
}
529+
if (string.IsNullOrEmpty(video.Uri))
530+
throw new ArgumentException("Google.GenAI.Types.Video.Uri is required", nameof(video));
531+
532+
return await DownloadAsync(video.Uri, config);
533+
}
534+
535+
/// <summary>
536+
/// Downloads a <see cref="Google.GenAI.Types.GeneratedVideo"/> object and returns it as a <see
537+
/// cref="Stream"/>. Caller is responsible for disposing the returned stream.
538+
/// </summary>
539+
/// <param name="file">The <see cref="Google.GenAI.Types.GeneratedVideo"/> object to
540+
/// download.</param> <param name="config">A <see cref="DownloadFileConfig"/> instance that
541+
/// specifies the optional configurations.</param> <returns>A <see cref="Task{Stream}"/> that
542+
/// represents the asynchronous operation. The task result contains a <see cref="Stream"/> with
543+
/// the file data.</returns>
544+
public async Task<Stream> DownloadAsync(Google.GenAI.Types.GeneratedVideo generatedVideo,
545+
DownloadFileConfig? config = null) {
546+
if (this._apiClient.VertexAI) {
547+
throw new NotSupportedException(
548+
"This method is only supported in the Gemini Developer API client.");
549+
}
550+
if (generatedVideo.Video == null)
551+
throw new ArgumentException("Google.GenAI.Types.Video is empty", nameof(generatedVideo));
552+
553+
return await DownloadAsync(generatedVideo.Video, config);
554+
}
555+
514556
/// <summary>
515557
/// Downloads a file directly to a file path.
516558
/// </summary>

0 commit comments

Comments
 (0)