title: Java client to set SAS, Policy and CORS on Azure
body: <p>I'm trying to set CORS properties on Azure using Java client. After executing code, I run HTML5 code to upload a file and facing following errors in chrome javascript console:</p>
  
  <blockquote>
    <p>max block size = 47276</p>
    
    <p>total blocks = 1</p>
    
    <p>https:myacc.blob.core.windows.net/mycon/ch1.jpg?sr=c&amp;sv=2015-04-05&amp;sig=djbVxIBlyVy18bV0SkqNSLql1n9efAVcYnGy3VsGKis%3D&amp;si=champ</p>
    
    <p>current file pointer = 0 bytes read = 47276</p>
    
    <p>block id = block-000000</p>
    
    <p>https:myacc.blob.core.windows.net/mycon/ch1.jpg?sr=c&amp;sv=2015-0…kqNSLql1n9efAVcYnGy3VsGKis%3D&amp;si=champ&amp;comp=block&amp;blockid=YmxvY2stMDAwMDAw</p>
    
    <p><strong>Failed to load resource: the server responded with a status of 403 (CORS not enabled or no matching rule found for this request.)</strong></p>
    
    <p>XMLHttpRequest cannot load
    https:myacc.blob.core.windows.net/mycon/ch1.jpg?sr=c&amp;sv=2015-0…kqNSLql1n9efAVcYnGy3VsGKis%3D&amp;si=heath&amp;comp=block&amp;blockid=YmxvY2stMDAwMDAw.</p>
    
    <p><strong>Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested
    resource. Origin 'file://' is therefore not allowed access. The
    response had HTTP status code 403.</strong></p>
  </blockquote>
  
  <p><strong>What I'm wondering is why it didn't set CORS if Java client was executed successfully ? Also, how do I verify if rule Policy "champ" is configured properly, if my generated SAS is correct and CORS properties are created or not</strong></p>
  
  <p>Here is Java Client code:</p>
  
  <pre><code>public class CORS_and_SAS {
  public static void main(String[] args) {
      // Define the connection-string with your values
      final String storageConnectionString ="DefaultEndpointsProtocol=http;" + "AccountName=myacc;" + "AccountKey=B2q4AGp6YoRsTREXIkOv3e/Sxf46YzqzfnM9F8U+o7VA5Y3EiKc+CuritnvuyZxGXKNOQ5nJy2KfkniF970on1dQ==";
      try {
          // Retrieve storage account from connection-string.
          CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
  
          // Create the blob client.
         CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
  
         // Get a reference to a container.
         // The container name must be lower case
         CloudBlobContainer container = blobClient.getContainerReference("mycon");
  
         // Create the container if it does not exist.
          //container.createIfNotExists();
         // Set CORS support
         //ServiceProperties blobServiceProperties = blobClient.GetServiceProperties();
         ServiceProperties propers = getCORS();
         blobClient.uploadServiceProperties(propers);
          SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy();
            GregorianCalendar calendar = 
               new GregorianCalendar(TimeZone.getTimeZone("UTC"));
            calendar.setTime(new Date());
            policy.setSharedAccessStartTime(calendar.getTime()); //Immediately applicable
            calendar.add(Calendar.HOUR, 3000); //Applicable time-span is 3000 hours
            policy.setSharedAccessExpiryTime(calendar.getTime());           
            policy.setPermissions(EnumSet.of(SharedAccessBlobPermissions.READ, 
               SharedAccessBlobPermissions.WRITE, SharedAccessBlobPermissions.DELETE, 
               SharedAccessBlobPermissions.LIST));
            BlobContainerPermissions containerPermissions = new BlobContainerPermissions();
            //Private container with no access for anonymous users
            containerPermissions.setPublicAccess(BlobContainerPublicAccessType.OFF);
            //Name the shared access policy: heath
            containerPermissions.getSharedAccessPolicies().put("champ", policy);
            container.uploadPermissions(containerPermissions);
            //Generate the policy SAS string for heath access
            String sas = container.generateSharedAccessSignature(
               new SharedAccessBlobPolicy(),"champ");           
            System.out.println("The stored access policy signature:");
            System.out.println(sas);
      } catch (Exception e) {
          // Output the stack trace.
          e.printStackTrace();
      }
  }
  
  private static ServiceProperties getCORS() {
      // TODO Auto-generated method stub
      ServiceProperties propers = new ServiceProperties();
      CorsProperties corsprop = propers.getCors();
  
      CorsRule cr = new CorsRule();
  
      List&lt;String&gt; allowedHeaders = new ArrayList&lt;String&gt;();
      allowedHeaders.add("x-ms-*");
      List&lt;String&gt; exposedHeaders = new ArrayList&lt;String&gt;();
      exposedHeaders.add("x-ms-*");
  
      cr.setAllowedHeaders(allowedHeaders);
      cr.setExposedHeaders(exposedHeaders);
      EnumSet&lt;CorsHttpMethods&gt; allowedMethod = EnumSet.of(CorsHttpMethods.PUT,CorsHttpMethods.GET,CorsHttpMethods.POST,CorsHttpMethods.HEAD,CorsHttpMethods.DELETE);
      //EnumSet&lt;CorsHttpMethods&gt; allowedMethod1 = EnumSet.of(CorsHttpMethods.GET);
      cr.setAllowedMethods(allowedMethod);
  
      List&lt;String&gt; allowedOrigin = new ArrayList&lt;String&gt;();
      allowedOrigin.add("*");
      cr.setAllowedOrigins(allowedOrigin);
      cr.setMaxAgeInSeconds(600);
  
      corsprop.getCorsRules().add(cr);
  
      //corsprop.getCorsRules().add(cr);
      propers.setCors(corsprop);
      return propers;
  }
  }
  </code></pre>