Set metadata in REST request to put blob in AZURE

2019-09-05 20:15发布

问题:

i am able to upload file to azure blob using REST api provided by Azure.

i want to set metadata at the time i am doing request for put blob, when i am setting it into header as shown here i am unble to upload file and getting following exception org.apache.http.client.ClientProtocolException.

from the last line of the code below

    HttpPut req = new HttpPut(uri);
    req.setHeader("x-ms-blob-type", blobType);
    req.setHeader("x-ms-date", date);
    req.setHeader("x-ms-version", storageServiceVersion);
    req.setHeader("x-ms-meta-Cat", user);
    req.setHeader("Authorization", authorizationHeader);

    HttpEntity entity = new InputStreamEntity(is,blobLength);
    req.setEntity(entity);

    HttpResponse response = httpClient.execute(req);

regarding the same, i have two questions.

  1. can setting different metadata, avoid overwriting of file? See my question for the same here

  2. if Yes for first question, how to set metadata in REST request to put blob into Azure?

please help

回答1:

So a few things are going here.

Regarding the error you're getting, it is because you're not adding your metadata header when calculating authorization header. Please read Constructing the Canonicalized Headers String section here: http://msdn.microsoft.com/en-us/library/windowsazure/dd179428.aspx.

Based on this, you would need to change the following line of code (from your blog post)

String canonicalizedHeaders = "x-ms-blob-type:"+blobType+"\nx-ms-date:"+date+"\nx-ms-version:"+storageServiceVersion;

to

String canonicalizedHeaders = "x-ms-blob-type:"+blobType+"\nx-ms-date:"+date+"\nx-ms-meta-cat"+user+"\nx-ms-version:"+storageServiceVersion;

(Note: I have just made these changes in Notepad so they may not work. Please go to the link I mentioned above for correctly creating the canonicalized headers string.

can setting different metadata, avoid overwriting of file?

Not sure what you mean by this. You can update metadata of a blob by performing Set Blob Metadata operation on a blog.