DiaGea
DiaGea

Reputation: 99

Create file on azure file storage

I have created file storage in Azure, can access it and verify the directory exists. How can I create a file in the cloud directory and write to it?

Upvotes: 3

Views: 10918

Answers (2)

Slava Fesenko
Slava Fesenko

Reputation: 21

General info, how to work with an Azure file storage: https://learn.microsoft.com/en-us/azure/storage/files/storage-dotnet-how-to-use-files How to create file and add some text: https://www.youtube.com/watch?v=HnkqqLOOnR4

// Create a CloudFileClient object for credentialed access to Azure Files.
CloudFileClient fileClient = storageAccount.CreateCloudFileClient();

// Get a reference to the file share we created previously.
CloudFileShare share = fileClient.GetShareReference("logs");

// Ensure that the share exists.
if (share.Exists())
{
    // Get a reference to the root directory for the share.
    CloudFileDirectory rootDir = share.GetRootDirectoryReference();

    // Get a reference to the directory we created previously.
    CloudFileDirectory sampleDir = rootDir.GetDirectoryReference("CustomLogs");

    // Ensure that the directory exists.
    if (sampleDir.Exists())
    {
        // Get a reference to the file we created previously.
        CloudFile file = sampleDir.GetFileReference("Log1.txt");

        // Ensure that the file exists.
        if (file.Exists())
        {
            // Write the contents of the file to the console window.
            Console.WriteLine(file.DownloadTextAsync().Result);
        }
    }
}

Upvotes: 2

Ali Baig
Ali Baig

Reputation: 3867

If you are looking for a solution to upload file to Azure container in C#, here's a function I use as a utility to upload files to Azure

public static string UploadBlob(string blobContainerName, string key, Stream sourceStrem, string contentType)
{
    //getting the storage account
    string uri = null;
    try
    {
        blobContainerName = blobContainerName.ToLowerInvariant();
        string azureStorageAccountConnection =
            ConfigurationManager.ConnectionStrings["AzureStorageAccount"].ConnectionString;
        CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(azureStorageAccountConnection);
        CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();

        CloudBlobContainer container = cloudBlobClient.GetContainerReference(blobContainerName);
        container.CreateIfNotExists();

        CloudBlockBlob blob = container.GetBlockBlobReference(key);
        blob.Properties.ContentType = contentType;

        blob.UploadFromStream(sourceStrem);
        uri = blob.Uri.ToString();
    }
    catch (Exception exception)
    {
        if (_logger.IsErrorEnabled)
            _logger.Error(exception.Message, exception);
    }
    return uri;
}

Where blobContainerName is your container on Azure, key is the name of the file with which you want to store this blob, third parameter is the Stream of file and last one is content type.

Hope it helps

Upvotes: 1

Related Questions