STARLIMS + Azure Blob containers through SAS

Here is a funny story.

Here I was trying, with the infra team, to access my Azure container through SAS. STARLIMS has a built-in Azure container support, but it relies on a connection string with account information and all. But, like most Azure customers, that is not our reality. We use shared containers, so we need a SAS token… Which is not supported to configure as a STARLIMS connection string.

This means that next step will be any other web service consumption instead of direct containers access. Is it complex? Less than I expected!

Step 1: let’s get a SAS token!

Now, finding the said token is not always obvious, but mine looked something like this:

sv=nnnn-nn-nn&sr=c&si=CompanyaccessPolicy&sig=someuglystring

Hopefully, yours too! In the Azure Container tool, look for the “Shared Access Signature”, it’s the same thing.

Step 2 – integrate the Azure API!

Now, how do we put files there? The connection string and tutorials on STARLIMS will not help… But the web services will! All we need to do is write a UploadToAzureBlob procedure and a DownloadFromAzureBlob procedure (both in SSL) and that will do the trick:

:PROCEDURE UploadToAzureBlob;
:PARAMETERS content, fileName;

:DECLARE    sasToken, storageAccount, containerName, method, sampleContent,
            contentLength, requestUri, oWebService, oClient, oRequest, innerRequest,
            stream, resp, encoding;

encoding := LimsNetConnect("", "System.Text.Encoding",, .T.);


sasToken := "yourSASToken";
storageAccount := "yourAccountName";
containerName := "yourContainerName";
blobName := fileName; /* can be something like folder/subfolder/name.ext ;

method := "PUT";
sampleContent := content;
contentLength := encoding:UTF8:GetByteCount(sampleContent);

requestUri := Replace(Replace(Replace(Replace(
                    "https://{storageAccount}.blob.core.windows.net/{containerName}/{blobName}?{sasToken}",
                        "{storageAccount}", storageAccount),
                        "{containerName}", containerName),
                        "{blobName}", blobName),
                        "{sasToken}", sasToken);

oWebService := WebServices{};
oClient := oWebService:CreateHttpClient();
oRequest := oClient:CreateHttpRequest(requestUri);

oRequest:Method := method;
oRequest:ContentType := "text/plain; charset=UTF-8";
oRequest:ContentLength := contentLength;

innerRequest := DoProc("GetInnerRequest", { oRequest });
innerRequest:Headers:Add("x-ms-blob-type", "BlockBlob");
stream := innerRequest:GetRequestStream();
stream:Write(encoding:UTF8:GetBytes(sampleContent), 0, contentLength);

resp := innerRequest:GetResponse();
:RETURN resp:StatusCode;

:ENDPROC;

And then you create the GetFromAzureBlob to retrieve the file in a similar fashion:

:PROCEDURE GetFromAzureBlob;
:PARAMETERS remoteFile;

:DECLARE    sasToken, storageAccount, containerName, method, sampleContent,
            contentLength, requestUri, oWebService, oClient, oRequest, innerRequest,
            stream, resp, encoding, sTmpFileName;

encoding := LimsNetConnect("", "System.Text.Encoding",, .T.);


sasToken := "yourSAStoken";
storageAccount := "yourAzureAccount";
containerName := "yourAzureContainer";
blobName := remoteFile;

method := "GET";

requestUri := Replace(Replace(Replace(Replace(
                    "https://{storageAccount}.blob.core.windows.net/{containerName}/{blobName}?{sasToken}",
                        "{storageAccount}", storageAccount),
                        "{containerName}", containerName),
                        "{blobName}", blobName),
                        "{sasToken}", sasToken);

oWebService := WebServices{};
oClient := oWebService:CreateHttpClient();
oRequest := oClient:CreateHttpRequest(requestUri);

oRequest:Method := method;
oRequest:ContentType := "text/plain; charset=UTF-8";
resp := oClient:GetResponse(oRequest);
sTmpFileName := GlbDefaultTempDirectory + CreateGuid() + ".tmp";
resp:SaveValueToFile(sTmpFileName);
:RETURN sTmpFileName;

:ENDPROC;

Step 3- use it

As simple as that, you got yourself an upload and download to azure containers.

Conclusion

As you can see, as usual, this was quite easy! One just needs the correct information. Next step will be to see what more can containers bring to your STARLIMS installation.

Hope this can come in handy sometime to someone!


Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.