<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Dan Franko on Medium]]></title>
        <description><![CDATA[Stories by Dan Franko on Medium]]></description>
        <link>https://medium.com/@dfranko?source=rss-b00ebf199c47------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*E7ztBis-oIhxtkX6</url>
            <title>Stories by Dan Franko on Medium</title>
            <link>https://medium.com/@dfranko?source=rss-b00ebf199c47------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 11:51:42 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dfranko/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Upload Attachments from ServiceNow to Azure Blob Storage]]></title>
            <link>https://medium.com/@dfranko/upload-attachments-from-servicenow-to-azure-blob-storage-3fe99ab3c9c2?source=rss-b00ebf199c47------2</link>
            <guid isPermaLink="false">https://medium.com/p/3fe99ab3c9c2</guid>
            <category><![CDATA[azure]]></category>
            <category><![CDATA[servicenow]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Dan Franko]]></dc:creator>
            <pubDate>Thu, 05 Dec 2019 21:31:11 GMT</pubDate>
            <atom:updated>2019-12-05T21:31:11.528Z</atom:updated>
            <content:encoded><![CDATA[<p>My main motivation for uploading documents from ServiceNow to Azure Blob storage was to be able to post public documents in a user friendly manner. You can create public knowledge records and attach files to them which makes the attachments themselves public, but it seems that clicking on these links will result in a file being downloaded as opposed to being opened natively in the browser. This is potentially solveable and I just haven’t come across the solution. Secondarily, I’m trying to learn more about Azure and how to integrate with it.</p><p>That said, here’s one way to upload attachments from ServiceNow to Azure Blob storage via REST. One of the more challenging things about this whole thing was figuring out the authentication. Smarter people have figured out that these various hoops you have to jump through are more secure that good ole basic auth, but it tends to make things more complicated. Below is the Azure documentation on how to do this using the shared key.</p><p><a href="https://docs.microsoft.com/en-us/rest/api/storageservices/authorize-with-shared-key">Authorize with Shared Key (REST API) - Azure Storage</a></p><p>Luckily instead of having to re-invent the wheel, someone at ServiceNow has done most of this work for us already. We can leverage the AzureRESTRequestSigningUtil script include which creates the Authorization header we need in our PUT request. However, it was missing some functionality needed to include some information in the header.</p><ol><li>Firstly the x-ms-blob-type header wasn’t included in the getHeadersToAuthorize method which is required when putting a blob.</li><li>Secondly I was using the setRequestBodyFromAttachment method from RESTMessageV2 and found that when it was trying to get the content length from request.getRequestBody() in getStorageAuthorization that it failed.</li></ol><p>To make this all work I created a new class that extended the class and made my own to support these needs. It looks like the below:</p><pre>var AzurePutRESTRequestSigningUtil = Class.create();<br>AzurePutRESTRequestSigningUtil.prototype = Object.extendsObject(sn_azure.AzureRESTRequestSigningUtil, {<br>  // --------------------------------------------------------------------------------<br>  // Perform shared key authentication<br>  // --------------------------------------------------------------------------------<br>  getStorageAuthorization: function(endpoint, method, request, accountName, subscriptionSysId, contentLength) {</pre><pre>// this is the raw representation of the message signature <br>    var headersToSort = this.getHeadersToAuthorize(request);<br>    var canonicalizedHeaders = this.getCanonicalHeadersString(headersToSort);<br>    var canonicalizedResource = this.getCanonicalResourceString(accountName, endpoint);<br>    var verb = method.toUpperCase();</pre><pre>var stringToSign = verb + &quot;\n&quot;;<br>    stringToSign += &quot;\n&quot;; //content-encoding<br>    stringToSign += &quot;\n&quot;; //content-languages</pre><pre>var s = ((contentLength == 0) ? &#39;&#39; : contentLength);<br>    stringToSign += s + &quot;\n&quot;; //content-length</pre><pre>var md5 = &#39;&#39;;<br>    stringToSign += md5 + &quot;\n&quot;; //content-md5</pre><pre>stringToSign += &quot;\n&quot;; //content-type<br>    stringToSign += &quot;\n&quot;; //date<br>    stringToSign += &quot;\n&quot;; //if modified since</pre><pre>var ifMatch = &#39;&#39;;<br>    stringToSign += ifMatch + &quot;\n&quot;; //if match</pre><pre>stringToSign += &quot;\n&quot;; //if non-match<br>    stringToSign += &quot;\n&quot;; //if unmodified since<br>    stringToSign += &quot;\n&quot;; //range<br>    stringToSign += canonicalizedHeaders;<br>    stringToSign += canonicalizedResource;</pre><pre>// generate the signature using HMAC-SHA256<br>    var signature = this.getSignature(stringToSign, this.getPrimaryKey(accountName, &#39;&#39; + subscriptionSysId), this.ALGORITHM);<br>    var authorization = &quot;SharedKey &quot; + accountName + &quot;:&quot; + signature;<br>    return authorization;<br>  },<br>  getHeadersToAuthorize: function(request) {<br>    var headers = [];</pre><pre>var msblob = {};<br>    msblob.name = &#39;x-ms-blob-type&#39;;<br>    msblob.value = &#39;BlockBlob&#39;;<br>    headers.push(msblob);</pre><pre>var msDate = {};<br>    msDate.name = &#39;x-ms-date&#39;;<br>    msDate.value = request.getRequestHeader ? request.getRequestHeader(msDate.name) : &#39;&#39;;<br>    if (gs.nil(msDate.value))<br>      msDate.value = this.getDateTimeString();</pre><pre>headers.push(msDate);</pre><pre>var msVersion = {};<br>    msVersion.name = &#39;x-ms-version&#39;;<br>    msVersion.value = request.getRequestHeader ? request.getRequestHeader(msVersion.name) : &#39;&#39;;<br>    if (gs.nil(msVersion.value))<br>      msVersion.value = this.getVersion();<br>    headers.push(msVersion);</pre><pre>this.addHeaderIfExists(headers, request, &#39;x-ms-copy-source&#39;);<br>    this.addHeaderIfExists(headers, request, &#39;x-ms-lease-action&#39;);<br>    return headers;<br>  },</pre><pre>type: &#39;AzurePutRESTRequestSigningUtil&#39;<br>});</pre><p>The main changes are passing in the content length to the getStorageAuthorization method and adding the x-ms-blob-type header in the headers to authorize. There’s probably a more elegant way to do this, but it works. The content-type header should probably also be included at some point.</p><p>You’ll need to setup a subscription under Azure Subscriptions. You will also need to setup or validate that you have an entry for your container in the azure_storage_accnts table that ties back to the container you’ll be putting or updating documents in. The sys_id of this record is the 5th argument to getStorageAuthorization.</p><p>Here’s an example of how you can call it. In my case I plan to use this in a workflow for a catalog item. The user is required to upload a document and then in the workflow or flow it would find that attachment record and then upload it to Azure.</p><pre>var attachment = new GlideRecord(&#39;sys_attachment&#39;);<br>attachment.get(&#39;a937d998dbeb2f84a150a4cb0b9619e8&#39;);<br>var endpoint = &#39;<a href="https://concertoservicenow.blob.core.windows.net/servicenowdocuments/&#39;">https://servicenow.blob.core.windows.net/servicenowdocuments/&#39;</a> + attachment.file_name;</pre><pre>var r = new sn_ws.RESTMessageV2(&#39;sn_azure.Microsoft Azure Storage Service&#39;, &#39;put&#39;);<br>r.setEndpoint(endpoint);<br>r.setRequestBodyFromAttachment(attachment.sys_id);</pre><pre>var auth = new sn_azure.AzurePutRESTRequestSigningUtil().getStorageAuthorization(endpoint,&#39;put&#39;,r,&#39;servicenow&#39;,&#39;05dd1b4d1b6d009868f5524d0d4bcbee&#39;,parseInt(attachment.size_bytes));</pre><pre>r.setRequestHeader(&#39;Authorization&#39;,auth);</pre><pre>var now = new Date();<br>var dt = new Date(now - 0 * 60000);</pre><pre>r.setRequestHeader(&#39;x-ms-date&#39;,dt.toGMTString());<br>r.setRequestHeader(&#39;x-ms-version&#39;,&#39;2014-02-14&#39;);<br>r.setRequestHeader(&#39;x-ms-blob-type&#39;,&#39;BlockBlob&#39;);<br>var response = r.execute();</pre><pre>if(response.getStatusCode() != 201){<br> gs.print(&quot;Something went wrong&quot;);<br>}</pre><p>This whole process ended up being much easier once I realized that ServiceNow had provided the basic components for performing the authentication. Let me know if you have any questions!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3fe99ab3c9c2" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>