"The x-ms-tags header is formatted incorrectly" when Assigning Multiple Blob Tags via "Put Blob" HTTP request

Viewed 46

I have created an HTTP Put request to Azure Blob Storage which I have successfully run and managed to add a single Blob index tag using the following headers for "x-ms-tags"...

{
  "Content-length": "0",
  "content-type": "application/pdf",
  "x-ms-version": "2020-04-08",
  "x-ms-blob-content-disposition": "attachment; filename=\"test1.pdf\"",
  "Date": "7/15/2022 11:53 AM",
  "x-ms-blob-type": "BlockBlob",
  "x-ms-tags": "blobtag=value"
}

Blob Object Result:

enter image description here

However, I have a requirement to add multiple tags to a blob object via this PUT Blob request. Reading the MS documentation I should be able to achieve this by modifying the "x-ms-tags" header. This is what I tried...

{
  "Content-length": "0",
  "content-type": "application/pdf",
  "x-ms-version": "2020-04-08",
  "x-ms-blob-content-disposition": "attachment; filename=\"test1.pdf\"",
  "Date": "7/15/2022 11:50 AM",
  "x-ms-blob-type": "BlockBlob",
  "x-ms-tags": "\"Project\"='Contoso'\"test1\"='1'"
} 

Unfortunately, this method returns an error for the "x-ms-tags" header value.

TagsHeaderInvalidFormatThe x-ms-tags header is formatted incorrectly.

Any advice on this challenge would be appreciated. Thank you for your time.

1 Answers

The documentation is incorrect. You would need to include & as tag separator.

Please try the following:

"x-ms-tags": "Project=Contoso&test1=1"

This is how JavaScript SDK for Azure Storage is doing it:

export function toBlobTagsString(tags?: Tags): string | undefined {
  if (tags === undefined) {
    return undefined;
  }

  const tagPairs = [];
  for (const key in tags) {
    if (Object.prototype.hasOwnProperty.call(tags, key)) {
      const value = tags[key];
      tagPairs.push(`${encodeURIComponent(key)}=${encodeURIComponent(value)}`);
    }
  }

  return tagPairs.join("&");
}
Related