aloneguid / stowage

Bloat-free, no BS cloud storage SDK.
Apache License 2.0
168 stars 14 forks source link

How to use Azurite (emulator) for Azure Blob Storage. #5

Closed yannickrondeau closed 1 year ago

yannickrondeau commented 2 years ago

Hi 👋

The previous version Storage.Net is supporting the emu account while in development. It doesn't seem like stowage is supporting it. I looked at the source code and couldn't find any way of connecting, easily, to the emulator. Therefore, I revised the current implementation and do have one issue.

To address the issue, I used the same approach as @iamkoch which is allowing to override the azure endpoint. It worked great; however, it looks like using the http://127.0.0.1:10000/devstoreaccount1 endpoint with a container (emu in this example) lead to an issue with the emulator not looking at the right account. To address that, I had to set my ContainerName to devstoreaccount1/emu.

Here is the code I changed to get things working. If you prefer, I can create a pull request to start a discussion.

Files.cs

public static IFileStorage AzureBlobStorage(this IFilesFactory _, string accountName, string sharedKey, string containerName = null)
{
   return AzureBlobStorage(_, new Uri($"https://{accountName}.blob.core.windows.net"), accountName, sharedKey, containerName);
}

public static IFileStorage AzureBlobStorage(this IFilesFactory _, Uri endpoint, string accountName, string sharedKey, string containerName = null)
{
   return new AzureBlobFileStorage(endpoint, containerName, new SharedKeyAuthHandler(accountName, sharedKey));
}

AzureBlobFileStorage.cs

public AzureBlobFileStorage(Uri endpoint, string containerName, DelegatingHandler authHandler) : base(endpoint, authHandler)
{
   if(endpoint is null)
      throw new ArgumentNullException(nameof(endpoint));
   if(string.IsNullOrEmpty(containerName))
      throw new ArgumentException($"'{nameof(containerName)}' cannot be null or empty", nameof(containerName));
   if(authHandler is null)
      throw new ArgumentNullException(nameof(authHandler));

   _containerName = containerName;
}

I guess I could keep it that way, but I find it a bit confusing to put the storage account in the container name when in development. Haven't tested in production with Azure Storage yet.

Thanks,

aloneguid commented 2 years ago

Sounds good to me, please raise a PR. I haven't used Azurite yet other than from azure functions emulator indirectly so can't confirm how it works - you have my full trust and support, should be a great addition.

IeuanWalker commented 1 year ago

@YannickRondeau are you doing a PR?

If not I can look at doing one, looking to provide more storage options for the thing I'm developing and this NuGet is perfect, but I use Azurite for local development.

yannickrondeau commented 1 year ago

@IeuanWalker I haven't work on anything yet. For our project, we've decided to go directly with the Azure Storage SDK which was easier in our case.