Skip to content

Latest commit

 

History

History
225 lines (175 loc) · 8.44 KB

File metadata and controls

225 lines (175 loc) · 8.44 KB

Azure Storage Files client library for .NET

Server Version: 2019-02-02

Azure Files offers fully managed file shares in the cloud that are accessible via the industry standard Server Message Block (SMB) protocol. Azure file shares can be mounted concurrently by cloud or on-premises deployments of Windows, Linux, and macOS. Additionally, Azure file shares can be cached on Windows Servers with Azure File Sync for fast access near where the data is being used.

Source code | Package (NuGet) | API reference documentation | REST API documentation | Product documentation

Getting started

Install the package

Install the Azure Storage Files client library for .NET with NuGet:

dotnet add package Azure.Storage.Files --version 12.0.0-preview.3

Prerequisites

You need an Azure subscription and a Storage Account to use this package.

To create a new Storage Account, you can use the Azure Portal, Azure PowerShell, or the Azure CLI. Here's an example using the Azure CLI:

az storage account create --name MyStorageAccount --resource-group MyResourceGroup --location westus --sku Standard_LRS

Key concepts

Azure file shares can be used to:

  • Completely replace or supplement traditional on-premises file servers or NAS devices.
  • "Lift and shift" applications to the cloud that expect a file share to store file application or user data.
  • Simplify new cloud development projects with shared application settings, diagnostic shares, and Dev/Test/Debug tool file shares.

Examples

Create a share and upload a file

using Azure.Storage;
using Azure.Storage.Files;
using Azure.Storage.Files.Models;

// Get a connection string to our Azure Storage account.  You can
// obtain your connection string from the Azure Portal (click
// Access Keys under Settings in the Portal Storage account blade)
// or using the Azure CLI with:
//
//     az storage account show-connection-string --name <account_name> --resource-group <resource_group>
//
// And you can provide the connection string to your application
// using an environment variable.
string connectionString = "<connection_string>";

// Get a reference to a share named "sample-share" and then create it
ShareClient share = new ShareClient(connectionString, "sample-share");
share.Create();

// Get a reference to a directory named "sample-dir" and then create it
DirectoryClient directory = share.GetDirectoryClient("sample-dir");
directory.Create();

// Get a reference to a file named "sample-file" in directory "sample-dir"
FileClient file = directory.GetFileClient("sample-file");

// Upload the file
using (FileStream stream = File.OpenRead("local-file.txt"))
{
    file.Create(stream.Length);
    file.UploadRange(
        FileRangeWriteType.Update,
        new HttpRange(0, stream.Length),
        stream);
}

Download a file

// Get a connection string to our Azure Storage account.
string connectionString = "<connection_string>";

// Get a reference to a share named "sample-share"
ShareClient share = new ShareClient(connectionString, "sample-share");

// Get a reference to a directory named "sample-dir"
DirectoryClient directory = share.GetDirectoryClient("sample-dir");

// Get a reference to a file named "sample-file" in directory "sample-dir"
FileClient file = directory.GetFileClient("sample-file");

// Download the file
StorageFileDownloadInfo download = file.Download();
using (FileStream stream = File.OpenWrite("downloaded-file.txt"))
{
    download.Content.CopyTo(stream);
}

Traverse a share

// Get a connection string to our Azure Storage account.
string connectionString = "<connection_string>";

// Get a reference to a share named "sample-share"
ShareClient share = new ShareClient(connectionString, "sample-share");

// Track the remaining directories to walk, starting from the root
Queue<DirectoryClient> remaining = new Queue<DirectoryClient>();
remaining.Enqueue(share.GetRootDirectoryClient());
while (remaining.Count > 0)
{
    // Get all of the next directory's files and subdirectories
    DirectoryClient dir = remaining.Dequeue();
    foreach (StorageFileItem item in dir.GetFilesAndDirectories())
    {
        Console.WriteLine(item.Name);

        // Keep walking down directories
        if (item.IsDirectory)
        {
            remaining.Enqueue(dir.GetSubdirectoryClient(item.Name));
        }
    }
}

Async APIs

We fully support both synchronous and asynchronous APIs.

string connectionString = "<connection_string>";
ShareClient share = new ShareClient(connectionString, "sample-share");
DirectoryClient directory = share.GetDirectoryClient("sample-dir");
FileClient file = directory.GetFileClient("sample-file");

// Download the file
StorageFileDownloadInfo download = await file.DownloadAsync();
using (FileStream stream = File.OpenWrite("downloaded-file.txt"))
{
    await download.Content.CopyToAsync(stream);
}

Troubleshooting

All Azure Storage File service operations will throw a StorageRequestFailedException on failure with helpful ErrorCodes. Many of these errors are recoverable.

// Get a connection string to our Azure Storage account
string connectionString = "<connection_string>";

// Try to create a share named "sample-share" and avoid any potential race
// conditions that might arise by checking if the share exists before creating
ShareClient share = new ShareClient(connectionString, "sample-share");
try
{
    share.Create();
}
catch (StorageRequestFailedException ex)
    when (ex.ErrorCode == FileErrorCode.ShareAlreadyExists)
{
    // Ignore any errors if the share already exists
}

Next steps

Get started with our File samples:

  1. Hello World: Upload files, download files, and traverse shares (or asynchronously)
  2. Auth: Authenticate with connection strings, shared keys, and shared access signatures.

Contributing

See the Storage CONTRIBUTING.md for details on building, testing, and contributing to this library.

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit cla.microsoft.com.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

Impressions