Klientská knihovna sdílených složek Azure Storage pro .NET – verze 12.13.1
Verze serveru: 2021-02-12, 2020-12-06, 2020-10-02, 2020-08-04, 2020-06-12, 2020-04-08, 2020-02-10, 2019-12-12, 2019-07-07 a 2019-02-02-02
Sdílené složky Azure nabízejí plně spravované sdílené složky v cloudu, které jsou přístupné prostřednictvím standardního protokolu SMB (Server Message Block). Sdílené složky Azure je možné připojit současně do cloudových i místních nasazení systémů Windows, Linux a macOS. Sdílené složky Azure je navíc možné ukládat do mezipaměti ve Windows Serveru pomocí Synchronizace souborů Azure, aby byly rychle přístupné poblíž místa, kde se data používají.
Zdrojový kód | Balíček (NuGet) | Referenční dokumentace k | rozhraní API Dokumentace k | rozhraní REST API Dokumentace k produktu
Začínáme
Instalace balíčku
Nainstalujte klientskou knihovnu sdílených složek Azure Storage pro .NET pomocí NuGetu:
dotnet add package Azure.Storage.Files.Shares
Požadavky
K použití tohoto balíčku potřebujete předplatné Azure a účet úložiště .
K vytvoření nového účtu úložiště můžete použít Azure Portal, Azure PowerShell nebo Azure CLI. Tady je příklad použití Azure CLI:
az storage account create --name MyStorageAccount --resource-group MyResourceGroup --location westus --sku Standard_LRS
Klíčové koncepty
Sdílené složky Azure lze použít k těmto činnostem:
- Zcela nahradit nebo doplnit tradiční místní souborové servery nebo zařízení NAS.
- "Lift and shift" aplikace do cloudu, které očekávají, že sdílená složka bude ukládat souborová aplikace nebo uživatelská data.
- Zjednodušte nové projekty vývoje cloudu pomocí sdílených nastavení aplikací, sdílených složek diagnostiky a sdílených složek nástroje pro vývoj, testování a ladění.
Bezpečnost vlákna
Zaručujeme, že všechny metody instance klienta jsou bezpečné pro přístup z více vláken a nezávislé na sobě (pokyny). Tím se zajistí, že doporučení opakovaného použití instancí klienta bude vždy bezpečné, a to i napříč vlákny.
Další koncepty
Možnosti | klienta Přístup k odpovědi | Dlouhotrvající operace | Zpracování selhání | Diagnostika | Zesměšňovat | Životnost klienta
Příklady
Vytvoření sdílené složky a nahrání souboru
// 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>";
// Name of the share, directory, and file we'll create
string shareName = "sample-share";
string dirName = "sample-dir";
string fileName = "sample-file";
// Path to the local file to upload
string localFilePath = @"<path_to_local_file>";
// Get a reference to a share and then create it
ShareClient share = new ShareClient(connectionString, shareName);
share.Create();
// Get a reference to a directory and create it
ShareDirectoryClient directory = share.GetDirectoryClient(dirName);
directory.Create();
// Get a reference to a file and upload it
ShareFileClient file = directory.GetFileClient(fileName);
using (FileStream stream = File.OpenRead(localFilePath))
{
file.Create(stream.Length);
file.UploadRange(
new HttpRange(0, stream.Length),
stream);
}
Stažení souboru
string connectionString = "<connection_string>";
// Name of the share, directory, and file we'll download from
string shareName = "sample-share";
string dirName = "sample-dir";
string fileName = "sample-file";
// Path to the save the downloaded file
string localFilePath = @"<path_to_local_file>";
// Get a reference to the file
ShareClient share = new ShareClient(connectionString, shareName);
ShareDirectoryClient directory = share.GetDirectoryClient(dirName);
ShareFileClient file = directory.GetFileClient(fileName);
// Download the file
ShareFileDownloadInfo download = file.Download();
using (FileStream stream = File.OpenWrite(localFilePath))
{
download.Content.CopyTo(stream);
}
Procházení sdílené složky
// Connect to the share
string connectionString = "<connection_string>";
string shareName = "sample-share";
ShareClient share = new ShareClient(connectionString, shareName);
// Track the remaining directories to walk, starting from the root
var remaining = new Queue<ShareDirectoryClient>();
remaining.Enqueue(share.GetRootDirectoryClient());
while (remaining.Count > 0)
{
// Get all of the next directory's files and subdirectories
ShareDirectoryClient dir = remaining.Dequeue();
foreach (ShareFileItem item in dir.GetFilesAndDirectories())
{
// Print the name of the item
Console.WriteLine(item.Name);
// Keep walking down directories
if (item.IsDirectory)
{
remaining.Enqueue(dir.GetSubdirectoryClient(item.Name));
}
}
}
Asynchronní rozhraní API
Plně podporujeme synchronní i asynchronní rozhraní API.
string connectionString = "<connection_string>";
// Name of the share, directory, and file we'll download from
string shareName = "sample-share";
string dirName = "sample-dir";
string fileName = "sample-file";
// Path to the save the downloaded file
string localFilePath = @"<path_to_local_file>";
// Get a reference to the file
ShareClient share = new ShareClient(connectionString, shareName);
ShareDirectoryClient directory = share.GetDirectoryClient(dirName);
ShareFileClient file = directory.GetFileClient(fileName);
// Download the file
ShareFileDownloadInfo download = await file.DownloadAsync();
using (FileStream stream = File.OpenWrite(localFilePath))
{
await download.Content.CopyToAsync(stream);
}
Řešení potíží
Všechny operace služby Sdílené složky Azure Storage vyvolají výjimku RequestFailedException při selhání s užitečnými ErrorCode
funkcemi. Mnoho z těchto chyb je možné obnovit.
// Connect to the existing share
string connectionString = "<connection_string>";
string shareName = "sample-share";
ShareClient share = new ShareClient(connectionString, shareName);
try
{
// Try to create the share again
share.Create();
}
catch (RequestFailedException ex)
when (ex.ErrorCode == ShareErrorCode.ShareAlreadyExists)
{
// Ignore any errors if the share already exists
}
Další kroky
Začínáme s našimi ukázkami souborů:
- Hello World: Nahrávání souborů, stahování souborů a procházení sdílených složek (nebo asynchronně)
- Ověřování: Ověřování pomocí připojovacích řetězců, sdílených klíčů a sdílených přístupových podpisů
Přispívání
Podrobnosti o sestavování, testování a přispívání do této knihovny najdete na CONTRIBUTING.md úložiště .
Tento projekt vítá příspěvky a návrhy. Většina příspěvků vyžaduje souhlas s licenční smlouvou s přispěvatelem (CLA), která stanoví, že máte právo udělit nám práva k používání vašeho příspěvku a skutečně tak činíte. Podrobnosti najdete na cla.microsoft.com.
Tento projekt přijal pravidla chování pro Microsoft Open Source. Další informace najdete v nejčastějších dotazech k pravidlům chování nebo se obraťte na opencode@microsoft.com případné další dotazy nebo komentáře.