Skip to content

Instantly share code, notes, and snippets.

@seesharprun
Created December 5, 2024 22:05
Show Gist options
  • Save seesharprun/fc563ad605bacffba2b47aafa2e49c20 to your computer and use it in GitHub Desktop.
Save seesharprun/fc563ad605bacffba2b47aafa2e49c20 to your computer and use it in GitHub Desktop.
Bulk & Stream support
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.13.1" />
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.46.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
</Project>
{
"id": "0001",
"name": "Sample Person"
}
using Azure.Core;
using Azure.Identity;
using Microsoft.Azure.Cosmos;
using Microsoft.Azure.Cosmos.Fluent;
string endpoint = "<nosql-account-endpoint>";
TokenCredential credential = new DefaultAzureCredential();
CosmosClient client = new CosmosClientBuilder(endpoint, credential)
.WithBulkExecution(true)
.Build();
Container container = client.GetContainer("<database-name>", "<container-name>");
List<FileInfo> files =
[
new FileInfo("first.json"),
new FileInfo("second.json"),
new FileInfo("third.json")
];
List<Task> tasks = new List<Task>();
foreach (FileInfo file in files)
{
tasks.Add(Task.Run(async () =>
{
using Stream stream = File.OpenRead(file.FullName);
ResponseMessage response = await container.UpsertItemStreamAsync(stream, PartitionKey.None);
if (response.IsSuccessStatusCode)
{
Console.WriteLine($"Uploaded {file.Name}");
}
}));
}
await Task.WhenAll(tasks);
{
"id": "0002",
"name": {
"first": "Sample",
"last": "Person"
}
}
{
"id": "0003",
"name": [
"Sample",
"Person"
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment