Skip to content

Commit

Permalink
Merge pull request Azure#3 from veena-udayabhanu/master
Browse files Browse the repository at this point in the history
Code Samples
  • Loading branch information
veena-udayabhanu committed Dec 19, 2013
2 parents bf43e2a + 0f5b3e9 commit e12461a
Show file tree
Hide file tree
Showing 8 changed files with 824 additions and 0 deletions.
28 changes: 28 additions & 0 deletions windowsazure-storage-samples/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>windowsazure-storage-samples</groupId>
<artifactId>windowsazure-storage-samples</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>windowsazure-storage-samples</name>
<description>Samples for creating blobs, queues and tables</description>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.microsoft.windowsazure.storage</groupId>
<artifactId>microsoft-windowsazure-storage-sdk</artifactId>
<version>0.5.0</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/**
* Copyright Microsoft Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.microsoft.windowsazure.storage.blob.gettingstarted;

import java.net.URISyntaxException;
import java.security.InvalidKeyException;

import com.microsoft.windowsazure.storage.CloudStorageAccount;
import com.microsoft.windowsazure.storage.blob.BlobContainerPermissions;
import com.microsoft.windowsazure.storage.blob.BlobContainerPublicAccessType;
import com.microsoft.windowsazure.storage.blob.CloudBlobClient;
import com.microsoft.windowsazure.storage.blob.CloudBlobContainer;
import com.microsoft.windowsazure.storage.blob.CloudBlockBlob;
import com.microsoft.windowsazure.storage.blob.ListBlobItem;
import com.microsoft.windowsazure.storage.util.Utility;

/**
* This sample illustrates basic usage of the various Blob Primitives provided
* in the Storage Client Library including CloudBlobContainer, CloudBlockBlob
* and CloudBlobClient.
*/
public class BlobBasics {

/**
* Executes the sample.
*
* @param args
* No input args are expected from users.
* @throws URISyntaxException
* @throws InvalidKeyException
*/
public static void main(String[] args) throws InvalidKeyException, URISyntaxException {
Utility.printSampleStartInfo("BlobBasics");

// Setup the cloud storage account.
CloudStorageAccount account = CloudStorageAccount.parse(Utility.storageConnectionString);

// Create a blob service client
CloudBlobClient blobClient = account.createCloudBlobClient();

try {
// Get a reference to a container
// The container name must be lower case
CloudBlobContainer container = blobClient.getContainerReference("blobbasicscontainer");

// Create the container if it does not exist
container.createIfNotExists();

// Make the container public
// Create a permissions object
BlobContainerPermissions containerPermissions = new BlobContainerPermissions();

// Include public access in the permissions object
containerPermissions.setPublicAccess(BlobContainerPublicAccessType.CONTAINER);

// Set the permissions on the container
container.uploadPermissions(containerPermissions);

// Upload 3 blobs
// Get a reference to a blob in the container
CloudBlockBlob blob1 = container.getBlockBlobReference("blobbasicsblob1");

// Upload text to the blob
blob1.uploadText("Hello, World1");

// Get a reference to a blob in the container
CloudBlockBlob blob2 = container.getBlockBlobReference("blobbasicsblob2");

// Upload text to the blob
blob2.uploadText("Hello, World2");

// Get a reference to a blob in the container
CloudBlockBlob blob3 = container.getBlockBlobReference("blobbasicsblob3");

// Upload text to the blob
blob3.uploadText("Hello, World3");

// Download the blob
// For each item in the container
for (ListBlobItem blobItem : container.listBlobs()) {
// If the item is a blob, not a virtual directory
if (blobItem instanceof CloudBlockBlob) {
// Download the text
CloudBlockBlob retrievedBlob = (CloudBlockBlob) blobItem;
System.out.println(retrievedBlob.downloadText());
}
}

// List the blobs in a container, loop over them and
// output the URI of each of them
for (ListBlobItem blobItem : container.listBlobs()) {
System.out.println(blobItem.getUri());
}

// Delete the blobs
blob1.deleteIfExists();
blob2.deleteIfExists();
blob3.deleteIfExists();

// Delete the container
container.deleteIfExists();
}
catch (Throwable t) {
Utility.printException(t);
}

Utility.printSampleCompleteInfo("BlobBasics");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/**
* Copyright Microsoft Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.microsoft.windowsazure.storage.queue.gettingstarted;

import java.net.URISyntaxException;
import java.security.InvalidKeyException;
import java.util.EnumSet;

import com.microsoft.windowsazure.storage.CloudStorageAccount;
import com.microsoft.windowsazure.storage.queue.CloudQueue;
import com.microsoft.windowsazure.storage.queue.CloudQueueClient;
import com.microsoft.windowsazure.storage.queue.CloudQueueMessage;
import com.microsoft.windowsazure.storage.queue.MessageUpdateFields;
import com.microsoft.windowsazure.storage.util.Utility;

/**
* This sample illustrates basic usage of the various Queue Primitives provided
* in the Storage Client Library including CloudQueue, CloudQueueMessage
* and CloudQueueClient.
*/
public class QueueBasics {

/**
* Executes the sample.
*
* @param args
* No input args are expected from users.
* @throws URISyntaxException
* @throws InvalidKeyException
*/
public static void main(String[] args) throws InvalidKeyException, URISyntaxException {
Utility.printSampleStartInfo("QueueBasics");

// Setup the cloud storage account.
CloudStorageAccount account = CloudStorageAccount.parse(Utility.storageConnectionString);

// Create a queue service client
CloudQueueClient queueClient = account.createCloudQueueClient();

try {
// Retrieve a reference to a queue
CloudQueue queue = queueClient.getQueueReference("queuebasics");

// Create the queue if it doesn't already exist
queue.createIfNotExists();

// Create messages and add it to the queue
CloudQueueMessage message1 = new CloudQueueMessage("Hello, World1");
queue.addMessage(message1);

CloudQueueMessage message2 = new CloudQueueMessage("Hello, World2");
queue.addMessage(message2);

CloudQueueMessage message3 = new CloudQueueMessage("Hello, World3");
queue.addMessage(message3);

CloudQueueMessage message4 = new CloudQueueMessage("Hello, World4");
queue.addMessage(message4);

CloudQueueMessage message5 = new CloudQueueMessage("Hello, World5");
queue.addMessage(message5);

// Peek at the next message
CloudQueueMessage peekedMessage = queue.peekMessage();
System.out.println(String.format("Peeked Message : %s", peekedMessage.getMessageContentAsString()));

// Modify the message content and set it to be visible immediately
// Retrieve the first visible message in the queue
CloudQueueMessage updateMessage = queue.retrieveMessage();
updateMessage.setMessageContent("Updated contents.");
EnumSet<MessageUpdateFields> updateFields =
EnumSet.of(MessageUpdateFields.CONTENT, MessageUpdateFields.VISIBILITY);
queue.updateMessage(updateMessage, 0, updateFields, null, null);

// Retrieve 3 messages from the queue with a visibility timeout of 1 second
queue.retrieveMessages(3, 1, null, null);

// Sleep for 2 seconds so the messages become visible and can be processed/deleted
Thread.sleep(2000);

// Retrieve the messages in the queue with a visibility timeout of 30 seconds and delete them
CloudQueueMessage retrievedMessage;
while((retrievedMessage = queue.retrieveMessage(30, null /*options*/, null /*opContext*/)) != null) {
// Process the message in less than 30 seconds, and then delete the message.
System.out.println(retrievedMessage.getMessageContentAsString());
queue.deleteMessage(retrievedMessage);
}

// Delete a queue
queue.deleteIfExists();

}catch (Throwable t) {
Utility.printException(t);
}

Utility.printSampleCompleteInfo("QueueBasics");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Copyright Microsoft Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.microsoft.windowsazure.storage.table.gettingtstarted;

import com.microsoft.windowsazure.storage.table.TableServiceEntity;

public class CustomerEntity extends TableServiceEntity {

// Note: An entity's partition and row key uniquely identify the entity in the table.
// Entities with the same partition key can be queried faster than those with different partition keys.
public CustomerEntity(String lastName, String firstName) {
this.partitionKey = lastName;
this.rowKey = firstName;
}

public CustomerEntity() { }

public String Email;
public String PhoneNumber;

public String getEmail() {
return this.Email;
}

public void setEmail(String email) {
this.Email = email;
}

public String getPhoneNumber() {
return this.PhoneNumber;
}

public void setPhoneNumber(String phoneNumber) {
this.PhoneNumber = phoneNumber;
}
}
Loading

0 comments on commit e12461a

Please sign in to comment.