public async Task <string> GetFileshare() { // this resource does not have support for managed identities, see https://docs.microsoft.com/en-us/azure/active-directory/managed-identities-azure-resources/services-support-managed-identities var fileName = "test.txt"; var storageConnectionString = _configuration.GetConnectionString("StorageConnectionString"); var shareClient = new ShareClient(storageConnectionString, Environment.GetEnvironmentVariable("FileShareName")); if (await shareClient.ExistsAsync()) { // getting file from root var directory = shareClient.GetRootDirectoryClient(); if (await directory.ExistsAsync()) { var fileClient = directory.GetFileClient(fileName); if (!await fileClient.ExistsAsync()) { await UploadFile(fileClient, fileName); } var result = await ReadFileFromFileShare(fileClient, fileName); return(!string.IsNullOrEmpty(result) ? result : "Something went wrong. Check the logs for more information."); } } return("The fileshare does not exist."); }
// <snippet_CreateShare> //------------------------------------------------- // Create a file share //------------------------------------------------- public async Task CreateShareAsync(string shareName) { // <snippet_CreateShareClient> // Get the connection string from app settings string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"]; // Instantiate a ShareClient which will be used to create and manipulate the file share ShareClient share = new ShareClient(connectionString, shareName); // </snippet_CreateShareClient> // Create the share if it doesn't already exist await share.CreateIfNotExistsAsync(); // Ensure that the share exists if (await share.ExistsAsync()) { Console.WriteLine($"Share created: {share.Name}"); // Get a reference to the sample directory ShareDirectoryClient directory = share.GetDirectoryClient("CustomLogs"); // Create the directory if it doesn't already exist await directory.CreateIfNotExistsAsync(); // Ensure that the directory exists if (await directory.ExistsAsync()) { // Get a reference to a file object ShareFileClient file = directory.GetFileClient("Log1.txt"); // Ensure that the file exists if (await file.ExistsAsync()) { Console.WriteLine($"File exists: {file.Name}"); // Download the file ShareFileDownloadInfo download = await file.DownloadAsync(); // Save the data to a local file, overwrite if the file already exists using (FileStream stream = File.OpenWrite(@"downloadedLog1.txt")) { await download.Content.CopyToAsync(stream); await stream.FlushAsync(); stream.Close(); // Display where the file was saved Console.WriteLine($"File downloaded: {stream.Name}"); } } } } else { Console.WriteLine($"CreateShareAsync failed"); } }
// </snippet_CopyFileToBlob> // <snippet_CreateShareSnapshot> //------------------------------------------------- // Create a share snapshot //------------------------------------------------- public async Task CreateShareSnapshotAsync(string shareName) { // Get the connection string from app settings string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"]; // Instatiate a ShareServiceClient ShareServiceClient shareServiceClient = new ShareServiceClient(connectionString); // Instantiate a ShareClient which will be used to access the file share ShareClient share = shareServiceClient.GetShareClient(shareName); // Ensure that the share exists if (await share.ExistsAsync()) { // Create a snapshot ShareSnapshotInfo snapshotInfo = await share.CreateSnapshotAsync(); Console.WriteLine($"Snapshot created: {snapshotInfo.Snapshot}"); } }
// <snippet_UploadFileWithSas> // <snippet_SetMaxShareSize> //------------------------------------------------- // Set the maximum size of a share //------------------------------------------------- public async Task SetMaxShareSizeAsync(string shareName, int increaseSizeInGiB) { const long ONE_GIBIBYTE = 10737420000; // Number of bytes in 1 gibibyte // Get the connection string from app settings string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"]; // Instantiate a ShareClient which will be used to access the file share ShareClient share = new ShareClient(connectionString, shareName); // Create the share if it doesn't already exist await share.CreateIfNotExistsAsync(); // Ensure that the share exists if (await share.ExistsAsync()) { // Get and display current share quota ShareProperties properties = await share.GetPropertiesAsync(); Console.WriteLine($"Current share quota: {properties.QuotaInGB} GiB"); // Get and display current usage stats for the share ShareStatistics stats = await share.GetStatisticsAsync(); Console.WriteLine($"Current share usage: {stats.ShareUsageInBytes} bytes"); // Convert current usage from bytes into GiB int currentGiB = (int)(stats.ShareUsageInBytes / ONE_GIBIBYTE); // This line sets the quota to be the current // usage of the share plus the increase amount await share.SetQuotaAsync(currentGiB + increaseSizeInGiB); // Get the new quota and display it properties = await share.GetPropertiesAsync(); Console.WriteLine($"New share quota: {properties.QuotaInGB} GiB"); } }