Sidebar

Has anyone passed a file into azure blob storage using a web service connection?

0 votes
358 views
asked Feb 17, 2020 by scott-b-8720 (560 points)
I'm attempting to use a SAS key to log into an azure blob storage container to PUT a blob there.  Anyone ever done that before?

2 Answers

0 votes
 
Best answer

Here is how this is done with a text file.  Still working on making it happen with a binary file.

function setDateTimeToUtc() {
   var deducedDate = qie.deduceDate(qie.getSystemDate());
   var date = new java.util.Date(deducedDate.getTime());
   var utc_format = new java.text.SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss");
   utc_format.setTimeZone(java.util.TimeZone.getTimeZone("UTC"));
   return utc_format.format(date);
}

function getHMAC256(key, valueToEncode) {
   var sha256_HMAC = null;
   var hash = null;
   try {
      sha256_HMAC = javax.crypto.Mac.getInstance("HmacSHA256");
      var theValue = new java.lang.String(valueToEncode);
      var secret_key = new javax.crypto.spec.SecretKeySpec(qie.base64DecodeToBytes(key), "HmacSHA256");
      sha256_HMAC.init(secret_key);

      hash = qie.base64EncodeBytes(sha256_HMAC.doFinal(theValue.getBytes("UTF-8")));

   } catch (e1) {
      throw e1;
   }
   return hash;
}

var blobName = qie.getFileName() + "." + qie.getUUID(true) + ".txt";
var container = <your blob container>;
var accountName = <your blob account>;
var requestMethod = "PUT";
var urlPath = container + "/" + blobName;
var storageServiceVersion = "2015-12-11";
var theDate = setDateTimeToUtc() + " GMT";
var content = source.toString();
var blobType = "BlockBlob";
var canonicalizedResource = "/" + accountName + "/" + urlPath;
var canonicalizedHeaders = "x-ms-blob-type:" + blobType + "\nx-ms-date:" + theDate + "\nx-ms-version:" + storageServiceVersion + "\n";
var accountKey = <your secret key>;

var tokenParameterMap = qie.newParameterMap();
tokenParameterMap.put("http.header.x-ms-version", storageServiceVersion);
tokenParameterMap.put("http.header.x-ms-date", theDate);
tokenParameterMap.put("http.header.x-ms-blob-type", blobType);

var stringToSign = requestMethod + "\n" +
   "\n" + //Content Encoding
   "\n" + //Content Language
   StringUtils.length(content) + "\n" + //Content Length
   "\n" + //Content MD5
   "text/plain; charset=UTF-8" + "\n" + //Content Type
   "\n" + //Date
   "\n" + //If - Modified - Since
   "\n" + //If - Match
   "\n" + //If - None - Match
   "\n" + //If - Unmodified - Since
   "\n" + //Range +
   canonicalizedHeaders + canonicalizedResource;
qie.info(stringToSign);

var theToken = getHMAC256(accountKey, stringToSign);

tokenParameterMap.put("http.header.Authorization", "SharedKey " + accountName + ":" + theToken);

var urlTemplate = "https://" + accountName + ".blob.core.windows.net/" + urlPath;
var tokenWSCall = qie.callRESTWebService(
   "CompScorecardBlobStorage",
   urlTemplate,
   "PUT",
   content,
   "text/plain",
   tokenParameterMap,
   60000);

qie.info(tokenWSCall);

answered Feb 28, 2020 by scott-b-8720 (560 points)
selected Mar 2, 2020 by scott-b-8720
0 votes
We are not aware of anyone doing this to date.  However, in reviewing the API documentation for the Azure SAS handling, this is something that we can do for sure.  Go ahead and give our support a call and we can get this up and running for you.
answered Feb 18, 2020 by ben-s-7515 (11,100 points)
...