Skip to content

Commit

Permalink
BlobCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH committed May 2, 2020
1 parent 646f314 commit 7082a39
Show file tree
Hide file tree
Showing 6 changed files with 879 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
</PropertyGroup>

<PropertyGroup>
<VersionPrefix>0.0.1-preview-00</VersionPrefix>
<VersionPrefix>0.0.1</VersionPrefix>
<Copyright>Copyright © 2020 Stef Heyenrath</Copyright>
<Authors>Stef Heyenrath</Authors>
<PackageReleaseNotes>See CHANGELOG.md</PackageReleaseNotes>
Expand Down
2 changes: 2 additions & 0 deletions Pulumi.Azure.Extensions Solution.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http:https://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http:https://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=Pulumi/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,18 @@ The following extensions are defined:

#### BlobCollection

...todo...
When you want to publish all files from a Blazor WASM website to an Azure Storage Static Website, use the code below:

``` c#
string sourceFolder = "C:\Users\xxx\Documents\GitHub\BlazorApp\publish\wwwroot";
var blobCollection = new BlobCollection(sourceFolder, new BlobCollectionArgs
{
// Required
Type = BlobTypes.Block,
StorageAccountName = storageAccount.Name,
StorageContainerName = "$web",
AccessTier = BlobAccessTiers.Hot
});
```

There is no need to specify the ContentType for each file, this is automatically resolved using [MimeTypeMap](https://github.com/samuelneff/MimeTypeMap).
4 changes: 4 additions & 0 deletions src/Pulumi.Azure.Extensions/Pulumi.Azure.Extensions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@
<ProjectGuid>{C7F2E3E7-05CE-4409-AC94-A0557B5B8C13}</ProjectGuid>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Pulumi.Azure" Version="3.0.0" />
</ItemGroup>

</Project>
90 changes: 90 additions & 0 deletions src/Pulumi.Azure.Extensions/Storage/BlobCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using System;
using System.IO;
using System.Linq;
using Pulumi.Azure.Extensions.Utils;
using Pulumi.Azure.Storage;

namespace Pulumi.Azure.Extensions.Storage
{
public sealed class BlobCollectionArgs : ResourceArgs
{
/// <summary>
/// The access tier of the storage blob. Possible values are `Archive`, `Cool` and `Hot`.
/// </summary>
[Input("accessTier", false, false)]
public Input<string> AccessTier { get; set; }

/// <summary>
/// The number of workers per CPU core to run for concurrent uploads. Defaults to `8`.
/// </summary>
[Input("parallelism", false, false)]
public Input<int> Parallelism { get; set; }

/// <summary>
/// Specifies the storage account in which to create the storage container.
/// Changing this forces a new resource to be created.
/// </summary>
[Input("storageAccountName", true, false)]
public Input<string> StorageAccountName { get; set; }

/// <summary>
/// The name of the storage container in which this blob should be created.
/// </summary>
[Input("storageContainerName", true, false)]
public Input<string> StorageContainerName { get; set; }

/// <summary>
/// The type of the storage blobs to be created. Possible values are `Append`, `Block` or `Page`. Changing this forces a new resource to be created.
/// </summary>
[Input("type", true, false)]
public Input<string> Type { get; set; }
}

public sealed class BlobCollection
{
private const string SearchPattern = "*.*";

/// <summary>
/// Upload all files and folders from a sourceFolder to a Blob Storage Account in Azure.
/// </summary>
/// <param name="sourceFolder">An absolute path to a folder on the local file system.</param>
/// <param name="args">The arguments used to populate the <see cref="Blob"/> resources.</param>
public BlobCollection(string sourceFolder, BlobCollectionArgs args)
{
if (string.IsNullOrEmpty(sourceFolder))
{
throw new ArgumentNullException(nameof(sourceFolder));
}

if (args == null)
{
throw new ArgumentNullException(nameof(args));
}

int sourceFolderLength = sourceFolder.Length + 1;

var files = Directory.EnumerateFiles(sourceFolder, SearchPattern, SearchOption.AllDirectories)
.Select(path => new
{
info = new FileInfo(path),
name = path.Remove(0, sourceFolderLength).Replace(Path.PathSeparator, '/'), // Make the name Azure Storage compatible
})
.Where(file => file.info.Length > 0) // https://github.com/pulumi/pulumi-azure/issues/544
;

foreach (var file in files)
{
_ = new Blob(file.name, new BlobArgs
{
AccessTier = args.AccessTier,
Name = file.name,
StorageAccountName = args.StorageAccountName,
StorageContainerName = args.StorageContainerName,
Type = args.Type,
Source = new FileAsset(file.info.FullName),
ContentType = MimeTypeMap.GetMimeType(file.info.Extension)
});
}
}
}
}
Loading

0 comments on commit 7082a39

Please sign in to comment.