Permalink
Browse files

Modifed code to use CloudConfigurationManager instead of hardcoding i…

…n account name/key
  • Loading branch information...
1 parent 1f05f1b commit c602626529b71da515a8329832f4bdd3ea53c5d1 Walter Poupore (Microsoft) committed Oct 25, 2013
Showing with 97 additions and 12 deletions.
  1. +48 −6 storage/BlockBlobExample.php
  2. +49 −6 storage/PageBlobExample.php
@@ -18,13 +18,11 @@
use WindowsAzure\Common\ServicesBuilder;
use WindowsAzure\Common\ServiceException;
+use WindowsAzure\Common\CloudConfigurationManager;
use WindowsAzure\Blob\Models\Block;
use WindowsAzure\Blob\Models\CreateContainerOptions;
use WindowsAzure\Blob\Models\ListContainersOptions;
-define("ACCOUNTNAME", "your_storage_account_name");
-define("ACCOUNTKEY", "your_storage_account_key");
-
define("CONTAINERNAME", "mycontainer");
define("BLOCKBLOBNAME", "myblockblob");
@@ -60,10 +58,54 @@ function createContainerIfNotExists($blobRestProxy)
try {
- print "Beginning processing.\n";
+ echo "Beginning processing.\n";
+
+ /*
+ Use CloudConfigurationManager::getConnectionString to retrieve
+ the connection string whose name (in this example) is
+ "StorageConnectionString".
+
+ By default, the CloudConfigurationManager::getConnectionString method
+ will look for an environment variable with the name that is passed in
+ as the method parameter, and then assign the environment variable's
+ value as the return value.
+
+ For example, if you want to use the storage emulator, start
+ the storage emulator, set an environment variable through a technique
+ such as
+
+ set StorageConnectionString=UseDevelopmentStorage=true
+
+ and then run this sample at a command prompt that has the
+ StorageConnectionString as an active environment variable.
+
+ If you want to use a production storage account, set the
+ environment variable through a technique such as
+
+ set StorageConnectionString=DefaultEndpointsProtocol=http;AccountName=your_account_name;AccountKey=your_account_key
+
+ (Substitute your storage account name and account key for
+ your_account_name and your_account_key, respectively.)
+ Then run this sample at a command prompt that has the
+ StorageConnectionString as an active environment variable.
+
+ The format for the storage connection string itself is documented at
+ http://msdn.microsoft.com/en-us/library/windowsazure/ee758697.aspx
+
+ If you do not want to use an environment variable as the source
+ for the connection string name, you can register other sources
+ via the CloudCofigurationManager::registerSource method.
+
+ */
+ $connectionString = CloudConfigurationManager::getConnectionString("StorageConnectionString");
- $ConnectionString="DefaultEndpointsProtocol=http;AccountName=" . ACCOUNTNAME . ";AccountKey=" . ACCOUNTKEY;
- $blobRestProxy = ServicesBuilder::getInstance()->createBlobService($ConnectionString);
+ if (null == $connectionString || "" == $connectionString)
+ {
+ echo "Did not find a connection string whose name is 'StorageConnectionString'.";
+ exit();
+ }
+
+ $blobRestProxy = ServicesBuilder::getInstance()->createBlobService($connectionString);
createContainerIfNotExists($blobRestProxy);
@@ -17,15 +17,12 @@
require_once 'WindowsAzure\WindowsAzure.php';
use WindowsAzure\Common\ServicesBuilder;
+use WindowsAzure\Common\CloudConfigurationManager;
use WindowsAzure\Blob\Models\ListContainersOptions;
use WindowsAzure\Blob\Models\CreateBlobPagesOptions;
use WindowsAzure\Blob\Models\PageRange;
use WindowsAzure\Common\ServiceException;
-define("ACCOUNTNAME", "your_storage_account_name");
-define("ACCOUNTKEY", "your_storage_account_key");
-
-
define("CONTAINERNAME", "mycontainer");
define("PAGEBLOBNAME", "mypageblob");
@@ -157,8 +154,54 @@ function waitForEnterKey()
try
{
- $ConnectionString="DefaultEndpointsProtocol=http;AccountName=" . ACCOUNTNAME . ";AccountKey=" . ACCOUNTKEY;
- $blobRestProxy = ServicesBuilder::getInstance()->createBlobService($ConnectionString);
+ echo "Beginning processing.\n";
+
+ /*
+ Use CloudConfigurationManager::getConnectionString to retrieve
+ the connection string whose name (in this example) is
+ "StorageConnectionString".
+
+ By default, the CloudConfigurationManager::getConnectionString method
+ will look for an environment variable with the name that is passed in
+ as the method parameter, and then assign the environment variable's
+ value as the return value.
+
+ For example, if you want to use the storage emulator, start
+ the storage emulator, set an environment variable through a technique
+ such as
+
+ set StorageConnectionString=UseDevelopmentStorage=true
+
+ and then run this sample at a command prompt that has the
+ StorageConnectionString as an active environment variable.
+
+ If you want to use a production storage account, set the
+ environment variable through a technique such as
+
+ set StorageConnectionString=DefaultEndpointsProtocol=http;AccountName=your_account_name;AccountKey=your_account_key
+
+ (Substitute your storage account name and account key for
+ your_account_name and your_account_key, respectively.)
+ Then run this sample at a command prompt that has the
+ StorageConnectionString as an active environment variable.
+
+ The format for the storage connection string itself is documented at
+ http://msdn.microsoft.com/en-us/library/windowsazure/ee758697.aspx
+
+ If you do not want to use an environment variable as the source
+ for the connection string name, you can register other sources
+ via the CloudCofigurationManager::registerSource method.
+
+ */
+ $connectionString = CloudConfigurationManager::getConnectionString("StorageConnectionString");
+
+ if (null == $connectionString || "" == $connectionString)
+ {
+ echo "Did not find a connection string whose name is 'StorageConnectionString'.";
+ exit();
+ }
+
+ $blobRestProxy = ServicesBuilder::getInstance()->createBlobService($connectionString);
createContainerIfNotExists($blobRestProxy);

0 comments on commit c602626

Please sign in to comment.