2 years ago

#35553

test-img

NNassar

storageAccount.CreateCloudFileClient VS URL for saving to Azure Storage folder

is there a better way to insert files into Azure File Share? I'm using rootDirectory.GetDirectoryReference("MaineFolder") but I would like to do its use a URL/URI to insert the files created in code(C#) into Azure File Share folder. Here's my code, is there a better, dynamic way of saving files to Azure File Share Folder?

        const string storageAccountName = "TestAccount";
        const string storageAccountKey = "AccountKey";
        var storageAccount =
            new CloudStorageAccount(
                new StorageCredentials(storageAccountName, storageAccountKey), true);

        //See if FileShare folder is there
        var share = storageAccount.CreateCloudFileClient().GetShareReference("MainFolder");
        share.CreateIfNotExists();

        //Get a reference to the root of the FileShare folder
        var rootDirectory = share.GetRootDirectoryReference();
        
        //Get a reference to the next level folder
        var folder1 = rootDirectory.GetDirectoryReference("Folder1");
        folder1.CreateIfNotExists();
        
        //Get a reference to the next level folder
        var pendingFolder = rootDirectory.GetDirectoryReference("Pending");
        pendingFolder.CreateIfNotExists();

        pendingFolder.GetFileReference("tested.txt").UploadText("Testing CreateCloudFileClient");

Based on what I've been reading, I think this is what it would kind of look like.

            var adlsClient = AdlsClient.CreateClient("myDataLakeAccount.azuredatalakestore.net", "Token");
            
            using MemoryStream memoryStream = new MemoryStream();
            using StreamWriter streamWriter = new StreamWriter(memoryStream);

            streamWriter.WriteLine("Testing file content to insert.");

            using var file = adlsClient.CreateFile("/Folder1/Folder2/Pending/TestFile.txt", IfExists.Overwrite);
            byte[] textByteArray = memoryStream.ToArray();
            file.Write(textByteArray, 0, textByteArray.Length);

c#

azure

visual-studio

azure-files

azure-storage-files

0 Answers

Your Answer

Accepted video resources