Skip to content

Instantly share code, notes, and snippets.

@LucGosso
Created April 6, 2022 09:21
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/8671e01f0ced2fa189d7ee95a12586e9 to your computer and use it in GitHub Desktop.
Save LucGosso/8671e01f0ced2fa189d7ee95a12586e9 to your computer and use it in GitHub Desktop.
using Azure.Storage.Blobs;
public string UploadJsonToAzureBlobStorage(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");
//Create a unique name for the container
string containerName = "datafeedwatch";
string fileName = "items.json";
string connectionString = $"DefaultEndpointsProtocol=https;AccountName={storageAccountName};AccountKey={storageAccountKey};EndpointSuffix=core.windows.net";
// Create a BlobServiceClient object which will be used to create a container client
BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
// Create the container and return a container client object
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
containerClient.CreateIfNotExists();
// Get a reference to our file
BlobClient file = containerClient.GetBlobClient(fileName);
file.DeleteIfExists();
// upload the file
byte[] bytes = Encoding.ASCII.GetBytes(json);
using (MemoryStream stream = new MemoryStream(bytes))
{
file.Upload(stream);
}
return file.Uri.ToString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment