简体   繁体   中英

Error InvalidUri when upload file to Azure File Share

I was given the address like following to upload file to Azure File Share using Shared Access Signature(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

This is my test program

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);
    }
}

The program throw RequestFailedException with following error:

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

Additional Information:
UriPath: /xxxxx

My question is what this error mean, is it anything wrong in my test code?

According to this Document uploading with SAS is not possible as it requires authentication to do so. Even though we try having the code correctly it still throws Authentication information is not given in the correct format. Check the value of the Authorization header Authentication information is not given in the correct format. Check the value of the Authorization header exception. An alternative way is to use a connection string which of working fine when we try reproducing from our end.

Here is the code

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);
  }
}

Here is the workaround using SAS that you can try:

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;
    }
  }
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM