Skip to content

Instantly share code, notes, and snippets.

@LucGosso
Last active April 1, 2022 05:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LucGosso/3c206d9cce050d607c3cf19a0c7a38b3 to your computer and use it in GitHub Desktop.
Save LucGosso/3c206d9cce050d607c3cf19a0c7a38b3 to your computer and use it in GitHub Desktop.
Example how to access share in azure
using Azure.Storage.Files.Shares;
using Azure.Storage.Files.Shares.Models;
public StockInventory[] GetFromAzureFileShare()
{
// Name of the share, directory, and file
string shareName = ConfigurationManager.AppSettings.Get("inRiver.StorageAccountShareReference");
string storageAccountName = ConfigurationManager.AppSettings.Get("inRiver.StorageAccountName");
string storageAccountKey = ConfigurationManager.AppSettings.Get("inRiver.StorageAccountKey");
string dirName = "stock-sync";
string fileName = "stock.json";
string connectionString = $"DefaultEndpointsProtocol=https;AccountName={storageAccountName};AccountKey={storageAccountKey};EndpointSuffix=core.windows.net";
// Get a reference from our share
ShareClient share = new ShareClient(connectionString, shareName);
// Get a reference from our directory - directory located on root level
ShareDirectoryClient directory = share.GetDirectoryClient(dirName);
// Get a reference to a subdirectory not located on root level
//directory = directory.GetSubdirectoryClient("A1");
// Get a reference to our file
ShareFileClient file = directory.GetFileClient(fileName);
// Download the file
ShareFileDownloadInfo download = file.Download();
using (var sr = new StreamReader(download.Content))
using (JsonReader r = new JsonTextReader(sr))
{
//serialize it to POCO
var serializer = new JsonSerializer();
return serializer.Deserialize<StockInventory[]>(r);
}
}
using Azure.Storage.Files.Shares;
using Azure.Storage.Files.Shares.Models;
public void UploadToAzureFileShare(string json)
{
// Name of the share, directory, and file
string shareName = ConfigurationManager.AppSettings.Get("inRiver.StorageAccountShareReference");
string storageAccountName = ConfigurationManager.AppSettings.Get("inRiver.StorageAccountName");
string storageAccountKey = ConfigurationManager.AppSettings.Get("inRiver.StorageAccountKey");
string dirName = "datafeedwatchpublic";
string fileName = "file_from_episerver.json";
string connectionString = $"DefaultEndpointsProtocol=https;AccountName={storageAccountName};AccountKey={storageAccountKey};EndpointSuffix=core.windows.net";
// Get a reference from our share
ShareClient share = new ShareClient(connectionString, shareName);
// Get a reference from our directory - directory located on root level
ShareDirectoryClient directory = share.GetDirectoryClient(dirName);
if (!directory.Exists())
directory.Create();
// Get a reference to a subdirectory not located on root level
//directory = directory.GetSubdirectoryClient("A1");
// Get a reference to our file
ShareFileClient file = directory.GetFileClient(fileName);
// upload the file
byte[] bytes = Encoding.ASCII.GetBytes(json);
using (MemoryStream stream = new MemoryStream(bytes))
{
file.Create(stream.Length);
file.UploadRange(
new HttpRange(0, stream.Length),
stream);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment