Unable to upload to azure storage with a SAS Token from flutter

567

Since our Azure function is generating the User Delegation SAS token, we use a registered app on Azure as a service principal.

This problem was solved by assigning Storage Blob Data Contributor in addition to the Storage Blob Delegator role to the service principal to accommodate addition of a file.

Share:
567
DCodes
Author by

DCodes

Happy Coding ^^

Updated on December 24, 2022

Comments

  • DCodes
    DCodes over 1 year

    I am attempting to upload files/data to azure storage with a SAS Token. I was successfully able to achieve this without a SAS Token using the following method:

      sendToBlob (Uint8List bytes)async {
        var storage = AzureStorage.parse('DefaultEndpointsProtocol=https;AccountName=#;AccountKey=#==;EndpointSuffix=core.windows.net');
        await storage.putBlob('/test/2.txt',bodyBytes: bytes);
      }
    

    When I generated a SAS Token and took the connection string with it written in the following format, I get a parse error:

    "BlobEndpoint=https://#.blob.core.windows.net/;QueueEndpoint=https://#.queue.core.windows.net/;FileEndpoint=https://#.file.core.windows.net/;TableEndpoint=https://#.table.core.windows.net/;SharedAccessSignature=sv=#"
    

    Attempting to send a put request without a connection string, my method is as follows:

      uploadtoAzure(String sas)async{
        try {
          var url = 'https://#.blob.core.windows.net/test/4.txt?'+sas;
          Map<String, String> headers = ({"x-ms-blob-type": "BlockBlob"});
          final response = await http.put(
              url,
              headers: headers,
              body: 'hi'
          );
          print(response.statusCode);
        } on DioError catch (e) {
          if(e.response.statusCode == 404){
            print(e.message);
          }else{
            print(e.message);
          }
        }
      }
    

    Knowing that I'm the Owner and the Storage Blob Data Contributor of the storage account I'm uploading to and the SAS Token is not expired ,this leads to a 403 AuthorizationPermissionMismatch error.

    ¿<?xml version="1.0" encoding="utf-8"?><Error><Code>AuthorizationPermissionMismatch</Code><Message>This request is not authorized to perform this operation using this permission.
    RequestId:bc7f5bb4-c01e-0066-0459-9a6d45000000
    Time:2020-10-04T14:16:54.7302685Z</Message></Error>
    

    Is there a method to upload data from flutter to azure using a connection string with a SAS Token? and how would I be able to solve this issue?

    Any help is appreciated.