Skip to content

Latest commit

 

History

History
279 lines (233 loc) · 16.3 KB

Knowledge sharing.md

File metadata and controls

279 lines (233 loc) · 16.3 KB

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:
        extension=php_pdo_sqlsrv.dll
        extension=php_sqlsrv.dll
        
      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 */         
            $resourceType='b';
            $permissions='r';
            /*$expTime_utc = new DateTime(null, new DateTimeZone("UTC"));
            $expTime_utc->add(new DateInterval('PT1H'));
            $expiry=$expTime_utc->format('Y-m-d\TH:i:s\Z');*/
            $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://'
            .$accountName.'.blob.core.windows.net/'
            . $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");  
      fmt.setTimeZone(TimeZone.getTimeZone("GMT"));  
      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 {
               service.shutdown();
           }
      
           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"),
           subscriptionId,
           getAccessTokenFromServicePrincipalCredentials().getAccessToken());

      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), 
           subscriptionId,
           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.

       <dependency>
           <groupId>com.microsoft.azure</groupId>
           <artifactId>azure-eventhubs</artifactId>
           <version>0.7.2</version>
       </dependency>

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


  • Python


  • Node.js

    • Q: How to build/deploy/configure a web app in Node.js on Azure Web Apps.
      A: Please refer to Get started with Node.js web apps in Azure App Service.
      Additionally, Azure Web Apps uses IISNode to host the Node process inside of IIS. Your Node site is actually given a Named Pipe which receives the incoming requests, not a TCP port like you would use when running locally or hosting yourself. Even if you could open a TCP port, Azure Websites are really only meant for traditional websites and don't have a way to open ports to the outside world.


    • Q: How to use tedious to connect to Azure SQL.
      A: Use following code snippet:

      var Connection = require('tedious').Connection;
      var config = {
          userName: '<yourusername>',
          password: '<yourpassword>',
          server: '<yourserver>.database.windows.net',
          // When you connect to Azure SQL Database, you need these next options.
          options: {encrypt: true, database: '<database>'}
      };
      var connection = new Connection(config);
      connection.on('connect', function(err) {
          // If no error, then good to proceed.
          console.log("Connected");
      });

    • Q: Azure webapp node.js app not starting
      A: Azure WebApp for node.js is running via iisnode as a native IIS module that allows hosting in IIS on Windows. Please see the document https://blogs.msdn.microsoft.com/silverlining/2012/06/14/windows-azure-websites-node-js/ to know its features.

      For debugging the webapp for node.js, I suggest that you can refer to the document to know how to do it.

      If you are using Visual Studio as the IDE for node.js, I recommend installing NTVS in VS for debugging and deploying the node.js. Please see the documents below to know how to get started.
      Installation for NTVS
      Install Node.js and get started with NTVS

      And there is an other tool called node-inspector for inspecting the node.js app on Azure. As a reference, you can refer to the doc http://www.ranjithr.com/?p=98.

      Meanwhile, please check the web.config file in your node webapp via Kudo Console, you can compare your codes with the sample generated by the template for Express from Gallery on Azure portal.


  • Rudy