从 Azure 文件共享下载和删除文件

Posted

技术标签:

【中文标题】从 Azure 文件共享下载和删除文件【英文标题】:Download and delete file from Azure file shares 【发布时间】:2021-12-06 09:42:32 【问题描述】:

我有以下代码可以将文件从 Azure 文件共享下载到本地,并且工作正常。

有什么办法,下载完成后可以删除文件吗?

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

static void Main(string[] args)

    // Get a connection string to our Azure Storage account.
    string connectionString = ConfigurationManager.AppSettings.Get("StorageConnectionString");
    
    // Get a reference to a share named "sample-share"
    ShareClient share = new ShareClient(connectionString, ConfigurationManager.AppSettings.Get("ShareNamed"));
    
    // Get a reference to a directory named "sample-dir"
    ShareDirectoryClient dir = share.GetDirectoryClient(ConfigurationManager.AppSettings.Get("SourceDirectory"));
    
    foreach (ShareFileItem item in dir.GetFilesAndDirectories())
    
        Console.WriteLine(item.Name);
         // Get a reference to a file named "sample-file" in directory "sample-dir"
        ShareFileClient file = dir.GetFileClient(item.Name);
    
        // Download the file
        ShareFileDownloadInfo download = file.Download();
                    
        using (FileStream stream = File.Open(ConfigurationManager.AppSettings.Get("DestinationDirectory") + item.Name, FileMode.Append))
        
            download.Content.CopyTo(stream);
            stream.FlushAsync();
            stream.Close();
        
    
    
                
    Console.ReadLine();

【问题讨论】:

@r2018 - 您共享的链接适用于存储 Blob,而 OP 正在寻找删除文件共享中的文件。 我删除它,只是为了避免混淆。我的错。 【参考方案1】:

读完流后,只需添加await file.DeleteAsync即可删除文件。

类似:

static void Main(string[] args)

    // Get a connection string to our Azure Storage account.
    string connectionString = ConfigurationManager.AppSettings.Get("StorageConnectionString");

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

    // Get a reference to a directory named "sample-dir"
    ShareDirectoryClient dir = share.GetDirectoryClient(ConfigurationManager.AppSettings.Get("SourceDirectory"));

    foreach (ShareFileItem item in dir.GetFilesAndDirectories())
    
        Console.WriteLine(item.Name);
        // Get a reference to a file named "sample-file" in directory "sample-dir"
        ShareFileClient file = dir.GetFileClient(item.Name);

        // Download the file
        ShareFileDownloadInfo download = file.Download();
        
        using (FileStream stream = File.Open(ConfigurationManager.AppSettings.Get("DestinationDirectory") + item.Name, FileMode.Append))
        
            download.Content.CopyTo(stream);
            stream.FlushAsync();
            stream.Close();
        
        await file.DeleteAsync();
    
    
    Console.ReadLine();

【讨论】:

以上是关于从 Azure 文件共享下载和删除文件的主要内容,如果未能解决你的问题,请参考以下文章

如何从 azure 文件共享中删除特定时间段之前的文件

如何使用 cli 从 Azure 文件共享中删除文件夹

如何从 azure 存储帐户中删除 azure 文件共享旧数据?

Azure Runbook 不删除文件共享中的文件

下载后如何从 azure blob 存储中删除文件

使用 Powershell 从 Azure 存储子容器中删除所有文件