├── .config └── dotnet-tools.json ├── .gitattributes ├── .github ├── FUNDING.yml └── workflows │ └── build.yml ├── .gitignore ├── LICENSE ├── README.md ├── build.cake ├── build ├── helpers.cake └── records.cake ├── global.json ├── icon ├── LICENSE.md └── blobify.png ├── nuget.config ├── renovate.json └── src ├── .editorconfig ├── Blobify.Tests ├── Blobify.Tests.csproj ├── Constants.cs ├── Extensions │ └── IServiceCollectionExtensions.cs ├── Fixture │ └── BlobifyServiceProviderFixture.cs ├── ModuleInit.cs ├── Resources │ ├── Commands │ │ └── ArchiveCommandTests │ │ │ ├── ExistingFiles │ │ │ └── ExistingFile.json │ │ │ └── NewFiles │ │ │ └── NewFile.json │ ├── Routes.json │ └── Services │ │ └── Storage │ │ └── TokenServiceTests │ │ └── GetAsyncString.json └── Unit │ ├── Commands │ ├── ArchiveCommandTests.ExistingFiles.ExecuteAsync_content=-ExistingFile-.verified.txt │ ├── ArchiveCommandTests.ExistingFiles.ExecuteAsync_content=-ExistingFileWrongHash-.verified.txt │ ├── ArchiveCommandTests.NewFiles.ExecuteAsync.verified.txt │ ├── ArchiveCommandTests.NoFiles.ExecuteAsync.verified.txt │ └── ArchiveCommandTests.cs │ └── Services │ └── Storage │ ├── TokenServiceTests.GetAsync.verified.txt │ ├── TokenServiceTests.HeadAsync.verified.txt │ ├── TokenServiceTests.PutAsync.verified.txt │ └── TokenServiceTests.cs ├── Blobify.sln ├── Blobify ├── Blobify.csproj ├── Commands │ ├── ArchiveCommand.cs │ ├── Settings │ │ └── ArchiveSettings.cs │ └── Validation │ │ ├── ValidatePathAttribute.cs │ │ └── ValidateStringAttribute.cs ├── Program.cs └── Services │ ├── AzureTokenService.cs │ └── Storage │ └── TokenService.cs └── Directory.Packages.props /.config/dotnet-tools.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": 1, 3 | "isRoot": true, 4 | "tools": { 5 | "cake.tool": { 6 | "version": "5.0.0", 7 | "commands": [ 8 | "dotnet-cake" 9 | ], 10 | "rollForward": false 11 | }, 12 | "dpi": { 13 | "version": "2025.6.11.205", 14 | "commands": [ 15 | "dpi" 16 | ], 17 | "rollForward": false 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | 2 | # Verify 3 | *.verified.txt text eol=lf working-tree-encoding=UTF-8 -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: devlead 2 | -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: Build 2 | on: 3 | workflow_dispatch: 4 | pull_request: 5 | push: 6 | branches: 7 | - main 8 | - develop 9 | - hotfix/* 10 | 11 | jobs: 12 | build: 13 | name: Build 14 | runs-on: ${{ matrix.os }} 15 | permissions: 16 | contents: write 17 | id-token: write 18 | packages: write 19 | strategy: 20 | fail-fast: false 21 | matrix: 22 | os: [windows-latest, ubuntu-latest, macos-latest] 23 | steps: 24 | - name: Get the sources 25 | uses: actions/checkout@v4 26 | with: 27 | fetch-depth: 0 28 | 29 | - name: Install .NET SDK 8.0.x 30 | uses: actions/setup-dotnet@v4 31 | with: 32 | dotnet-version: | 33 | 8.0.x 34 | 35 | - name: Install .NET SDK 36 | uses: actions/setup-dotnet@v4 37 | with: 38 | global-json-file: global.json 39 | 40 | - name: Run Cake script 41 | uses: cake-build/cake-action@v3 42 | env: 43 | NuGetReportSettings_SharedKey: ${{ secrets.NUGETREPORTSETTINGS_SHAREDKEY }} 44 | NuGetReportSettings_WorkspaceId: ${{ secrets.NUGETREPORTSETTINGS_WORKSPACEID }} 45 | GH_PACKAGES_NUGET_SOURCE: ${{ secrets.GH_PACKAGES_NUGET_SOURCE }} 46 | NUGET_SOURCE: ${{ secrets.NUGET_SOURCE }} 47 | NUGET_APIKEY: ${{ secrets.NUGET_APIKEY }} 48 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 49 | AZURE_TENANT_ID: ${{ secrets.AZURE_TENANT_ID }} 50 | AZURE_CLIENT_ID: ${{ secrets.AZURE_CLIENT_ID }} 51 | AZURE_CLIENT_SECRET: ${{ secrets.AZURE_CLIENT_SECRET }} 52 | AZURE_AUTHORITY_HOST: "https://login.microsoftonline.com" 53 | AZURE_STORAGE_ACCOUNT: ${{ secrets.AZURE_STORAGE_ACCOUNT }} 54 | AZURE_STORAGE_ACCOUNT_CONTAINER: ${{ secrets.AZURE_STORAGE_ACCOUNT_CONTAINER }}${{ matrix.os }} 55 | with: 56 | cake-version: tool-manifest 57 | target: GitHub-Actions 58 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Aa][Rr][Mm]/ 27 | [Aa][Rr][Mm]64/ 28 | bld/ 29 | [Bb]in/ 30 | [Oo]bj/ 31 | [Ll]og/ 32 | [Ll]ogs/ 33 | 34 | # Visual Studio 2015/2017 cache/options directory 35 | .vs/ 36 | # Uncomment if you have tasks that create the project's static files in wwwroot 37 | #wwwroot/ 38 | 39 | # Visual Studio 2017 auto generated files 40 | Generated\ Files/ 41 | 42 | # MSTest test Results 43 | [Tt]est[Rr]esult*/ 44 | [Bb]uild[Ll]og.* 45 | 46 | # NUnit 47 | *.VisualState.xml 48 | TestResult.xml 49 | nunit-*.xml 50 | 51 | # Build Results of an ATL Project 52 | [Dd]ebugPS/ 53 | [Rr]eleasePS/ 54 | dlldata.c 55 | 56 | # Benchmark Results 57 | BenchmarkDotNet.Artifacts/ 58 | 59 | # .NET Core 60 | project.lock.json 61 | project.fragment.lock.json 62 | artifacts/ 63 | 64 | # StyleCop 65 | StyleCopReport.xml 66 | 67 | # Files built by Visual Studio 68 | *_i.c 69 | *_p.c 70 | *_h.h 71 | *.ilk 72 | *.meta 73 | *.obj 74 | *.iobj 75 | *.pch 76 | *.pdb 77 | *.ipdb 78 | *.pgc 79 | *.pgd 80 | *.rsp 81 | *.sbr 82 | *.tlb 83 | *.tli 84 | *.tlh 85 | *.tmp 86 | *.tmp_proj 87 | *_wpftmp.csproj 88 | *.log 89 | *.vspscc 90 | *.vssscc 91 | .builds 92 | *.pidb 93 | *.svclog 94 | *.scc 95 | 96 | # Chutzpah Test files 97 | _Chutzpah* 98 | 99 | # Visual C++ cache files 100 | ipch/ 101 | *.aps 102 | *.ncb 103 | *.opendb 104 | *.opensdf 105 | *.sdf 106 | *.cachefile 107 | *.VC.db 108 | *.VC.VC.opendb 109 | 110 | # Visual Studio profiler 111 | *.psess 112 | *.vsp 113 | *.vspx 114 | *.sap 115 | 116 | # Visual Studio Trace Files 117 | *.e2e 118 | 119 | # TFS 2012 Local Workspace 120 | $tf/ 121 | 122 | # Guidance Automation Toolkit 123 | *.gpState 124 | 125 | # ReSharper is a .NET coding add-in 126 | _ReSharper*/ 127 | *.[Rr]e[Ss]harper 128 | *.DotSettings.user 129 | 130 | # TeamCity is a build add-in 131 | _TeamCity* 132 | 133 | # DotCover is a Code Coverage Tool 134 | *.dotCover 135 | 136 | # AxoCover is a Code Coverage Tool 137 | .axoCover/* 138 | !.axoCover/settings.json 139 | 140 | # Visual Studio code coverage results 141 | *.coverage 142 | *.coveragexml 143 | 144 | # NCrunch 145 | _NCrunch_* 146 | .*crunch*.local.xml 147 | nCrunchTemp_* 148 | 149 | # MightyMoose 150 | *.mm.* 151 | AutoTest.Net/ 152 | 153 | # Web workbench (sass) 154 | .sass-cache/ 155 | 156 | # Installshield output folder 157 | [Ee]xpress/ 158 | 159 | # DocProject is a documentation generator add-in 160 | DocProject/buildhelp/ 161 | DocProject/Help/*.HxT 162 | DocProject/Help/*.HxC 163 | DocProject/Help/*.hhc 164 | DocProject/Help/*.hhk 165 | DocProject/Help/*.hhp 166 | DocProject/Help/Html2 167 | DocProject/Help/html 168 | 169 | # Click-Once directory 170 | publish/ 171 | 172 | # Publish Web Output 173 | *.[Pp]ublish.xml 174 | *.azurePubxml 175 | # Note: Comment the next line if you want to checkin your web deploy settings, 176 | # but database connection strings (with potential passwords) will be unencrypted 177 | *.pubxml 178 | *.publishproj 179 | 180 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 181 | # checkin your Azure Web App publish settings, but sensitive information contained 182 | # in these scripts will be unencrypted 183 | PublishScripts/ 184 | 185 | # NuGet Packages 186 | *.nupkg 187 | # NuGet Symbol Packages 188 | *.snupkg 189 | # The packages folder can be ignored because of Package Restore 190 | **/[Pp]ackages/* 191 | # except build/, which is used as an MSBuild target. 192 | !**/[Pp]ackages/build/ 193 | # Uncomment if necessary however generally it will be regenerated when needed 194 | #!**/[Pp]ackages/repositories.config 195 | # NuGet v3's project.json files produces more ignorable files 196 | *.nuget.props 197 | *.nuget.targets 198 | 199 | # Microsoft Azure Build Output 200 | csx/ 201 | *.build.csdef 202 | 203 | # Microsoft Azure Emulator 204 | ecf/ 205 | rcf/ 206 | 207 | # Windows Store app package directories and files 208 | AppPackages/ 209 | BundleArtifacts/ 210 | Package.StoreAssociation.xml 211 | _pkginfo.txt 212 | *.appx 213 | *.appxbundle 214 | *.appxupload 215 | 216 | # Visual Studio cache files 217 | # files ending in .cache can be ignored 218 | *.[Cc]ache 219 | # but keep track of directories ending in .cache 220 | !?*.[Cc]ache/ 221 | 222 | # Others 223 | ClientBin/ 224 | ~$* 225 | *~ 226 | *.dbmdl 227 | *.dbproj.schemaview 228 | *.jfm 229 | *.pfx 230 | *.publishsettings 231 | orleans.codegen.cs 232 | 233 | # Including strong name files can present a security risk 234 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 235 | #*.snk 236 | 237 | # Since there are multiple workflows, uncomment next line to ignore bower_components 238 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 239 | #bower_components/ 240 | 241 | # RIA/Silverlight projects 242 | Generated_Code/ 243 | 244 | # Backup & report files from converting an old project file 245 | # to a newer Visual Studio version. Backup files are not needed, 246 | # because we have git ;-) 247 | _UpgradeReport_Files/ 248 | Backup*/ 249 | UpgradeLog*.XML 250 | UpgradeLog*.htm 251 | ServiceFabricBackup/ 252 | *.rptproj.bak 253 | 254 | # SQL Server files 255 | *.mdf 256 | *.ldf 257 | *.ndf 258 | 259 | # Business Intelligence projects 260 | *.rdl.data 261 | *.bim.layout 262 | *.bim_*.settings 263 | *.rptproj.rsuser 264 | *- [Bb]ackup.rdl 265 | *- [Bb]ackup ([0-9]).rdl 266 | *- [Bb]ackup ([0-9][0-9]).rdl 267 | 268 | # Microsoft Fakes 269 | FakesAssemblies/ 270 | 271 | # GhostDoc plugin setting file 272 | *.GhostDoc.xml 273 | 274 | # Node.js Tools for Visual Studio 275 | .ntvs_analysis.dat 276 | node_modules/ 277 | 278 | # Visual Studio 6 build log 279 | *.plg 280 | 281 | # Visual Studio 6 workspace options file 282 | *.opt 283 | 284 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 285 | *.vbw 286 | 287 | # Visual Studio LightSwitch build output 288 | **/*.HTMLClient/GeneratedArtifacts 289 | **/*.DesktopClient/GeneratedArtifacts 290 | **/*.DesktopClient/ModelManifest.xml 291 | **/*.Server/GeneratedArtifacts 292 | **/*.Server/ModelManifest.xml 293 | _Pvt_Extensions 294 | 295 | # Paket dependency manager 296 | .paket/paket.exe 297 | paket-files/ 298 | 299 | # FAKE - F# Make 300 | .fake/ 301 | 302 | # CodeRush personal settings 303 | .cr/personal 304 | 305 | # Python Tools for Visual Studio (PTVS) 306 | __pycache__/ 307 | *.pyc 308 | 309 | # Cake 310 | tools/ 311 | 312 | # Tabs Studio 313 | *.tss 314 | 315 | # Telerik's JustMock configuration file 316 | *.jmconfig 317 | 318 | # BizTalk build output 319 | *.btp.cs 320 | *.btm.cs 321 | *.odx.cs 322 | *.xsd.cs 323 | 324 | # OpenCover UI analysis results 325 | OpenCover/ 326 | 327 | # Azure Stream Analytics local run output 328 | ASALocalRun/ 329 | 330 | # MSBuild Binary and Structured Log 331 | *.binlog 332 | 333 | # NVidia Nsight GPU debugger configuration file 334 | *.nvuser 335 | 336 | # MFractors (Xamarin productivity tool) working folder 337 | .mfractor/ 338 | 339 | # Local History for Visual Studio 340 | .localhistory/ 341 | 342 | # BeatPulse healthcheck temp database 343 | healthchecksdb 344 | 345 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 346 | MigrationBackup/ 347 | 348 | # Ionide (cross platform F# VS Code tools) working folder 349 | .ionide/ 350 | 351 | [Aa]rtifacts/ 352 | 353 | launchSettings.json 354 | 355 | !src/**/[Aa][Rr][Mm]/ 356 | 357 | src/ARI.TestWeb/theme/ 358 | src/ARI.TestWeb/cache/ 359 | src/ARI.TestWeb/output/ 360 | 361 | # Rider/ReSharper temp 362 | src/.idea/ 363 | 364 | 365 | # Verify 366 | *.received.* 367 | *.received/ -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Mattias Karlsson 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Blobify 2 | 3 | Blobify is a .NET Global tool that archives (moves) files from a local folder to Azure Blob Storage container. 4 | 5 | ## Obtain 6 | 7 | ```bash 8 | dotnet tool install -g Blobify 9 | ``` 10 | 11 | ## Usage 12 | 13 | blobify archive [OPTIONS] 14 | 15 | ### Example 16 | 17 | blobify archive inputpath storageaccountname storagecontainer 18 | 19 | ### Arguments 20 | 21 | Input path 22 | Azure Storage Account Name 23 | Azure Storage Account Container Name 24 | 25 | ### Options 26 | -h, --help Prints help information 27 | --azure-tenant-id Azure Tentant ID to sign into 28 | --file-pattern Local file pattern to match 29 | 30 | 31 | ## Authentication 32 | 33 | By default it'll try authenticate using the [DefaultAzureCredential](https://learn.microsoft.com/en-us/dotnet/api/azure.identity.defaultazurecredential?view=azure-dotnet) which tries to authorize in the following order based on your environment. 34 | 35 | 1. [EnvironmentCredential](https://learn.microsoft.com/en-us/dotnet/api/azure.identity.environmentcredential?view=azure-dotnet) 36 | 1. [WorkloadIdentityCredential](https://learn.microsoft.com/en-us/dotnet/api/azure.identity.workloadidentitycredential?view=azure-dotnet) 37 | 1. [ManagedIdentityCredential](https://learn.microsoft.com/en-us/dotnet/api/azure.identity.managedidentitycredential?view=azure-dotnet) 38 | 1. [SharedTokenCacheCredential](https://learn.microsoft.com/en-us/dotnet/api/azure.identity.sharedtokencachecredential?view=azure-dotnet) 39 | 1. [VisualStudioCredential](https://learn.microsoft.com/en-us/dotnet/api/azure.identity.visualstudiocredential?view=azure-dotnet) 40 | 1. [VisualStudioCodeCredential](https://learn.microsoft.com/en-us/dotnet/api/azure.identity.visualstudiocodecredential?view=azure-dotnet) 41 | 1. [AzureCliCredential](https://learn.microsoft.com/en-us/dotnet/api/azure.identity.azureclicredential?view=azure-dotnet) 42 | 1. [AzurePowerShellCredential](https://learn.microsoft.com/en-us/dotnet/api/azure.identity.azurepowershellcredential?view=azure-dotnet) 43 | 1. [AzureDeveloperCliCredential](https://learn.microsoft.com/en-us/dotnet/api/azure.identity.azuredeveloperclicredential?view=azure-dotnet) 44 | 1. [InteractiveBrowserCredential](https://learn.microsoft.com/en-us/dotnet/api/azure.identity.interactivebrowsercredential?view=azure-dotnet) 45 | 46 | ### Using EnvironmentCredential 47 | 48 | 1. Set the environment variable `AZURE_TENANT_ID` to the tenant ID (found in the `App Registration` overview for your app). 49 | 1. Set the environment variable `AZURE_CLIENT_ID` to the client ID (found in the `App Registration` overview for your app). 50 | 1. Set the environment variable `AZURE_CLIENT_SECRET` to the secret noted earlier. 51 | 1. Set the environment variable `AZURE_AUTHORITY_HOST` to `https://login.microsoftonline.com/`. 52 | 53 | ## Tool flow 54 | 55 | ```mermaid 56 | flowchart TD 57 | ls[List files in source path] 58 | exists[Verifies if blob file already exists] 59 | upload[Upload file] 60 | verify[Verifies MD5 hash] 61 | delete[Delete file] 62 | skip[Skip file] 63 | ls --> exists 64 | exists --Found--> verify 65 | exists --Not Found--> upload 66 | upload --> verify 67 | verify --Matches--> delete 68 | verify --Not Matches--> skip 69 | ``` -------------------------------------------------------------------------------- /build.cake: -------------------------------------------------------------------------------- 1 | #tool dotnet:?package=GitVersion.Tool&version=6.3.0 2 | #load "build/records.cake" 3 | #load "build/helpers.cake" 4 | 5 | /***************************** 6 | * Setup 7 | *****************************/ 8 | Setup( 9 | static context => { 10 | var assertedVersions = context.GitVersion(new GitVersionSettings 11 | { 12 | OutputType = GitVersionOutput.Json 13 | }); 14 | 15 | var branchName = assertedVersions.BranchName; 16 | var isMainBranch = StringComparer.OrdinalIgnoreCase.Equals("main", branchName); 17 | 18 | var gh = context.GitHubActions(); 19 | var buildDate = DateTime.UtcNow; 20 | var runNumber = gh.IsRunningOnGitHubActions 21 | ? gh.Environment.Workflow.RunNumber 22 | : (short)((buildDate - buildDate.Date).TotalSeconds/3); 23 | 24 | var version = FormattableString 25 | .Invariant($"{buildDate:yyyy.M.d}.{runNumber}"); 26 | 27 | context.Information("Building version {0} (Branch: {1}, IsMain: {2})", 28 | version, 29 | branchName, 30 | isMainBranch); 31 | 32 | var artifactsPath = context 33 | .MakeAbsolute(context.Directory("./artifacts")); 34 | 35 | var projectRoot = context 36 | .MakeAbsolute(context.Directory("./src")); 37 | 38 | var projectPath = projectRoot.CombineWithFilePath("Blobify/Blobify.csproj"); 39 | 40 | return new BuildData( 41 | version, 42 | isMainBranch, 43 | !context.IsRunningOnWindows(), 44 | context.BuildSystem().IsLocalBuild, 45 | projectRoot, 46 | projectPath, 47 | new DotNetMSBuildSettings() 48 | .SetConfiguration("Release") 49 | .SetVersion(version) 50 | .WithProperty("Copyright", $"Mattias Karlsson © {DateTime.UtcNow.Year}") 51 | .WithProperty("Authors", "devlead") 52 | .WithProperty("Company", "devlead") 53 | .WithProperty("PackageLicenseExpression", "MIT") 54 | .WithProperty("PackageTags", "tool;blob;storage;azure") 55 | .WithProperty("PackageDescription", ".NET Tool that archives local files to Azure Blob Storage") 56 | .WithProperty("RepositoryUrl", "https://github.com/devlead/Blobify.git") 57 | .WithProperty("ContinuousIntegrationBuild", gh.IsRunningOnGitHubActions ? "true" : "false") 58 | .WithProperty("EmbedUntrackedSources", "true"), 59 | artifactsPath, 60 | artifactsPath.Combine(version) 61 | ); 62 | } 63 | ); 64 | 65 | /***************************** 66 | * Tasks 67 | *****************************/ 68 | Task("Clean") 69 | .Does( 70 | static (context, data) => context.CleanDirectories(data.DirectoryPathsToClean) 71 | ) 72 | .Then("Restore") 73 | .Does( 74 | static (context, data) => context.DotNetRestore( 75 | data.ProjectRoot.FullPath, 76 | new DotNetRestoreSettings { 77 | MSBuildSettings = data.MSBuildSettings 78 | } 79 | ) 80 | ) 81 | .Then("DPI") 82 | .Does( 83 | static (context, data) => context.DotNetTool( 84 | "tool", 85 | new DotNetToolSettings { 86 | ArgumentCustomization = args => args 87 | .Append("run") 88 | .Append("dpi") 89 | .Append("nuget") 90 | .Append("--silent") 91 | .AppendSwitchQuoted("--output", "table") 92 | .Append( 93 | ( 94 | !string.IsNullOrWhiteSpace(context.EnvironmentVariable("NuGetReportSettings_SharedKey")) 95 | && 96 | !string.IsNullOrWhiteSpace(context.EnvironmentVariable("NuGetReportSettings_WorkspaceId")) 97 | ) 98 | ? "report" 99 | : "analyze" 100 | ) 101 | .AppendSwitchQuoted("--buildversion", data.Version) 102 | } 103 | ) 104 | ) 105 | .Then("Build") 106 | .Does( 107 | static (context, data) => context.DotNetBuild( 108 | data.ProjectRoot.FullPath, 109 | new DotNetBuildSettings { 110 | NoRestore = true, 111 | MSBuildSettings = data.MSBuildSettings 112 | } 113 | ) 114 | ) 115 | .Then("Test") 116 | .Does( 117 | static (context, data) => context.DotNetTest( 118 | data.ProjectRoot.FullPath, 119 | new DotNetTestSettings { 120 | NoBuild = true, 121 | NoRestore = true, 122 | MSBuildSettings = data.MSBuildSettings 123 | } 124 | ) 125 | ) 126 | .Then("Pack") 127 | .Does( 128 | static (context, data) => context.DotNetPack( 129 | data.ProjectPath.FullPath, 130 | new DotNetPackSettings { 131 | NoBuild = true, 132 | NoRestore = true, 133 | OutputDirectory = data.NuGetOutputPath, 134 | MSBuildSettings = data.MSBuildSettings 135 | } 136 | ) 137 | ) 138 | .Then("Upload-Artifacts") 139 | .WithCriteria(BuildSystem.IsRunningOnGitHubActions, nameof(BuildSystem.IsRunningOnGitHubActions)) 140 | .Does( 141 | static (context, data) => context 142 | .GitHubActions() is var gh && gh != null 143 | ? gh.Commands 144 | .UploadArtifact(data.ArtifactsPath, $"Artifact_{gh.Environment.Runner.ImageOS ?? gh.Environment.Runner.OS}_{context.Environment.Runtime.BuiltFramework.Identifier}_{context.Environment.Runtime.BuiltFramework.Version}") 145 | : throw new Exception("GitHubActions not available") 146 | ) 147 | .Then("Integration-Tests-Tool-Manifest") 148 | .Does( 149 | static (context, data) => context.DotNetTool( 150 | "new", 151 | new DotNetToolSettings { 152 | ArgumentCustomization = args => args 153 | .Append("tool-manifest"), 154 | WorkingDirectory = data.IntegrationTestPath 155 | } 156 | ) 157 | ) 158 | .Then("Integration-Tests-Tool-Install") 159 | .Does( 160 | static (context, data) => context.DotNetTool( 161 | "tool", 162 | new DotNetToolSettings { 163 | ArgumentCustomization = args => args 164 | .Append("install") 165 | .AppendSwitchQuoted("--add-source", data.NuGetOutputPath.FullPath) 166 | .AppendSwitchQuoted("--version", data.Version) 167 | .Append("Blobify"), 168 | WorkingDirectory = data.IntegrationTestPath 169 | } 170 | ) 171 | ) 172 | .Then("Integration-Tests-Tool") 173 | .WithCriteria((context, data) => data.ShouldRunIntegrationTests(), "ShouldRunIntegrationTests") 174 | .Does( 175 | static (context, data) => context.DotNetTool( 176 | "tool", 177 | new DotNetToolSettings { 178 | ArgumentCustomization = args => args 179 | .Append("run") 180 | .Append("--") 181 | .Append("blobify") 182 | .Append("archive") 183 | .AppendQuoted(data.ArtifactsPath.FullPath) 184 | .AppendQuotedSecret(data.AzureStorageAccount) 185 | .AppendQuotedSecret(data.AzureStorageAccountContainer) 186 | .AppendSwitchQuoted("--file-pattern", "**/dotnet-tools.json"), 187 | WorkingDirectory = data.IntegrationTestPath 188 | } 189 | ) 190 | ) 191 | .Then("Integration-Tests") 192 | .Default() 193 | .Then("Push-GitHub-Packages") 194 | .WithCriteria( (context, data) => data.ShouldPushGitHubPackages()) 195 | .DoesForEach( 196 | static (data, context) 197 | => context.GetFiles(data.NuGetOutputPath.FullPath + "/*.nupkg"), 198 | static (data, item, context) 199 | => context.DotNetNuGetPush( 200 | item.FullPath, 201 | new DotNetNuGetPushSettings 202 | { 203 | Source = data.GitHubNuGetSource, 204 | ApiKey = data.GitHubNuGetApiKey 205 | } 206 | ) 207 | ) 208 | .Then("Push-NuGet-Packages") 209 | .WithCriteria( (context, data) => data.ShouldPushNuGetPackages()) 210 | .DoesForEach( 211 | static (data, context) 212 | => context.GetFiles(data.NuGetOutputPath.FullPath + "/*.nupkg"), 213 | static (data, item, context) 214 | => context.DotNetNuGetPush( 215 | item.FullPath, 216 | new DotNetNuGetPushSettings 217 | { 218 | Source = data.NuGetSource, 219 | ApiKey = data.NuGetApiKey 220 | } 221 | ) 222 | ) 223 | .Then("Create-GitHub-Release") 224 | .WithCriteria( (context, data) => data.ShouldPushNuGetPackages()) 225 | .Does( 226 | static (context, data) => context 227 | .Command( 228 | new CommandSettings { 229 | ToolName = "GitHub CLI", 230 | ToolExecutableNames = new []{ "gh.exe", "gh" }, 231 | EnvironmentVariables = { { "GH_TOKEN", data.GitHubNuGetApiKey } } 232 | }, 233 | new ProcessArgumentBuilder() 234 | .Append("release") 235 | .Append("create") 236 | .Append(data.Version) 237 | .AppendSwitchQuoted("--title", data.Version) 238 | .Append("--generate-notes") 239 | .Append(string.Join( 240 | ' ', 241 | context 242 | .GetFiles(data.NuGetOutputPath.FullPath + "/*.nupkg") 243 | .Select(path => path.FullPath.Quote()) 244 | )) 245 | 246 | ) 247 | ) 248 | .Then("GitHub-Actions") 249 | .Run(); 250 | -------------------------------------------------------------------------------- /build/helpers.cake: -------------------------------------------------------------------------------- 1 | #addin "nuget:?package=xunit.assert&version=2.9.3" 2 | #load "records.cake" 3 | 4 | // Usings 5 | using Xunit; 6 | 7 | /***************************** 8 | * Helpers 9 | *****************************/ 10 | 11 | private static ExtensionHelper extensionHelper; 12 | extensionHelper = new ExtensionHelper(Task, () => RunTarget(Argument("target", "Default"))); 13 | public static CakeTaskBuilder Then(this CakeTaskBuilder cakeTaskBuilder, string name) 14 | => extensionHelper 15 | .TaskCreate(name) 16 | .IsDependentOn(cakeTaskBuilder); 17 | 18 | 19 | public static CakeReport Run(this CakeTaskBuilder cakeTaskBuilder) 20 | => extensionHelper.Run(); 21 | 22 | public static CakeTaskBuilder Default(this CakeTaskBuilder cakeTaskBuilder) 23 | { 24 | extensionHelper 25 | .TaskCreate("Default") 26 | .IsDependentOn(cakeTaskBuilder); 27 | return cakeTaskBuilder; 28 | } 29 | 30 | if (BuildSystem.GitHubActions.IsRunningOnGitHubActions) 31 | { 32 | TaskSetup(context=> System.Console.WriteLine($"::group::{context.Task.Name.Quote()}")); 33 | TaskTeardown(context=>System.Console.WriteLine("::endgroup::")); 34 | } 35 | 36 | public class FilePathJsonConverter : PathJsonConverter 37 | { 38 | protected override FilePath ConvertFromString(string value) => FilePath.FromString(value); 39 | } 40 | 41 | public abstract class PathJsonConverter : System.Text.Json.Serialization.JsonConverter where TPath : Cake.Core.IO.Path 42 | { 43 | public override TPath Read(ref System.Text.Json.Utf8JsonReader reader, Type typeToConvert, System.Text.Json.JsonSerializerOptions options) 44 | { 45 | var value = reader.GetString(); 46 | 47 | return value is null ? null : ConvertFromString(value); 48 | } 49 | 50 | public override void Write(System.Text.Json.Utf8JsonWriter writer, TPath value, System.Text.Json.JsonSerializerOptions options) 51 | { 52 | writer.WriteStringValue(value.FullPath); 53 | } 54 | 55 | protected abstract TPath ConvertFromString(string value); 56 | } -------------------------------------------------------------------------------- /build/records.cake: -------------------------------------------------------------------------------- 1 | #load "helpers.cake" 2 | using System.Text.Json.Serialization; 3 | 4 | /***************************** 5 | * Records 6 | *****************************/ 7 | public record BuildData( 8 | string Version, 9 | bool IsMainBranch, 10 | bool ShouldNotPublish, 11 | bool IsLocalBuild, 12 | DirectoryPath ProjectRoot, 13 | FilePath ProjectPath, 14 | DotNetMSBuildSettings MSBuildSettings, 15 | DirectoryPath ArtifactsPath, 16 | DirectoryPath OutputPath 17 | ) 18 | { 19 | private const string IntegrationTest = "integrationtest", 20 | Output = "output"; 21 | public DirectoryPath NuGetOutputPath { get; } = OutputPath.Combine("nuget"); 22 | public DirectoryPath BinaryOutputPath { get; } = OutputPath.Combine("bin"); 23 | public DirectoryPath IntegrationTestPath { get; } = OutputPath.Combine(IntegrationTest); 24 | 25 | public string GitHubNuGetSource { get; } = System.Environment.GetEnvironmentVariable("GH_PACKAGES_NUGET_SOURCE"); 26 | public string GitHubNuGetApiKey { get; } = System.Environment.GetEnvironmentVariable("GITHUB_TOKEN"); 27 | 28 | public bool ShouldPushGitHubPackages() => !ShouldNotPublish 29 | && !string.IsNullOrWhiteSpace(GitHubNuGetSource) 30 | && !string.IsNullOrWhiteSpace(GitHubNuGetApiKey); 31 | 32 | public string NuGetSource { get; } = System.Environment.GetEnvironmentVariable("NUGET_SOURCE"); 33 | public string NuGetApiKey { get; } = System.Environment.GetEnvironmentVariable("NUGET_APIKEY"); 34 | public bool ShouldPushNuGetPackages() => IsMainBranch && 35 | !ShouldNotPublish && 36 | !string.IsNullOrWhiteSpace(NuGetSource) && 37 | !string.IsNullOrWhiteSpace(NuGetApiKey); 38 | 39 | public ICollection DirectoryPathsToClean = new []{ 40 | ArtifactsPath, 41 | OutputPath, 42 | OutputPath.Combine(IntegrationTest) 43 | }; 44 | 45 | public AzureCredentials AzureCredentials { get; } = new AzureCredentials( 46 | System.Environment.GetEnvironmentVariable("AZURE_TENANT_ID"), 47 | System.Environment.GetEnvironmentVariable("AZURE_CLIENT_ID"), 48 | System.Environment.GetEnvironmentVariable("AZURE_CLIENT_SECRET"), 49 | System.Environment.GetEnvironmentVariable("AZURE_AUTHORITY_HOST") 50 | ); 51 | 52 | public string AzureStorageAccount { get; } = System.Environment.GetEnvironmentVariable("AZURE_STORAGE_ACCOUNT"); 53 | 54 | public string AzureStorageAccountContainer { get; } = System.Environment.GetEnvironmentVariable("AZURE_STORAGE_ACCOUNT_CONTAINER"); 55 | 56 | public bool ShouldRunIntegrationTests() => !string.IsNullOrWhiteSpace(AzureStorageAccount) && 57 | !string.IsNullOrWhiteSpace(AzureStorageAccountContainer) && 58 | ( 59 | AzureCredentials.AzureCredentialsSpecified || 60 | IsLocalBuild 61 | ); 62 | } 63 | 64 | public record AzureCredentials( 65 | string TenantId, 66 | string ClientId, 67 | string ClientSecret, 68 | string AuthorityHost = "login.microsoftonline.com" 69 | ) 70 | { 71 | public bool AzureCredentialsSpecified { get; } = !string.IsNullOrWhiteSpace(TenantId) && 72 | !string.IsNullOrWhiteSpace(ClientId) && 73 | !string.IsNullOrWhiteSpace(ClientSecret) && 74 | !string.IsNullOrWhiteSpace(AuthorityHost); 75 | } 76 | private record ExtensionHelper(Func TaskCreate, Func Run); 77 | -------------------------------------------------------------------------------- /global.json: -------------------------------------------------------------------------------- 1 | { 2 | "sdk": { 3 | "rollForward": "latestMinor", 4 | "version": "9.0.301" 5 | } 6 | } -------------------------------------------------------------------------------- /icon/LICENSE.md: -------------------------------------------------------------------------------- 1 | # Icon License 2 | 3 | *Package icon is not MIT licensed* 4 | 5 | May 28, 2024 6 | 7 | ## ROYALTY–FREE LICENSE 8 | 9 | Image: “folder upload Icon #4330395” 10 | 11 | Purchased from Noun Project 12 | 13 | ## LICENSEE 14 | 15 | Mattias Karlsson 16 | 17 | ## LICENSOR 18 | 19 | The Noun Project, Inc. 20 | 21 | ## On behalf of 22 | [Kmg Design](https://thenounproject.com/creator/kmgdesignid/) 23 | 24 | This license grants its holder perpetual, non-exclusive, worldwide rights to unlimited use of the licensed 25 | image, as covered by Noun Project's Terms of Use ([thenounproject.com/legal](https://thenounproject.com/legal/)). 26 | -------------------------------------------------------------------------------- /icon/blobify.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devlead/Blobify/051f956943ef8c61f99665ae0e4397de07156329/icon/blobify.png -------------------------------------------------------------------------------- /nuget.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://docs.renovatebot.com/renovate-schema.json" 3 | } 4 | -------------------------------------------------------------------------------- /src/.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | 4 | # Verify 5 | [*.{received,verified}.{txt}] 6 | charset = "utf-8-bom" 7 | end_of_line = lf 8 | indent_size = unset 9 | indent_style = unset 10 | insert_final_newline = false 11 | tab_width = unset 12 | trim_trailing_whitespace = false -------------------------------------------------------------------------------- /src/Blobify.Tests/Blobify.Tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net8.0;net9.0 5 | enable 6 | enable 7 | true 8 | false 9 | true 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /src/Blobify.Tests/Constants.cs: -------------------------------------------------------------------------------- 1 | namespace Blobify.Tests; 2 | 3 | public class Constants 4 | { 5 | public static class Request 6 | { 7 | public static readonly Uri BaseUri = new ("https://blobify.tests/", UriKind.Absolute); 8 | } 9 | 10 | public static class Tenant 11 | { 12 | public const string Id = "daea2e9b-847b-4c93-850d-2aa6f2d7af33"; 13 | } 14 | } -------------------------------------------------------------------------------- /src/Blobify.Tests/Extensions/IServiceCollectionExtensions.cs: -------------------------------------------------------------------------------- 1 | using Cake.Core.Configuration; 2 | using Cake.Core.Diagnostics; 3 | using Cake.Core.IO; 4 | using Cake.Core; 5 | 6 | namespace Blobify.Tests.Extensions; 7 | 8 | public static class IServiceCollectionExtensions 9 | { 10 | public static IServiceCollection AddCakeFakes( 11 | this IServiceCollection services, 12 | Action? configureFileSystem = null 13 | ) 14 | { 15 | var configuration = new FakeConfiguration(); 16 | 17 | var environment = FakeEnvironment.CreateUnixEnvironment(); 18 | 19 | var fileSystem = new FakeFileSystem(environment); 20 | configureFileSystem?.Invoke(fileSystem); 21 | 22 | var globber = new Globber(fileSystem, environment); 23 | 24 | var log = new FakeLog(); 25 | 26 | var Context = Substitute.For(); 27 | Context.Configuration.Returns(configuration); 28 | Context.Environment.Returns(environment); 29 | Context.FileSystem.Returns(fileSystem); 30 | Context.Globber.Returns(globber); 31 | Context.Log.Returns(log); 32 | 33 | return services.AddSingleton(configuration) 34 | .AddSingleton(environment) 35 | .AddSingleton(fileSystem) 36 | .AddSingleton(fileSystem) 37 | .AddSingleton(globber) 38 | .AddSingleton(log) 39 | .AddSingleton(environment.Runtime) 40 | .AddSingleton(Context); 41 | } 42 | } -------------------------------------------------------------------------------- /src/Blobify.Tests/Fixture/BlobifyServiceProviderFixture.cs: -------------------------------------------------------------------------------- 1 | using Blobify.Services.Storage; 2 | using Blobify.Services; 3 | using Azure.Core; 4 | using Blobify.Tests.Extensions; 5 | using Blobify.Commands; 6 | using Blobify.Commands.Settings; 7 | using Devlead.Testing.MockHttp; 8 | using Blobify.Tests; 9 | 10 | public static partial class ServiceProviderFixture 11 | { 12 | static partial void InitServiceProvider(IServiceCollection services) 13 | { 14 | services 15 | .AddLogging() 16 | .AddCakeFakes() 17 | .AddSingleton( 18 | (_, _) => Task.FromResult(new AccessToken(nameof(AccessToken), DateTimeOffset.UtcNow.AddDays(1))) 19 | ) 20 | .AddSingleton() 21 | .AddSingleton() 22 | .AddTransient( 23 | _ => new ArchiveSettings 24 | { 25 | InputPath = "InputPath", 26 | AzureStorageAccount = "AzureStorageAccount", 27 | AzureStorageAccountContainer = "AzureStorageAccountContainer", 28 | AzureTenantId = Constants.Tenant.Id 29 | } 30 | ) 31 | .AddMockHttpClient(); 32 | } 33 | } -------------------------------------------------------------------------------- /src/Blobify.Tests/ModuleInit.cs: -------------------------------------------------------------------------------- 1 | using System.Runtime.CompilerServices; 2 | 3 | public static class ModuleInit 4 | { 5 | [ModuleInitializer] 6 | public static void Init() => 7 | VerifierSettings.DontIgnoreEmptyCollections(); 8 | } -------------------------------------------------------------------------------- /src/Blobify.Tests/Resources/Commands/ArchiveCommandTests/ExistingFiles/ExistingFile.json: -------------------------------------------------------------------------------- 1 | "ExistingFile" -------------------------------------------------------------------------------- /src/Blobify.Tests/Resources/Commands/ArchiveCommandTests/NewFiles/NewFile.json: -------------------------------------------------------------------------------- 1 | "NewFile" -------------------------------------------------------------------------------- /src/Blobify.Tests/Resources/Routes.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "Request": { 4 | "Methods": [ 5 | { 6 | "Method": "GET" 7 | } 8 | ], 9 | "AbsoluteUri": "https://blobify.tests/Services/Storage/TokenServiceTests/GetAsync" 10 | }, 11 | "Responses": [ 12 | { 13 | "RequestHeaders": {}, 14 | "ContentResource": "Services.Storage.TokenServiceTests.GetAsyncString.json", 15 | "ContentType": "application/json", 16 | "ContentHeaders": {}, 17 | "StatusCode": 200 18 | } 19 | ], 20 | "Authorization": { 21 | "Authorization": [ 22 | "Bearer AccessToken" 23 | ] 24 | } 25 | }, 26 | { 27 | "Request": { 28 | "Methods": [ 29 | { 30 | "Method": "HEAD" 31 | } 32 | ], 33 | "AbsoluteUri": "https://blobify.tests/Services/Storage/TokenServiceTests/HeadAsync" 34 | }, 35 | "Responses": [ 36 | { 37 | "RequestHeaders": {}, 38 | "ContentType": "application/json", 39 | "ContentHeaders": {}, 40 | "StatusCode": 200 41 | } 42 | ], 43 | "Authorization": { 44 | "Authorization": [ 45 | "Bearer AccessToken" 46 | ] 47 | } 48 | }, 49 | { 50 | "Request": { 51 | "Methods": [ 52 | { 53 | "Method": "PUT" 54 | } 55 | ], 56 | "AbsoluteUri": "https://blobify.tests/Services/Storage/TokenServiceTests/PutAsync" 57 | }, 58 | "Responses": [ 59 | { 60 | "RequestHeaders": {}, 61 | "ContentType": "application/json", 62 | "ContentHeaders": {}, 63 | "StatusCode": 202 64 | } 65 | ], 66 | "Authorization": { 67 | "Authorization": [ 68 | "Bearer AccessToken" 69 | ] 70 | } 71 | }, 72 | { 73 | "Request": { 74 | "Methods": [ 75 | { 76 | "Method": "HEAD" 77 | } 78 | ], 79 | "AbsoluteUri": "https://azurestorageaccount.blob.core.windows.net/AzureStorageAccountContainer/?restype=container" 80 | }, 81 | "Responses": [ 82 | { 83 | "RequestHeaders": {}, 84 | "ContentType": "application/json", 85 | "ContentHeaders": { 86 | "x-ms-version": [ "2024-05-04" ], 87 | "x-ms-request-id": [ "197a6f73-c01e-0176-612c-ceab32000000" ], 88 | "x-ms-lease-status": [ "unlocked" ], 89 | "x-ms-lease-state": [ "available" ], 90 | "x-ms-immutable-storage-with-versioning-enabled": [ "false" ], 91 | "x-ms-has-legal-hold": [ "false" ], 92 | "x-ms-has-immutability-policy": [ "false" ], 93 | "x-ms-deny-encryption-scope-override": [ "false" ], 94 | "x-ms-default-encryption-scope": [ "$account-encryption-key" ], 95 | "Server": [ "Windows-Azure-Blob/1.0", "Microsoft-HTTPAPI/2.0" ], 96 | "ETag": [ "0x8DC7FB978A480F9" ], 97 | "Date": [ "Thu, 04 Jul 2024 16:10:08 GMT" ] 98 | }, 99 | "StatusCode": 200 100 | } 101 | ], 102 | "Authorization": { 103 | "Authorization": [ 104 | "Bearer AccessToken" 105 | ] 106 | } 107 | }, 108 | { 109 | "Request": { 110 | "Methods": [ 111 | { 112 | "Method": "HEAD" 113 | } 114 | ], 115 | "AbsoluteUri": "https://azurestorageaccount.blob.core.windows.net/AzureStorageAccountContainer/ExistingFile.json" 116 | }, 117 | "Responses": [ 118 | { 119 | "RequestHeaders": {}, 120 | "ContentType": "application/json", 121 | "ContentResource": "Commands.ArchiveCommandTests.ExistingFiles.ExistingFile.json", 122 | "ContentHeaders": { 123 | "Last-Modified": [ "Wed, 03 Jul 2024 22:06:52 GMT" ], 124 | "Date": [ "Thu, 04 Jul 2024 16:39:57 GMT" ], 125 | "ETag": [ "0x8DC9BAC719D2469" ], 126 | "Server": [ "Windows-Azure-Blob/1.0", "Microsoft-HTTPAPI/2.0" ], 127 | "x-ms-access-tier": [ "Hot" ], 128 | "x-ms-access-tier-inferred": [ "true" ], 129 | "x-ms-blob-type": [ "BlockBlob" ], 130 | "x-ms-creation-time": [ "Wed, 03 Jul 2024 22:06:52 GMT" ], 131 | "x-ms-lease-state": [ "available" ], 132 | "x-ms-lease-status": [ "unlocked" ], 133 | "x-ms-request-id": [ "14fdc4b0-001e-0179-5930-cedd5e000000" ], 134 | "x-ms-server-encrypted": [ "true" ], 135 | "x-ms-version": [ "2024-05-04" ] 136 | }, 137 | "StatusCode": 200 138 | } 139 | ], 140 | "Authorization": { 141 | "Authorization": [ 142 | "Bearer AccessToken" 143 | ] 144 | } 145 | }, 146 | { 147 | "Request": { 148 | "Methods": [ 149 | { 150 | "Method": "PUT" 151 | } 152 | ], 153 | "AbsoluteUri": "https://azurestorageaccount.blob.core.windows.net/AzureStorageAccountContainer/NewFile.json" 154 | }, 155 | "Responses": [ 156 | { 157 | "RequestHeaders": { 158 | "Content-Type": [ "application/json" ], 159 | "Content-Length": [ "9" ], 160 | "x-ms-blob-type": [ "BlockBlob" ], 161 | "Content-MD5": [ "uasiD3l1Rg7NApFvCBOV1Q==" ] 162 | }, 163 | "ContentType": "application/json", 164 | "ContentResource": "Commands.ArchiveCommandTests.NewFiles.NewFile.json", 165 | "ContentHeaders": { 166 | "Last-Modified": [ "Wed, 03 Jul 2024 22:06:52 GMT" ], 167 | "Date": [ "Thu, 04 Jul 2024 16:39:57 GMT" ], 168 | "ETag": [ "0x8DC9BAC719D2469" ], 169 | "Server": [ "Windows-Azure-Blob/1.0", "Microsoft-HTTPAPI/2.0" ], 170 | "x-ms-access-tier": [ "Hot" ], 171 | "x-ms-access-tier-inferred": [ "true" ], 172 | "x-ms-blob-type": [ "BlockBlob" ], 173 | "x-ms-creation-time": [ "Wed, 03 Jul 2024 22:06:52 GMT" ], 174 | "x-ms-lease-state": [ "available" ], 175 | "x-ms-lease-status": [ "unlocked" ], 176 | "x-ms-request-id": [ "14fdc4b0-001e-0179-5930-cedd5e000000" ], 177 | "x-ms-server-encrypted": [ "true" ], 178 | "x-ms-version": [ "2024-05-04" ] 179 | }, 180 | "StatusCode": 201, 181 | "EnableRequests": [ 182 | { 183 | "Method": "HEAD", 184 | "AbsoluteUri": "https://azurestorageaccount.blob.core.windows.net/AzureStorageAccountContainer/NewFile.json" 185 | } 186 | ] 187 | } 188 | ], 189 | "Authorization": { 190 | "Authorization": [ 191 | "Bearer AccessToken" 192 | ] 193 | } 194 | }, 195 | { 196 | "Request": { 197 | "Methods": [ 198 | { 199 | "Method": "HEAD" 200 | } 201 | ], 202 | "AbsoluteUri": "https://azurestorageaccount.blob.core.windows.net/AzureStorageAccountContainer/NewFile.json", 203 | "Disabled": true 204 | }, 205 | "Responses": [ 206 | { 207 | "RequestHeaders": { }, 208 | "ContentType": "application/json", 209 | "ContentResource": "Commands.ArchiveCommandTests.NewFiles.NewFile.json", 210 | "ContentHeaders": { 211 | "Last-Modified": [ "Wed, 03 Jul 2024 22:06:52 GMT" ], 212 | "Date": [ "Thu, 04 Jul 2024 16:39:57 GMT" ], 213 | "ETag": [ "0x8DC9BAC719D2469" ], 214 | "Server": [ "Windows-Azure-Blob/1.0", "Microsoft-HTTPAPI/2.0" ], 215 | "x-ms-access-tier": [ "Hot" ], 216 | "x-ms-access-tier-inferred": [ "true" ], 217 | "x-ms-blob-type": [ "BlockBlob" ], 218 | "x-ms-creation-time": [ "Wed, 03 Jul 2024 22:06:52 GMT" ], 219 | "x-ms-lease-state": [ "available" ], 220 | "x-ms-lease-status": [ "unlocked" ], 221 | "x-ms-request-id": [ "14fdc4b0-001e-0179-5930-cedd5e000000" ], 222 | "x-ms-server-encrypted": [ "true" ], 223 | "x-ms-version": [ "2024-05-04" ] 224 | }, 225 | "StatusCode": 200 226 | } 227 | ], 228 | "Authorization": { 229 | "Authorization": [ 230 | "Bearer AccessToken" 231 | ] 232 | } 233 | } 234 | ] -------------------------------------------------------------------------------- /src/Blobify.Tests/Resources/Services/Storage/TokenServiceTests/GetAsyncString.json: -------------------------------------------------------------------------------- 1 | "OK" -------------------------------------------------------------------------------- /src/Blobify.Tests/Unit/Commands/ArchiveCommandTests.ExistingFiles.ExecuteAsync_content=-ExistingFile-.verified.txt: -------------------------------------------------------------------------------- 1 | { 2 | FileExists: false 3 | } -------------------------------------------------------------------------------- /src/Blobify.Tests/Unit/Commands/ArchiveCommandTests.ExistingFiles.ExecuteAsync_content=-ExistingFileWrongHash-.verified.txt: -------------------------------------------------------------------------------- 1 | { 2 | FileExists: true 3 | } -------------------------------------------------------------------------------- /src/Blobify.Tests/Unit/Commands/ArchiveCommandTests.NewFiles.ExecuteAsync.verified.txt: -------------------------------------------------------------------------------- 1 | { 2 | FileExists: false 3 | } -------------------------------------------------------------------------------- /src/Blobify.Tests/Unit/Commands/ArchiveCommandTests.NoFiles.ExecuteAsync.verified.txt: -------------------------------------------------------------------------------- 1 | 0 -------------------------------------------------------------------------------- /src/Blobify.Tests/Unit/Commands/ArchiveCommandTests.cs: -------------------------------------------------------------------------------- 1 | using Blobify.Commands; 2 | using Blobify.Commands.Settings; 3 | 4 | namespace Blobify.Tests.Unit.Commands; 5 | public class ArchiveCommandTests 6 | { 7 | public class NoFiles 8 | { 9 | [Test] 10 | public async Task ExecuteAsync() 11 | { 12 | // Given 13 | var (archiveCommand, settings) = ServiceProviderFixture.GetRequiredService(); 14 | 15 | // When 16 | var result = await archiveCommand.ExecuteAsync( 17 | null!, 18 | settings 19 | ); 20 | 21 | // Then 22 | await Verify(result); 23 | } 24 | } 25 | 26 | public class ExistingFiles 27 | { 28 | 29 | [TestCase("\"ExistingFile\"")] 30 | [TestCase("\"ExistingFileWrongHash\"")] 31 | public async Task ExecuteAsync(string content) 32 | { 33 | // Given 34 | var (archiveCommand, settings, fileSystem) = ServiceProviderFixture.GetRequiredService(); 35 | var file = fileSystem.CreateFile("/Working/InputPath/ExistingFile.json").SetContent(content); 36 | 37 | // When 38 | var result = await archiveCommand.ExecuteAsync( 39 | null!, 40 | settings 41 | ); 42 | 43 | // Then 44 | await Verify( 45 | new 46 | { 47 | ExitCode = result, 48 | FileExists = file.Exists 49 | } 50 | ); 51 | } 52 | } 53 | 54 | public class NewFiles 55 | { 56 | 57 | [Test] 58 | public async Task ExecuteAsync() 59 | { 60 | // Given 61 | var (archiveCommand, settings, fileSystem) = ServiceProviderFixture.GetRequiredService(); 62 | var file = fileSystem.CreateFile("/Working/InputPath/NewFile.json").SetContent("\"NewFile\""); 63 | 64 | // When 65 | var result = await archiveCommand.ExecuteAsync( 66 | null!, 67 | settings 68 | ); 69 | 70 | // Then 71 | await Verify( 72 | new 73 | { 74 | ExitCode = result, 75 | FileExists = file.Exists 76 | } 77 | ); 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/Blobify.Tests/Unit/Services/Storage/TokenServiceTests.GetAsync.verified.txt: -------------------------------------------------------------------------------- 1 | OK -------------------------------------------------------------------------------- /src/Blobify.Tests/Unit/Services/Storage/TokenServiceTests.HeadAsync.verified.txt: -------------------------------------------------------------------------------- 1 | { 2 | Item1: OK, 3 | Item2: [] 4 | } -------------------------------------------------------------------------------- /src/Blobify.Tests/Unit/Services/Storage/TokenServiceTests.PutAsync.verified.txt: -------------------------------------------------------------------------------- 1 | { 2 | Item1: Accepted, 3 | Item2: [] 4 | } -------------------------------------------------------------------------------- /src/Blobify.Tests/Unit/Services/Storage/TokenServiceTests.cs: -------------------------------------------------------------------------------- 1 | using Blobify.Services.Storage; 2 | using System.Runtime.CompilerServices; 3 | 4 | namespace Blobify.Tests.Unit.Services.Storage; 5 | 6 | public class TokenServiceTests 7 | { 8 | public Uri BaseUri { get; private set; }= new Uri(Constants.Request.BaseUri, "Services/Storage/TokenServiceTests/"); 9 | 10 | private Uri GetUri( 11 | [CallerMemberName] 12 | string path = "" 13 | ) => new (BaseUri, path); 14 | 15 | [Test] 16 | public async Task GetAsync() 17 | { 18 | // Given 19 | var tokenService = ServiceProviderFixture.GetRequiredService(); 20 | 21 | // When 22 | var result = await tokenService.GetAsync( 23 | Constants.Tenant.Id, 24 | GetUri() 25 | ); 26 | 27 | // Then 28 | await Verify(result); 29 | } 30 | 31 | [Test] 32 | public async Task HeadAsync() 33 | { 34 | // Given 35 | var tokenService = ServiceProviderFixture.GetRequiredService(); 36 | 37 | // When 38 | var result = await tokenService.HeadAsync( 39 | Constants.Tenant.Id, 40 | GetUri() 41 | ); 42 | 43 | // Then 44 | await Verify(result); 45 | } 46 | 47 | [Test] 48 | public async Task PutAsync() 49 | { 50 | // Given 51 | var tokenService = ServiceProviderFixture.GetRequiredService(); 52 | 53 | // When 54 | var result = await tokenService.PutAsync( 55 | Constants.Tenant.Id, 56 | GetUri() 57 | ); 58 | 59 | // Then 60 | await Verify(result); 61 | } 62 | } -------------------------------------------------------------------------------- /src/Blobify.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.0.31903.59 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Blobify", "Blobify\Blobify.csproj", "{1711E5D9-9016-43F0-B637-DDB3FAF146FA}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Blobify.Tests", "Blobify.Tests\Blobify.Tests.csproj", "{1E755EC6-7FA4-426D-9FCC-B8FBE6DD9CCC}" 9 | EndProject 10 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".SolutionItems", ".SolutionItems", "{5E3E15E0-9B5F-408E-9FC9-CAF201693D99}" 11 | ProjectSection(SolutionItems) = preProject 12 | Directory.Packages.props = Directory.Packages.props 13 | EndProjectSection 14 | EndProject 15 | Global 16 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 17 | Debug|Any CPU = Debug|Any CPU 18 | Release|Any CPU = Release|Any CPU 19 | EndGlobalSection 20 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 21 | {1711E5D9-9016-43F0-B637-DDB3FAF146FA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 22 | {1711E5D9-9016-43F0-B637-DDB3FAF146FA}.Debug|Any CPU.Build.0 = Debug|Any CPU 23 | {1711E5D9-9016-43F0-B637-DDB3FAF146FA}.Release|Any CPU.ActiveCfg = Release|Any CPU 24 | {1711E5D9-9016-43F0-B637-DDB3FAF146FA}.Release|Any CPU.Build.0 = Release|Any CPU 25 | {1E755EC6-7FA4-426D-9FCC-B8FBE6DD9CCC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 26 | {1E755EC6-7FA4-426D-9FCC-B8FBE6DD9CCC}.Debug|Any CPU.Build.0 = Debug|Any CPU 27 | {1E755EC6-7FA4-426D-9FCC-B8FBE6DD9CCC}.Release|Any CPU.ActiveCfg = Release|Any CPU 28 | {1E755EC6-7FA4-426D-9FCC-B8FBE6DD9CCC}.Release|Any CPU.Build.0 = Release|Any CPU 29 | EndGlobalSection 30 | GlobalSection(SolutionProperties) = preSolution 31 | HideSolutionNode = FALSE 32 | EndGlobalSection 33 | GlobalSection(ExtensibilityGlobals) = postSolution 34 | SolutionGuid = {8A8DCF37-E3AD-414B-AD18-E5B8E0A98F41} 35 | EndGlobalSection 36 | EndGlobal 37 | -------------------------------------------------------------------------------- /src/Blobify/Blobify.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0;net9.0 6 | enable 7 | true 8 | enable 9 | icon/blobify.png 10 | true 11 | Blobify 12 | blobify 13 | README.md 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /src/Blobify/Commands/ArchiveCommand.cs: -------------------------------------------------------------------------------- 1 | 2 | using Blobify.Services.Storage; 3 | using Cake.Common.IO; 4 | using Cake.Common.Security; 5 | using System.Net.Http.Headers; 6 | 7 | namespace Blobify.Commands; 8 | 9 | public class ArchiveCommand( 10 | ICakeContext cakeContext, 11 | ILogger logger, 12 | TokenService tokenService 13 | ) : AsyncCommand 14 | { 15 | public override async Task ExecuteAsync(CommandContext context, ArchiveSettings settings) 16 | { 17 | if (settings.InputPath.IsRelative) 18 | { 19 | logger.LogInformation("Relative inputpath {InputPath} making absolute...", settings.InputPath); 20 | settings.InputPath = settings.InputPath.MakeAbsolute(cakeContext.Environment); 21 | } 22 | logger.LogInformation("InputPath: {InputPath}", settings.InputPath); 23 | logger.LogInformation("AzureStorageAccount: {AzureStorageAccount}", settings.AzureStorageAccount); 24 | logger.LogInformation("AzureStorageAccountContainer: {AzureStorageAccountContainer}", settings.AzureStorageAccountContainer); 25 | 26 | var searchPattern = settings.InputPath.CombineWithFilePath(settings.FilePattern).FullPath; 27 | logger.LogInformation("Looking for files ({SearchPattern})...", searchPattern); 28 | 29 | var files = cakeContext.GetFiles(searchPattern); 30 | 31 | logger.LogInformation("Found {FileCount} files.", files.Count); 32 | 33 | if(files.Count == 0) 34 | { 35 | logger.LogInformation("No files found, exiting..."); 36 | return 0; 37 | } 38 | 39 | var containerUrl = settings.GetAzureStorageBlobUrl(); 40 | 41 | switch (await tokenService.HeadAsync(settings.AzureTenantId, new Uri(containerUrl, "?restype=container"))) 42 | { 43 | case (HttpStatusCode.NotFound, _, _): 44 | logger.LogInformation("Container {AzureStorageAccount}/{AzureStorageAccountContainer} not found, creating...", settings.AzureStorageAccount, settings.AzureStorageAccountContainer); 45 | await tokenService.PutAsync(settings.AzureTenantId, new Uri(containerUrl, "?restype=container")); 46 | break; 47 | case (HttpStatusCode.OK, _, _): 48 | logger.LogInformation("Container {AzureStorageAccount}/{AzureStorageAccountContainer} found", settings.AzureStorageAccount, settings.AzureStorageAccountContainer); 49 | break; 50 | default: 51 | logger.LogError("Failed to check container {ContainerUrl}", containerUrl); 52 | return 1; 53 | } 54 | 55 | await Parallel.ForEachAsync( 56 | files, 57 | async (filePath, ct) => 58 | { 59 | var targetPath = settings.InputPath.GetRelativePath(filePath); 60 | var targetUri = new Uri(containerUrl, targetPath.FullPath); 61 | var file = cakeContext.FileSystem.GetFile(filePath); 62 | var contentType = MimeTypes.TryGetMimeType( 63 | filePath.FullPath, 64 | out var mimeType 65 | ) 66 | ? mimeType 67 | : "application/octet-stream"; 68 | 69 | var hash = cakeContext.CalculateFileHash(filePath, HashAlgorithm.MD5); 70 | 71 | switch (await tokenService.HeadAsync(settings.AzureTenantId, targetUri, ct)) 72 | { 73 | case (HttpStatusCode.NotFound, _, _): 74 | { 75 | logger.LogInformation("Blob {File} not found, uploading...", targetPath.FullPath); 76 | await using var stream = file.OpenRead(); 77 | using var content = new StreamContent(stream) { 78 | Headers = { 79 | ContentType = new MediaTypeHeaderValue(contentType), 80 | ContentLength = file.Length, 81 | ContentMD5 = hash.ComputedHash 82 | } 83 | }; 84 | content.Headers.TryAddWithoutValidation("x-ms-blob-type", "BlockBlob"); 85 | 86 | switch (await tokenService.PutAsync(settings.AzureTenantId, targetUri, ct, content)) 87 | { 88 | case (HttpStatusCode.Created, _, _): 89 | logger.LogInformation("Blob {File} uploaded", targetPath.FullPath); 90 | break; 91 | default: 92 | throw new Exception($"Failed to upload blob {targetUri}"); 93 | } 94 | break; 95 | } 96 | case (HttpStatusCode.OK, _, byte[] md5Hash): 97 | if (md5Hash.SequenceEqual(hash.ComputedHash)) 98 | { 99 | logger.LogInformation("Blob {File} found and hash match deleting...", targetPath.FullPath); 100 | file.Delete(); 101 | } 102 | else 103 | { 104 | logger.LogInformation("Blob {File} found, skipping...", targetPath.FullPath); 105 | } 106 | return; 107 | default: 108 | throw new Exception($"Failed to check blob {targetUri}"); 109 | } 110 | 111 | switch (await tokenService.HeadAsync(settings.AzureTenantId, targetUri, ct)) 112 | { 113 | case (HttpStatusCode.OK, _, byte[] md5Hash): 114 | if (md5Hash.SequenceEqual(hash.ComputedHash)) 115 | { 116 | logger.LogInformation("Blob {File} found and hash match deleting...", targetPath.FullPath); 117 | file.Delete(); 118 | } 119 | else 120 | { 121 | throw new Exception($"Blob {targetPath.FullPath} found blob but hash mismatch, won't delete."); 122 | } 123 | 124 | return; 125 | } 126 | } 127 | ); 128 | 129 | 130 | return 0; 131 | } 132 | } -------------------------------------------------------------------------------- /src/Blobify/Commands/Settings/ArchiveSettings.cs: -------------------------------------------------------------------------------- 1 | using System.ComponentModel; 2 | 3 | namespace Blobify.Commands.Settings; 4 | 5 | public class ArchiveSettings : CommandSettings 6 | { 7 | [CommandArgument(0, "")] 8 | [ValidatePath] 9 | [Description("Input path")] 10 | public required DirectoryPath InputPath { get; set; } 11 | 12 | [CommandArgument(1, "")] 13 | [ValidateString] 14 | [Description("Azure Storage Account Name")] 15 | public required string AzureStorageAccount { get; set; } 16 | 17 | [CommandArgument(2, "")] 18 | [ValidateString] 19 | [Description("Azure Storage Account Container Name")] 20 | public required string AzureStorageAccountContainer { get; set; } 21 | 22 | 23 | [CommandOption("--azure-tenant-id")] 24 | [Description("Azure Tentant ID to sign into")] 25 | public string? AzureTenantId { get; set; } = Environment.GetEnvironmentVariable("AZURE_TENANT_ID"); 26 | 27 | 28 | [CommandOption("--file-pattern")] 29 | [Description("Local file pattern to match")] 30 | public string FilePattern { get; set; } = "**/*.*"; 31 | 32 | public string AzureStorageBlobSuffix { get; set; } = "blob.core.windows.net"; 33 | 34 | public Uri GetAzureStorageBlobUrl() => new ( 35 | $"https://{AzureStorageAccount}.{AzureStorageBlobSuffix}/{AzureStorageAccountContainer}/", 36 | UriKind.Absolute 37 | ); 38 | } -------------------------------------------------------------------------------- /src/Blobify/Commands/Validation/ValidatePathAttribute.cs: -------------------------------------------------------------------------------- 1 | namespace Blobify.Commands.Validation; 2 | 3 | public class ValidatePathAttribute : ParameterValidationAttribute 4 | { 5 | #pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type. 6 | public ValidatePathAttribute() : base(null) 7 | #pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type. 8 | { 9 | } 10 | 11 | public override ValidationResult Validate(CommandParameterContext context) => context.Value switch 12 | { 13 | FilePath filePath when File.Exists(filePath.FullPath) 14 | => ValidationResult.Success(), 15 | 16 | DirectoryPath directoryPath when Directory.Exists(directoryPath.FullPath) 17 | => ValidationResult.Success(), 18 | 19 | _ => ValidationResult.Error($"Invalid {context.Parameter?.PropertyName} ({context.Value}) specified.") 20 | }; 21 | } 22 | -------------------------------------------------------------------------------- /src/Blobify/Commands/Validation/ValidateStringAttribute.cs: -------------------------------------------------------------------------------- 1 | namespace Blobify.Commands.Validation; 2 | 3 | public class ValidateStringAttribute : ParameterValidationAttribute 4 | { 5 | public const int MinimumLength = 3; 6 | private static readonly (bool IsString, bool IsMinimumLength, string Value) InvalidStringValue = (false, false, string.Empty); 7 | 8 | #nullable disable 9 | public ValidateStringAttribute() : base(errorMessage: null) 10 | { 11 | } 12 | #nullable enable 13 | 14 | public override ValidationResult Validate(CommandParameterContext context) 15 | => ( 16 | context.Value is string stringValue 17 | ? ( 18 | IsString: true, 19 | IsMinimumLength: stringValue.Length >= MinimumLength, 20 | Value: stringValue 21 | ) 22 | : InvalidStringValue 23 | ) switch 24 | { 25 | {IsString: true, IsMinimumLength: true} 26 | => ValidationResult.Success(), 27 | 28 | {IsString: true, IsMinimumLength: false} invalidValue 29 | => ValidationResult.Error( 30 | $"{context.Parameter.PropertyName} ({invalidValue.Value}) needs to be at least {MinimumLength} characters long was {invalidValue.Value.Length}."), 31 | 32 | _ => ValidationResult.Error( 33 | $"Invalid {context.Parameter.PropertyName} ({context.Value ?? ""}) specified.") 34 | }; 35 | } -------------------------------------------------------------------------------- /src/Blobify/Program.cs: -------------------------------------------------------------------------------- 1 | using Azure.Core; 2 | using Azure.Identity; 3 | 4 | public partial class Program 5 | { 6 | static partial void AddServices(IServiceCollection services) 7 | { 8 | services 9 | .AddCakeCore() 10 | 11 | .AddSingleton( 12 | async (tenantId, scope) => 13 | { 14 | var tokenCredential = new DefaultAzureCredential(); 15 | var accessToken = await tokenCredential.GetTokenAsync( 16 | new TokenRequestContext( 17 | tenantId: tenantId, 18 | scopes: [ 19 | scope ?? "https://storage.azure.com/.default" 20 | ] 21 | ) 22 | ); 23 | return accessToken; 24 | } 25 | ) 26 | .AddSingleton() 27 | .AddSingleton(); 28 | 29 | services.AddHttpClient(); 30 | } 31 | 32 | // Configure commands 33 | static partial void ConfigureApp(AppServiceConfig appServiceConfig) 34 | { 35 | appServiceConfig.SetApplicationName("blobify"); 36 | 37 | appServiceConfig.AddCommand("archive") 38 | .WithDescription("Example Archive command.") 39 | .WithExample(["archive", "inputpath", "storageaccountname"]); 40 | } 41 | } -------------------------------------------------------------------------------- /src/Blobify/Services/AzureTokenService.cs: -------------------------------------------------------------------------------- 1 | namespace Blobify.Services; 2 | 3 | public delegate Task AzureTokenService(string? tenantId, string? scope = null); -------------------------------------------------------------------------------- /src/Blobify/Services/Storage/TokenService.cs: -------------------------------------------------------------------------------- 1 | using System.Net.Http.Json; 2 | using System.Runtime.CompilerServices; 3 | 4 | namespace Blobify.Services.Storage; 5 | public class TokenService( 6 | IHttpClientFactory httpClientFactory, 7 | AzureTokenService azureTokenService, 8 | ILogger logger 9 | ) 10 | { 11 | static Azure.Core.AccessToken? cachedAccessToken = default; 12 | 13 | private async Task GetAzureToken(string? tenantId) 14 | { 15 | if (cachedAccessToken.HasValue && (cachedAccessToken.Value.ExpiresOn - DateTimeOffset.UtcNow).TotalMinutes > 1) 16 | { 17 | return cachedAccessToken.Value.Token; 18 | } 19 | 20 | logger.LogInformation("Getting azure token..."); 21 | try 22 | { 23 | var accessToken = await azureTokenService(tenantId); 24 | cachedAccessToken = accessToken; 25 | return accessToken.Token; 26 | } 27 | catch (Exception ex) 28 | { 29 | logger.LogError(ex, "Failed to fetch Azure Access Token"); 30 | throw; 31 | } 32 | } 33 | 34 | private HttpClient GetBearerTokenHttpClient( 35 | string bearerToken, 36 | string? accept = "application/json", 37 | string? contentType = "application/json", 38 | [CallerMemberName] 39 | string name = nameof(GetBearerTokenHttpClient) 40 | ) 41 | { 42 | var bearerHttpClient = httpClientFactory.CreateClient(name); 43 | 44 | bearerHttpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue( 45 | "Bearer", 46 | bearerToken 47 | ); 48 | 49 | bearerHttpClient.DefaultRequestHeaders.TryAddWithoutValidation("x-ms-version", "2024-05-04"); 50 | bearerHttpClient.DefaultRequestHeaders.TryAddWithoutValidation("x -ms-date", DateTimeOffset.UtcNow.ToString("R")); 51 | bearerHttpClient.DefaultRequestHeaders.Date = DateTimeOffset.UtcNow; 52 | 53 | 54 | if (!string.IsNullOrWhiteSpace(accept)) 55 | { 56 | bearerHttpClient.DefaultRequestHeaders.Accept.TryParseAdd(accept); 57 | } 58 | 59 | if (!string.IsNullOrWhiteSpace(contentType)) 60 | { 61 | bearerHttpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", contentType); 62 | } 63 | 64 | return bearerHttpClient; 65 | } 66 | 67 | public async Task GetAsync( 68 | string? tenantId, 69 | Uri url, 70 | CancellationToken cancellationToken = default, 71 | string? accept = "application/json" 72 | ) 73 | { 74 | using var httpClient = GetBearerTokenHttpClient( 75 | await GetAzureToken(tenantId), 76 | accept, 77 | null 78 | ); 79 | 80 | return await GetFromJsonAsync(httpClient, url, cancellationToken); 81 | } 82 | 83 | public Task<(HttpStatusCode StatusCode, ILookup Headers, byte[]? ContentMD5)> HeadAsync( 84 | string? tenantId, 85 | Uri url, 86 | CancellationToken cancellationToken = default 87 | ) => SendAsync(tenantId, url, HttpMethod.Head, cancellationToken); 88 | 89 | public Task<(HttpStatusCode StatusCode, ILookup Headers, byte[]? ContentMD5)> PutAsync( 90 | string? tenantId, 91 | Uri url, 92 | CancellationToken cancellationToken = default, 93 | HttpContent? content = null 94 | ) => SendAsync(tenantId, url, HttpMethod.Put, cancellationToken, content); 95 | 96 | private async Task<(HttpStatusCode StatusCode, ILookup Headers, byte[]? ContentMD5)> SendAsync( 97 | string? tenantId, 98 | Uri url, 99 | HttpMethod method, 100 | CancellationToken cancellationToken, 101 | HttpContent? content = null 102 | ) 103 | { 104 | var httpClient = GetBearerTokenHttpClient( 105 | await GetAzureToken(tenantId), 106 | null, 107 | null 108 | ); 109 | 110 | using var response = await httpClient.SendAsync( 111 | new HttpRequestMessage( 112 | method, 113 | url 114 | ) 115 | { 116 | Content = content 117 | }, 118 | HttpCompletionOption.ResponseHeadersRead, 119 | cancellationToken 120 | ); 121 | 122 | return ( 123 | response.StatusCode, 124 | ( 125 | from header in response.Headers.Union(response.Content.Headers) 126 | from value in header.Value 127 | select (header.Key, value) 128 | ).ToLookup( 129 | x => x.Key, 130 | x => x.value 131 | ), 132 | response.Content.Headers.ContentMD5 133 | ); 134 | } 135 | 136 | private static async Task GetFromJsonAsync(HttpClient httpClient, Uri url, CancellationToken cancellationToken) 137 | { 138 | var result = await httpClient.GetFromJsonAsync(url, cancellationToken); 139 | 140 | ArgumentNullException.ThrowIfNull(result); 141 | 142 | return result; 143 | } 144 | } 145 | -------------------------------------------------------------------------------- /src/Directory.Packages.props: -------------------------------------------------------------------------------- 1 | 2 | 3 | true 4 | false 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | --------------------------------------------------------------------------------