-1

When I try to call this https://learn.microsoft.com/en-us/rest/api/storageservices/get-blob-metadata api in microsoft flow I always get this error with 400 bad request.

I edited my authorization header regarding this answer https://stackoverflow.com/a/22029178/10389562 but couldn't get what am I doing wrong.

Method: GET
Uri: https://myaccount.blob.core.windows.net/containername/blobname?comp=metadata
Headers :
{
  "Authorization": "SharedKey storageaccountname: primary key in the storage 
   account properties",
  "x-ms-date": "Thu, 21 Sep 2018 23:45:00 GMT",
  "x-ms-version": "2018-03-28"
}

After call this API I got this output

<?xml version="1.0" encoding="utf-8"?><Error> 
<Code>InvalidAuthenticationInfo</Code><Message>Authentication information is 
not given in the correct format. Check the value of Authorization header.
RequestId:f3b3051b-601e-00a4-4b3c-51c58d000000
Time:2018-09-20T23:46:40.6659210Z</Message></Error>

Thanks for any help

Jerry Liu
  • 17,282
  • 4
  • 40
  • 61

1 Answers1

0

Update

In Microsoft Flow, calling Rest Api against Azure Storage seems not a valid way. Authorization needs x-ms-* headers sent by the flow(like x-ms-tracking-id,x-ms-workflow-id,etc.) adding to stringStr, which is not under our control. What's more, signature is only valid for 15m since generated.

There's a built-in Get Blob Metadata action. And for Storage, other common actions are available as well.

To set blob metadata, I suggest to host the logic in Azure function.

  1. Follow this tutorial to create Function app and a httptrigger function, remember to choose the Storage Account where we need to set blob metadata.

  2. Replace the httptrigger sample with code below, and modify metadataName to what you need.

    #r "Microsoft.WindowsAzure.Storage"
    
    using System.Net;
    using Microsoft.WindowsAzure.Storage;
    using Microsoft.WindowsAzure.Storage.Blob;
    
    public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
    {
        dynamic data = await req.Content.ReadAsAsync<object>();
    
        if (data == null)
        {
            return req.CreateResponse(HttpStatusCode.BadRequest, "No request body posted");
        }
        else
        {
            string metadata = data.metadata;
            string blobName = data.blobName;
            string containerName = data.containerName;
    
            try
            {
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(Environment.GetEnvironmentVariable("AzureWebJobsStorage"));
                CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
                CloudBlobContainer blobContainer = blobClient.GetContainerReference(containerName);
                CloudBlob blob = blobContainer.GetBlobReference(blobName);
    
                blob.Metadata.Add("metadataName", metadata);
                blob.SetMetadata();
            }
            catch (Exception e)
            {
                log.Error(e.ToString());
                return req.CreateResponse(HttpStatusCode.InternalServerError, "Fail to set metadata");
            }
    
            return (string.IsNullOrEmpty(metadata) || string.IsNullOrEmpty(blobName) || string.IsNullOrEmpty(containerName))
                ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass necessary parameters in the request body")
                : req.CreateResponse(HttpStatusCode.OK, $"Metadata of {blobName} has been set");
        }
    }
    
  3. In Microsoft Flow, create a Http action, post content below to the function url got in step 2.

    {
        "metadata": "test",
        "blobName":"myblob",
        "containerName":"mycontainer"
    }
    
Jerry Liu
  • 17,282
  • 4
  • 40
  • 61
  • Thank you for answer Jerry. I created the signature in the way that you showed but I'm still getting same error. And I confused a bit because is this signature can be used always by creating just ones? If not how will I create it dynamically in the flow ? – Serenay KIZILARSLAN Sep 21 '18 at 05:58
  • @SerenayKIZILARSLAN Sorry for my mistakes, in microsoft flow, this authorization needs several other headers adding to stringStr, which is not under our control. And the signature is only valid for 15m, so calling this rest api in the flow seems not a good idea. Why not try the built-in `Get Blob Metadata` action? – Jerry Liu Sep 21 '18 at 06:51
  • Yes I can use this action but afterwards I'll call https://learn.microsoft.com/en-us/rest/api/storageservices/set-blob-metadata to set metada and I need Authorization headers again. Before using this api I just want to check whether I can get metadata with api call, that's why I tried.. – Serenay KIZILARSLAN Sep 21 '18 at 07:19
  • @SerenayKIZILARSLAN Does my suggestion work or it's not an ideal option for you? – Jerry Liu Sep 26 '18 at 01:06
  • Thank you for your help @JerryLiu your suggestion looks like a very reasonable approach. I believe it's likely the answer. However I cannot test it now because this task has been removed from my assignments. – Serenay KIZILARSLAN Oct 13 '18 at 14:04