FAQ on Open Source Development Using Azure RRS feed

  • General discussion

  • We have collected top 3 common/hot issues for each OSS Dev language that using Azure. The FAQ list has covered 5 OSS dev languages, i.e. Java, Python, Ruby, PHP, Node.js on Azure. Hope this collection could quickly solve or give you some quick hits about the how to resolve issues.

    • PHP

      • Q: What are the PHP configurations we can modify in Azure App Service Web Apps?
        A: We can change the built-in PHP version from 5.4(default), 5.5, 5.6, and 7.0(preview).
        We can change the built-in PHP_INI_USER, PHP_INI_PERDIR, PHP_INI_ALL and, PHP_INI_SYSTEM configurations.
        We can enable extensions in the default PHP runtime.
        We can even use a custom PHP runtime.
        We also can enable Composer automation in Azure for manage your PHP packages.
        For more details, please refer to the offical guide, Configure PHP in Azure App Service Web Apps.

      • Q: How to set up a connection to Azure SQL Database in PHP applications on Azure Web Apps?
        A: Be default, the DLL libraries php_sqlsrv and php_pdo_sqlsrv have been installed in the PHP ext folder on Azure, but haven’t been configured in PHP runtime. So we need to configure the extensions manually:

        1. Create a file named .user.ini at the root directory of your application on Azure.
        2. Write the following content in .user.ini:
        3. Restart your application. Check the extension of sqlsrv and pdo_sqlsrv again.

      • Q: How to generate SAS token of Azure Blob Storage in PHP in version 2015-04-05?
        A: The code snippet:

          function getSASForBlob($accountName,$containerName, $fileName, $resourceType, $permissions, $expiry) {
             /* Create the signature */ 
              $_arraysign = array();
              $_arraysign[] = $permissions;
              $_arraysign[] = '';
              $_arraysign[] = $expiry;
              $_arraysign[] = '/blob/' . $accountName . '/' . $container . '/' . $blob;
              $_arraysign[] = '';
              $_arraysign[] = '';
              $_arraysign[] = '';
              $_arraysign[] = "2015-04-05"; //the API version is now required
              $_arraysign[] = '';
              $_arraysign[] = '';
              $_arraysign[] = '';
              $_arraysign[] = '';
              $_arraysign[] = '';
              $_str2sign = implode("\n", $_arraysign);
              return base64_encode(
                  hash_hmac('sha256', urldecode(utf8_encode($_str2sign)), base64_decode($key), true)
          function getBlobDownloadUrl($container,$blob,$accountName,$key){
              /* Create the signed query part */         
              /*$expTime_utc = new DateTime(null, new DateTimeZone("UTC"));
              $expTime_utc->add(new DateInterval('PT1H'));
              $expiry = date('Y-m-d\TH:i:s\Z', strtotime('+1 day'));
              $_signature=getSASForBlob($accountName,$key,$container, $blob, $resourceType, $permissions, $expiry);
              $_parts = array();
              $_parts[] = (!empty($expiry))?'se=' . urlencode($expiry):'';
              $_parts[] = 'sr=' . $resourceType;
              $_parts[] = (!empty($permissions))?'sp=' . $permissions:'';
              $_parts[] = 'sig=' . urlencode($_signature);
              $_parts[] = "sv=2015-04-05";
              /* Create the signed blob URL */
              $_url = 'https://'
              . $container . '/'
              . $blob . '?'
              . implode('&', $_parts);x
              return $_url;

        Refer to Constructing a Service SAS for more details.

    • Java

      • Q: How to generate the authorization header for using REST APIs of Azure Blob Storage.
        A: The details of authentication for the Azure Storage Services shows at https://msdn.microsoft.com/en-us/library/azure/dd179428.aspx, please see the code snippet using Java below.

        private static Base64 base64 = new Base64();  
        private String createAuthorizationHeader(String canonicalizedString)     {  
            Mac mac = Mac.getInstance("HmacSHA256");  
            mac.init(new SecretKeySpec(base64.decode(key), "HmacSHA256"));  
            String authKey = new String(base64.encode(mac.doFinal(canonicalizedString.getBytes("UTF-8"))));  
            String authStr = "SharedKey " + account + ":" + authKey;  
            return authStr;  
        String urlPath = containerName+"/"+ blobName;  
        String storageServiceVersion = "2009-09-19";  
        SimpleDateFormat fmt = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss");  
        String date = fmt.format(Calendar.getInstance().getTime()) + " GMT";  
        String blobType = "BlockBlob"; //This is important as there are two types of blob, Page blob and Block blob  
        String canonicalizedHeaders = "x-ms-blob-type:"+blobType+"\nx-ms-date:"+date+"\nx-ms-version:"+storageServiceVersion;  
        String canonicalizedResource = "/"+account+"/"+urlPath;  
        //This will change based on the Authentication scheme you are using. This is for SharedKey, you need to change it if you are using SharedKeyLite
        String stringToSign = requestMethod+"\n\n\n"+blobLength+"\n\n\n\n\n\n\n\n\n"+canonicalizedHeaders+"\n"+canonicalizedResource; 
        String authorizationHeader = createAuthorizationHeader(stringToSign);  

      • Q: How to authenticate with ARM & ASM using Azure SDK for Java.
        A: Please see the codes below.

        Code 1. Authentication with ARM

         // The parameters include clientId, clientSecret, tenantId, subscriptionId and resourceGroupName.
         private static final String clientId = "<client-id>";
         private static final String clientSecret = "<key>";
         private static final String tenantId = "<tenant-id>";
         private static final String subscriptionId = "<subscription-id>";
         private static final String resouceGroupName = "<resource-group-name>";
         // The function for getting the access token via Class AuthenticationResult
         private static AuthenticationResult getAccessTokenFromServicePrincipalCredentials()
                 throws ServiceUnavailableException, MalformedURLException, ExecutionException, InterruptedException {
             AuthenticationContext context;
             AuthenticationResult result = null;
             ExecutorService service = null;
             try {
                 service = Executors.newFixedThreadPool(1);
                 // TODO: add your tenant id
                 context = new AuthenticationContext("https://login.windows.net/" + tenantId, false, service);
                 // TODO: add your client id and client secret
                 ClientCredential cred = new ClientCredential(clientId, clientSecret);
                 Future<AuthenticationResult> future = context.acquireToken("https://management.azure.com/", cred, null);
                 result = future.get();
             } finally {
             if (result == null) {
                 throw new ServiceUnavailableException("authentication result was null");
             return result;
         // The process for getting the list of VMs in a resource group
         Configuration config = ManagementConfiguration.configure(null, 
             new URI("https://management.core.windows.net"),

        Code2. Authentication with ASM

         static String uri = "https://management.core.windows.net/";
         static String subscriptionId = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX";
         static String keyStoreLocation = "<cert-file>.jks";
         static String keyStorePassword = "my-cert-password";
         Configuration config = ManagementConfiguration.configure(
             new URI(uri), 
             keyStoreLocation, // the file path to the JKS
             keyStorePassword, // the password for the JKS
             KeyStoreType.jks // flags that I'm using a JKS keystore

      • Q: How to get started with Event Hubs using Java.
        A: We can get started with Event Hubs by following three steps below.

        1. Create an Event Hub via Azure classic portal.
        2. Send messages to Event Hubs with EventHubClient.
        3. Receive messages from Event Hubs with EventProcessorHost.

        The classes above EventHubClient & EventProcessorHost are in the Azure SDK for Java, you can configure the pom.xml file of your Maven project to reference them.


        As a reference, please see the offical tutorial at https://azure.microsoft.com/en-us/documentation/articles/event-hubs-java-ephjava-getstarted/.

    Thursday, June 30, 2016 8:46 AM