Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Blob storage examples for PHP, initial checkin.

  • Loading branch information...
commit 1f05f1b069acd1803f67279f894b51c47d73facf 1 parent 557686e
@WalterHub authored
Showing with 365 additions and 0 deletions.
  1. +128 −0 storage/BlockBlobExample.php
  2. +237 −0 storage/PageBlobExample.php
View
128 storage/BlockBlobExample.php
@@ -0,0 +1,128 @@
+<?php
+
+/** * Copyright 2013 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.
+ */
+
+require_once 'WindowsAzure\WindowsAzure.php';
+
+use WindowsAzure\Common\ServicesBuilder;
+use WindowsAzure\Common\ServiceException;
+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");
+
+define("BLOCKSIZE", 4 * 1024 * 1024); // Size of the block, modify if needed.
+define("PADLENGTH", 5); // Size of the string used for the block ID, modify if needed.
+
+define("FILENAME", "myfile.txt"); // Local file to upload as a block blob.
+
+function createContainerIfNotExists($blobRestProxy)
+{
+ // See if the container already exists.
+ $listContainersOptions = new ListContainersOptions;
+ $listContainersOptions->setPrefix(CONTAINERNAME);
+ $listContainersResult = $blobRestProxy->listContainers($listContainersOptions);
+ $containerExists = false;
+ foreach ($listContainersResult->getContainers() as $container)
+ {
+ if ($container->getName() == CONTAINERNAME)
+ {
+ // The container exists.
+ $containerExists = true;
+ // No need to keep checking.
+ break;
+ }
+ }
+ if (!$containerExists)
+ {
+ echo "Creating container.\n";
+ $blobRestProxy->createContainer(CONTAINERNAME);
+ echo "Container '" . CONTAINERNAME . "' successfully created.\n";
+ }
+}
+
+try {
+
+ print "Beginning processing.\n";
+
+ $ConnectionString="DefaultEndpointsProtocol=http;AccountName=" . ACCOUNTNAME . ";AccountKey=" . ACCOUNTKEY;
+ $blobRestProxy = ServicesBuilder::getInstance()->createBlobService($ConnectionString);
+
+ createContainerIfNotExists($blobRestProxy);
+
+ echo "Using the '" . CONTAINERNAME . "' container and the '" . BLOCKBLOBNAME . "' blob.\n";
+
+ echo "Using file '" . FILENAME . "'\n";
+
+ if (!file_exists(FILENAME))
+ {
+ echo "The '" . FILENAME . "' file does not exist. Exiting program.\n";
+ exit();
+ }
+
+ $handle = fopen(FILENAME, "r");
+
+ // Upload the blob using blocks.
+ $counter = 1;
+ $blockIds = array();
+
+ while (!feof($handle))
+ {
+ $blockId = str_pad($counter, PADLENGTH, "0", STR_PAD_LEFT);
+ echo "Processing block $blockId.\n";
+
+ $block = new Block();
+ $block->setBlockId(base64_encode($blockId));
+ $block->setType("Uncommitted");
+ array_push($blockIds, $block);
+
+ $data = fread($handle, BLOCKSIZE);
+
+ // Upload the block.
+ $blobRestProxy->createBlobBlock(CONTAINERNAME, BLOCKBLOBNAME, base64_encode($blockId), $data);
+ $counter++;
+ }
+
+ // Done creating the blocks. Close the file and commit the blocks.
+ fclose($handle);
+ echo "Commiting the blocks.\n";
+ $blobRestProxy->commitBlobBlocks(CONTAINERNAME, BLOCKBLOBNAME, $blockIds);
+
+ echo "Done processing.\n";
+}
+catch(ServiceException $serviceException)
+{
+ // Handle exception based on error codes and messages.
+ // Error codes and messages are here:
+ // http://msdn.microsoft.com/en-us/library/windowsazure/dd179439.aspx
+ echo "ServiceException encountered.\n";
+ $code = $serviceException->getCode();
+ $error_message = $serviceException->getMessage();
+ echo "$code: $error_message";
+}
+catch (Exception $exception)
+{
+ echo "Exception encountered.\n";
+ $code = $exception->getCode();
+ $error_message = $exception->getMessage();
+ echo "$code: $error_message";
+}
+
+?>
View
237 storage/PageBlobExample.php
@@ -0,0 +1,237 @@
+<?php
+
+/** * Copyright 2013 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.
+ */
+
+require_once 'WindowsAzure\WindowsAzure.php';
+
+use WindowsAzure\Common\ServicesBuilder;
+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");
+
+define("PAGESIZE", 512); // Must be a multiple of 512.
+
+define("NUMPAGES", 6); // Keeping it small for this example.
+
+function createContainerIfNotExists($blobRestProxy)
+{
+ // See if the container already exists.
+ $listContainersOptions = new ListContainersOptions;
+ $listContainersOptions->setPrefix(CONTAINERNAME);
+ $listContainersResult = $blobRestProxy->listContainers($listContainersOptions);
+ $containerExists = false;
+ foreach ($listContainersResult->getContainers() as $container)
+ {
+ if ($container->getName() == CONTAINERNAME)
+ {
+ // The container exists.
+ $containerExists = true;
+ // No need to keep checking.
+ break;
+ }
+ }
+ if (!$containerExists)
+ {
+ echo "Creating container.\n";
+ $blobRestProxy->createContainer(CONTAINERNAME);
+ echo "Container '" . CONTAINERNAME . "' successfully created.\n";
+ }
+}
+
+function createPageBlob($blobRestProxy)
+{
+
+ echo "Creating page blob.\n";
+ $len = NUMPAGES * PAGESIZE;
+ $blobRestProxy->createPageBlob(CONTAINERNAME, PAGEBLOBNAME, $len);
+ echo "Page blob '" . PAGEBLOBNAME . "' successfully created with length $len.\n";
+
+}
+
+function writeContent($blobRestProxy, $pageIndex, $createBlobPagesOptions)
+{
+ echo "Writing to blob using page index $pageIndex.\n";
+
+ // Determine the page range.
+ $start = $pageIndex * PAGESIZE;
+ $end = $start + PAGESIZE - 1;
+ $pageRange = new PageRange($start, $end);
+
+ // Generate a random string of the desired length.
+ $content = "";
+ for ($i = 0; $i < PAGESIZE; $i++)
+ {
+ // Create the string with random lowercase alphabet characters.
+ $content .= chr(mt_rand(97, 122));
+ }
+
+ $leaseID = $blobRestProxy->acquireLease(CONTAINERNAME, PAGEBLOBNAME)->getLeaseId();
+ echo "Acquired lease $leaseID.\n";
+ $createBlobPagesOptions->setLeaseId($leaseID);
+ $blobRestProxy->createBlobPages(CONTAINERNAME, PAGEBLOBNAME, $pageRange, $content, $createBlobPagesOptions);
+ $blobRestProxy->releaseLease(CONTAINERNAME, PAGEBLOBNAME, $leaseID);
+ echo "Released lease $leaseID.\n";
+ echo "Wrote to blob.\n";
+}
+
+function clearPages($blobRestProxy, $pageIndex, $numPages, $createBlobPagesOptions)
+{
+ echo "Clearing page(s).\n";
+ $pageRange = new PageRange($pageIndex * PAGESIZE, ($pageIndex + $numPages) * PAGESIZE - 1);
+ $leaseID = $blobRestProxy->acquireLease(CONTAINERNAME, PAGEBLOBNAME)->getLeaseId();
+ echo "Acquired lease $leaseID.\n";
+ $createBlobPagesOptions->setLeaseId($leaseID);
+ $blobRestProxy->clearBlobPages(CONTAINERNAME, PAGEBLOBNAME, $pageRange, $createBlobPagesOptions);
+ $blobRestProxy->releaseLease(CONTAINERNAME, PAGEBLOBNAME, $leaseID);
+ echo "Released lease $leaseID.\n";
+ echo "Cleared $numPages page(s), beginning with page $pageIndex.\n";
+}
+
+function showActiveRanges($blobRestProxy)
+{
+ echo "Determining active ranges.\n";
+ $listPageRangesResult = $blobRestProxy->listPageBlobRanges(CONTAINERNAME, PAGEBLOBNAME);
+ $ranges = $listPageRangesResult->getPageRanges();
+ if (0 == count($ranges))
+ {
+ echo "No ranges are active.\n";
+ }
+ else
+ {
+ echo "Active ranges: ";
+ echo "< ";
+ foreach ($ranges as $range)
+ {
+ echo "[" . $range->getStart() . " - " . $range->getEnd() . "] ";
+ }
+ echo " >\n";
+ }
+}
+
+function displayContents($blobRestProxy)
+{
+ $getBlobResult = $blobRestProxy->getBlob(CONTAINERNAME, PAGEBLOBNAME);
+ $stream = $getBlobResult->getContentStream();
+ echo "Displaying the blob contents.\n";
+ fpassthru($stream);
+ fclose($stream);
+ echo "\n";
+}
+
+function deleteBlob($blobRestProxy)
+{
+ echo "Deleting the blob.\n";
+ $blobRestProxy->deleteBlob(CONTAINERNAME, PAGEBLOBNAME);
+ echo "Deleted the blob named '" . PAGEBLOBNAME . "' from the '" . CONTAINERNAME . "' container.\n";
+}
+
+function waitForEnterKey()
+{
+ // Prompt the user to press the Enter key.
+ echo "Press Enter to continue. ";
+ fgets(STDIN);
+
+ // Add a blank link.
+ echo "\n";
+}
+
+try
+{
+ $ConnectionString="DefaultEndpointsProtocol=http;AccountName=" . ACCOUNTNAME . ";AccountKey=" . ACCOUNTKEY;
+ $blobRestProxy = ServicesBuilder::getInstance()->createBlobService($ConnectionString);
+
+ createContainerIfNotExists($blobRestProxy);
+
+ echo "Using the '" . CONTAINERNAME . "' container and the '" . PAGEBLOBNAME . "' blob.\n";
+
+ $createBlobPagesOptions = new CreateBlobPagesOptions();
+
+ // Create the page blob.
+ createPageBlob($blobRestProxy);
+ waitForEnterKey();
+
+ // Show active ranges (there won't be any for a newly created blob).
+ showActiveRanges($blobRestProxy);
+ waitForEnterKey();
+
+ // Write to the blob, using the third page. The page index is zero-based.
+ writeContent($blobRestProxy, 2, $createBlobPagesOptions);
+ waitForEnterKey();
+
+ // Show active ranges.
+ showActiveRanges($blobRestProxy);
+ waitForEnterKey();
+
+ // Display the contents of the blob.
+ displayContents($blobRestProxy);
+ waitForEnterKey();
+
+ // Write to the blob again, using the first page.
+ writeContent($blobRestProxy, 0, $createBlobPagesOptions);
+ waitForEnterKey();
+
+ // Show active ranges.
+ showActiveRanges($blobRestProxy);
+ waitForEnterKey();
+
+ // Display the contents of the blob.
+ displayContents($blobRestProxy);
+ waitForEnterKey();
+
+ // Starting at the third page, clear one page.
+ clearPages($blobRestProxy, 2, 1, $createBlobPagesOptions);
+ waitForEnterKey();
+
+ // Show active ranges.
+ showActiveRanges($blobRestProxy);
+ waitForEnterKey();
+
+ // Display the contents of the blob.
+ displayContents($blobRestProxy);
+ waitForEnterKey();
+
+ // Delete the blob.
+ // Comment this line out if you want to keep the blob.
+ deleteBlob($blobRestProxy);
+
+ echo "Exiting application.\n";
+}
+catch(ServiceException $serviceException)
+{
+ // Handle exception based on error codes and messages.
+ // Error codes and messages are here:
+ // http://msdn.microsoft.com/en-us/library/windowsazure/dd179439.aspx
+ echo "ServiceException encountered.\n";
+ $code = $serviceException->getCode();
+ $error_message = $serviceException->getMessage();
+ echo "$code: $error_message";
+}
+catch (Exception $exception)
+{
+ echo "Exception encountered.\n";
+ $code = $exception->getCode();
+ $error_message = $exception->getMessage();
+ echo "$code: $error_message";
+}
+
+?>
Please sign in to comment.
Something went wrong with that request. Please try again.