Skip to content

Commit 01537eb

Browse files
authored
Clean up tools (#506)
Update links and references. Added a simple tool to upload blob files for dev tests on Azure Blobs.
1 parent 237f486 commit 01537eb

16 files changed

+219
-36
lines changed

KernelMemory.sln

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ EndProject
106106
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tools", "tools", "{CA49F1A1-C3FA-4E99-ACB3-D7FF33D47976}"
107107
ProjectSection(SolutionItems) = preProject
108108
tools\ask.sh = tools\ask.sh
109-
tools\create-azure-webapp-publish-artifacts.sh = tools\create-azure-webapp-publish-artifacts.sh
110109
tools\README.md = tools\README.md
111110
tools\run-elasticsearch.sh = tools\run-elasticsearch.sh
112111
tools\run-mongodb-atlas.sh = tools\run-mongodb-atlas.sh
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace />
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<NoWarn>$(NoWarn);KMEXP03;CA2000;CA1303;</NoWarn>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<ProjectReference Include="..\..\extensions\AzureBlobs\AzureBlobs.csproj" />
13+
</ItemGroup>
14+
15+
</Project>

tools/AzureBlobUpload/Program.cs

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
/*
4+
* Usage: dotnet run [file path]
5+
*
6+
* Example:
7+
* dotnet run file4-KM-Readme.pdf
8+
* upload.sh file4-KM-Readme.pdf
9+
*
10+
* For more advanced features, consider using azcopy: https://learn.microsoft.com/azure/storage/common/storage-ref-azcopy
11+
*
12+
* Env vars required:
13+
* - BLOB_CONN_STRING: Azure blob connection string
14+
* - BLOB_CONTAINER: name of the container where files are uploaded
15+
* - BLOB_PATH: name of the virtual folder where files are uploaded
16+
* - DOCUMENT_ID: name of the document folder containing the files
17+
*
18+
* You can store env vars under Properties/launchSettings.json, see the example below:{
19+
{
20+
"profiles": {
21+
"run": {
22+
"environmentVariables": {
23+
"ASPNETCORE_ENVIRONMENT": "Development",
24+
"BLOB_CONN_STRING": "DefaultEndpointsProtocol=https;AccountName=...FOO...;AccountKey=...KEY...;EndpointSuffix=core.windows.net",
25+
"BLOB_CONTAINER": "...NAME...",
26+
"BLOB_PATH": "/",
27+
"DOCUMENT_ID": "...NAME..."
28+
},
29+
"commandName": "Project",
30+
"launchBrowser": false
31+
}
32+
}
33+
}
34+
*/
35+
36+
using Microsoft.KernelMemory;
37+
using Microsoft.KernelMemory.DocumentStorage.AzureBlobs;
38+
39+
if (args.Length == 0 || string.IsNullOrWhiteSpace(args[0]))
40+
{
41+
Console.WriteLine("File path not specified. Provide the path of the file to upload.");
42+
Environment.Exit(-1);
43+
}
44+
45+
var filePath = args[0];
46+
if (Directory.Exists(filePath))
47+
{
48+
Console.WriteLine($"The path provided is a directory, not a file: {filePath}");
49+
Environment.Exit(-2);
50+
}
51+
52+
if (!File.Exists(filePath))
53+
{
54+
Console.WriteLine($"File not found: {filePath}");
55+
Environment.Exit(-3);
56+
}
57+
58+
var fileName = filePath.Replace('\\', '/').Split('/').Last();
59+
60+
Console.WriteLine("Uploading...");
61+
await GetAzureBlobClient().WriteFileAsync(
62+
index: GetIndexName(),
63+
documentId: GetDocumentId(),
64+
fileName: fileName,
65+
streamContent: File.OpenRead(filePath)).ConfigureAwait(false);
66+
67+
Console.WriteLine($"File uploaded: {fileName}");
68+
69+
static AzureBlobsStorage GetAzureBlobClient()
70+
{
71+
var blobConnString = Environment.GetEnvironmentVariable("BLOB_CONN_STRING");
72+
var blobContainer = Environment.GetEnvironmentVariable("BLOB_CONTAINER");
73+
74+
if (string.IsNullOrWhiteSpace(blobConnString))
75+
{
76+
Console.WriteLine("BLOB_CONN_STRING env var not defined. Provide the Azure Blobs connection string.");
77+
Environment.Exit(-4);
78+
}
79+
80+
if (string.IsNullOrWhiteSpace(blobContainer))
81+
{
82+
Console.WriteLine("BLOB_CONTAINER env var not defined. Provide the Azure Blobs container name.");
83+
Environment.Exit(-5);
84+
}
85+
86+
return new AzureBlobsStorage(new AzureBlobsConfig
87+
{
88+
Auth = AzureBlobsConfig.AuthTypes.ConnectionString,
89+
ConnectionString = blobConnString,
90+
Container = blobContainer,
91+
});
92+
}
93+
94+
static string GetIndexName()
95+
{
96+
var indexName = Environment.GetEnvironmentVariable("BLOB_PATH");
97+
if (string.IsNullOrWhiteSpace(indexName))
98+
{
99+
Console.WriteLine("BLOB_PATH env var not defined. Provide the Azure Blobs container virtual folder name.");
100+
Environment.Exit(-6);
101+
}
102+
103+
return indexName;
104+
}
105+
106+
static string GetDocumentId()
107+
{
108+
var id = Environment.GetEnvironmentVariable("DOCUMENT_ID");
109+
if (string.IsNullOrWhiteSpace(id))
110+
{
111+
Console.WriteLine("DOCUMENT_ID env var not defined. Provide the name of the document folder containing the files.");
112+
Environment.Exit(-6);
113+
}
114+
115+
return id;
116+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
launchSettings.json

tools/AzureBlobUpload/build.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
HERE="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)"
6+
cd $HERE
7+
8+
dotnet build -c Release --nologo -v m
170 KB
Binary file not shown.
213 KB
Binary file not shown.

tools/AzureBlobUpload/upload.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
HERE="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)"
6+
cd $HERE
7+
8+
# if file x doesn't exist, then create it
9+
if [ ! -f "bin/Release/net8.0/AzureBlobUpload.dll" ]; then
10+
echo "Building tool..."
11+
dotnet build -c Release --nologo -v q
12+
fi
13+
14+
dotnet run -c Release --no-build $*

tools/create-azure-webapp-publish-artifacts.sh

Lines changed: 0 additions & 10 deletions
This file was deleted.

tools/dev/build.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && cd ../.. && pwd)"
6+
cd $ROOT
7+
8+
clean() {
9+
dotnet clean --nologo -v q -c Release KernelMemory.sln
10+
dotnet clean --nologo -v q -c Debug KernelMemory.sln
11+
}
12+
13+
echo "### Release build"
14+
clean
15+
dotnet build --nologo -v m -c Release KernelMemory.sln
16+
17+
echo "### Debug build"
18+
clean
19+
dotnet build --nologo -v m -c Debug KernelMemory.sln

0 commit comments

Comments
 (0)