【问题标题】:Error InvalidUri when upload file to Azure File Share将文件上传到 Azure 文件共享时出错 InvalidUri
【发布时间】:2021-12-28 05:14:20
【问题描述】:

我获得了如下地址,以便使用共享访问签名 (SAS) 将文件上传到 Azure 文件共享

https://myaccount.file.core.windows.net/xxxxx?sv=2020-08-04&ss=bfqt&srt=so&sp=rwdlacupitfx&se=2022-12-30T18:11:32Z&st=2021-12-12T10:11:32Z&spr=https&sig=signature

这是我的测试程序

using Azure.Storage.Files.Shares;

public async Task TestAsync()
{
    var sas = @"https://myaccount.file.core.windows.net/xxxxx?sv=2020-08-04&ss=bfqt&srt=so&sp=rwdlacupitfx&se=2022-12-30T18:11:32Z&st=2021-12-12T10:11:32Z&spr=https&sig=signature";
    var localfile = @"C:\Test\local.txt";
    
    var client = new ShareFileClient(new Uri(sas));
    using (var stream = new FileStream(localfile, FileMode.Open, FileAccess.Read))
    {
        var response = await client.UploadAsync(stream);
    }
}

程序抛出 RequestFailedException 并出现以下错误:

Status: 400 (The requested URI does not represent any resource on the server.)
ErrorCode: InvalidUri

Additional Information:
UriPath: /xxxxx

我的问题是这个错误是什么意思,我的测试代码有什么问题吗?

【问题讨论】:

    标签: c# azure-files


    【解决方案1】:

    根据这个Document,使用 SAS 上传是不可能的,因为它需要身份验证才能这样做。即使我们尝试正确使用代码,它仍然会抛出 Authentication information is not given in the correct format. Check the value of the Authorization header 异常。另一种方法是使用连接字符串,当我们尝试从一端进行复制时,该字符串工作正常。

    这里是代码

    static async Task Main(string[] args) {
      CloudStorageAccount storageAccount = CloudStorageAccount.Parse("<YOUR CONNECTION STRING>");
    
      CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
    
      CloudFileShare share = fileClient.GetShareReference("<YOUR FILE SHARE>");
    
      if (await share.ExistsAsync()) {
        CloudFileDirectory rootDir = share.GetRootDirectoryReference();
    
        CloudFile file = rootDir.GetFileReference("sample.txt");
    
        byte[] data = File.ReadAllBytes(@ "sample.txt");
        Stream fileStream = new MemoryStream(data);
        await file.UploadFromStreamAsync(fileStream);
      }
    }
    

    您可以尝试以下使用 SAS 的解决方法:

    using System.IO;
    using Azure.Storage;
    using Azure.Storage.Files.Shares;
    
    public class SampleClass {
      public void Upload() {
        ///Get information from your Azure storage account and configure
        string accountName = "{Get the account name from the Azure portal site.}";
        string accessKey = "{Get the access key from the Azure portal site.}";
        Uri serverurl = new Uri(@ "{Get the URL from the Azure portal.}");
    
        ///Upload destination(azure side)
        string azureDirectoryPath = @ "{Destination(azure side)Specify the directory of}";
        string azureFileName = "{Specify the file name to save}";
    
        ///Upload target(Local side)
        string localDirectoryPath = @ "{Upload target(Local side)Specify the directory of}";
        string localFileName = "{Upload target(Local side)Specify the file name of}";
    
        //SSL communication permission setting
        //If you don't do this, SSL(https)An error occurs in communication.
        System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls12;
    
        try {
          //Preparing to connect to Azure: Setting connection information
          StorageSharedKeyCredential credential = new StorageSharedKeyCredential(accountName, accessKey);
    
          //Connect to Azure
          ShareClient share = new ShareClient(serverurl, credential);
    
          ShareDirectoryClient directory = share.GetDirectoryClient(azureDirectoryPath);
    
          //Upload destination(azure side)Create if there is no folder in.
          directory.CreateIfNotExists();
    
          //Upload destination(azure side)Create a file instance in.
          ShareFileClient file = directory.GetFileClient(azureFileName);
    
          //Delete any file with the same name
          file.DeleteIfExists();
    
          //Open the Local file to be uploaded. It is easy to get binary information by opening it with FileStream type.
          FileStream stream = File.OpenRead(Path.Combine(localDirectoryPath, localFileName));
    
          //Upload destination(azure side)Inject binary information into a file instance
          file.Create(stream.Length);
          file.UploadRange(new Azure.HttpRange(0, stream.Length), stream);
    
          //Free local files
          stream.Dispose();
        } catch (Exception ex) {
          System.Console.WriteLine(ex.Message);
          return;
        }
      }
    }
    

    参考: How to programmatically upload files to Azure Storage (File Share)

    【讨论】:

    • 如果回答对您有帮助,请您接受它作为回答,以便遇到相同问题的其他人可以找到此解决方案并解决他们的问题
    猜你喜欢
    • 2021-01-10
    • 2020-09-26
    • 2021-05-03
    • 2021-12-05
    • 2018-10-16
    • 1970-01-01
    • 2016-03-27
    • 2018-08-09
    • 1970-01-01
    相关资源
    最近更新 更多