Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>@VJD I have written some guidance docs to using the Java API, with sample Java code. This ought to be published on MSDN in the next day or so. I will post a link here once it goes live. Here are the links to the entire content.</p> <p><a href="http://msdn.microsoft.com/en-us/library/dn246928.aspx" rel="nofollow">Controlling Access to Windows Azure Blob Containers with Java</a></p> <p><a href="http://msdn.microsoft.com/en-us/library/dn246927.aspx" rel="nofollow">Controlling Access to Windows Azure Queues with Java</a></p> <p><a href="http://msdn.microsoft.com/en-us/library/dn246929.aspx" rel="nofollow">Controlling Access to Windows Azure Tables with Java</a> </p> <p>You can control access to blob containers in your storage account in several ways, but the use of stored access policy may be the most flexible. This enables you to give temporary access to clients, without revealing your secret storage account key, and enables you to cancel, extend, or update the type of access without having to redistribute basic SAS strings.</p> <p>For example, your application can generate a stored access policy signature for a container using Java code similar to the following.</p> <pre><code>public class PolicySAS { public static void main(String[] args) throws InvalidKeyException, URISyntaxException, StorageException { Account creds = new Account(); //Account key required to create SAS final String storageConnectionString = creds.getstorageconnectionstring(); CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString); CloudBlobClient blobClient = storageAccount.createCloudBlobClient(); CloudBlobContainer container = blobClient.getContainerReference("container1"); container.createIfNotExist(); 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, 3); //Applicable time-span is 3 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("heath", policy); container.uploadPermissions(containerPermissions); //Generate the policy SAS string for heath access String sas = container.generateSharedAccessSignature( new SharedAccessBlobPolicy(),"heath"); System.out.println("The stored access policy signature:"); System.out.println(sas); } } </code></pre> <p>A client can use a class similar to this to upload a blob with metadata. </p> <pre><code>public class SASblober { public static void main(String[] args) throws URISyntaxException, FileNotFoundException, StorageException, IOException { //This does not reveal the secret storage account key URI baseuri = new URI("http://grassy.blob.core.windows.net"); CloudBlobClient blobclient = new CloudBlobClient(baseuri); MyUploadBlob("container1", "sr=c&amp;sv=2012-02-12&amp;sig=nnPn5P5nnPPnn5Pnn5PPnPPPnPPP5PPPPPP%5PPnn5PPn%55&amp;si=heath", blobclient); } public static void MyUploadBlob(String containerName, String containerSAS, CloudBlobClient blobClient) throws URISyntaxException, StorageException, FileNotFoundException, IOException { //Uploads a local file to blob-container in cloud String blobName = "image3.jpg"; String localFileName = "c:\\myimages\\image3.jpg"; URI uri = new URI(blobClient.getEndpoint().toString() + "/" + containerName + "/" + blobName + "?" + containerSAS); CloudBlockBlob sasBlob = new CloudBlockBlob(uri, blobClient); HashMap&lt;String, String&gt; user = new HashMap&lt;String, String&gt;(); user.put("firstname", "Joe"); user.put("lastname", "Brown" ); user.put("age", "28"); user.put("presenter", "no"); sasBlob.setMetadata(user); File fileReference = new File(localFileName); sasBlob.upload(new FileInputStream(fileReference), fileReference.length()); System.out.println("The blob: " + blobName + " has been uploaded to:"); System.out.println(uri); } } </code></pre> <p>A client can use a class similar to this to read a blob.</p> <pre><code>public class SASread { public static void main(String[] args) throws URISyntaxException, FileNotFoundException, StorageException, IOException { URI baseuri = new URI("http://grassy.blob.core.windows.net"); CloudBlobClient blobclient = new CloudBlobClient(baseuri); MyDownloadBlob("container1", "sr=c&amp;sv=2012-02-12&amp;sig=nnPn5P5nnPPnn5Pnn5PPnPPPnPPP5PPPPPP%5PPnn5PPn%55&amp;si=heath", blobclient); } public static void MyDownloadBlob(String containerName, String containerSAS, CloudBlobClient blobClient) throws URISyntaxException, StorageException, FileNotFoundException, IOException { // Downloads blob in cloud to local file String blobName = "image3.jpg"; String localFileName = "c:\\myoutputimages\\image3.jpg"; URI uri = new URI(blobClient.getEndpoint().toString() + "/" + containerName + "/" + blobName + "?" + containerSAS); CloudBlockBlob sasBlob = new CloudBlockBlob(uri, blobClient); File fileTarget = new File(localFileName); sasBlob.download(new FileOutputStream(fileTarget)); HashMap&lt;String, String&gt; user = new HashMap&lt;String, String&gt;(); user = sasBlob.getMetadata(); String name = (user.get("firstname") + " " + user.get("lastname")); String age = ("age: " + user.get("age")); String present = ("Presenting talk? " + user.get("presenter")); System.out.println(name); System.out.println(age); System.out.println(present); System.out.println("The blob at:\n" + uri + "\nwas downloaded from the cloud to local file:\n" + localFileName); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload