Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.

Sponsors

News

 
 
 
DZone MVB

Links

Social

Using Windows Azure BLOB storage with PHP

Windows Azure My last posting described how to read and write files located in Windows Azure cloud storage. In this posting I will show you how to do almost same thing using PHP. We will use Windows Azure SDK for PHP. The purpose of this example is to show you how simple it is to use Windows Azure storage services in your PHP applications.

Preparing for example

I expect you have everything needed for this example:

If you need more information then please check out these resources:

Before we start scripting let’s make sure that our environment is configured correctly:

  • copy folder named Microsoft to your script folder (or somewhere else where PHP file include functions can access it),
  • let PHP display errors to page if you don’t prefer to read log files,
  • make sure you have cURL extension enabled in php.ini file.

Reading and writing cloud storage files

Our script does two simple operations – it reads one file from Windows Azure BLOB storage and writes the other one there.  Note that in the beginning of script I turn on error reporting and set content type as plain text. If there are any errors or warnings then this information is written out and formatted so it is easy to read.


<?php
error_reporting(E_ALL);
header('Content-type: text/plain');
require_once 'Microsoft/WindowsAzure/Storage/Blob.php';

// Connect to Windows Azure cloud storage
$client = new Microsoft_WindowsAzure_Storage_Blob(
                    "blob.core.windows.net",
                    "<ACCOUNT>",
                    "<KEY>"
                    );

// Read file Data.xml from container called dataset
$localpath = getcwd() . '\Data.xml';
$client->getBlob('dataset', 'Data.xml', $localpath);

// Write file example.txt to container called dataset
$localpath = getcwd() . '\example.txt';
$result = $client->putBlob('dataset', 'example.txt', $localpath);
?>

Some notes too. Windows Azure SDK for PHP handles cloud storage address as raw host name. <ACCOUNT> – you cloud storage service name – is used with raw host name to build up correct cloud storage URL. It is handled by library and you don’t have to worry about it. <KEY> is base64 encoded key and it is generated when you create your BLOB storage service.

Conclusion

It is very easy to communicate with Windows Azure cloud storage services using PHP and Windows Azure SDK for PHP. The code example above shows also one good thing: we wrote very basic and extremely simple code to get our works done – even complete beginners are able to use Windows Azure cloud storage in their scripts.


kick it on DotNetKicks.com pimp it 顶 Progg it Shout it
Shout it!
Posted: Dec 05 2009, 03:20 PM by DigiMortal | with 12 comment(s)
Filed under: , ,

Comments

DotNetShoutout said:

Thank you for submitting this cool story - Trackback from DotNetShoutout

# December 5, 2009 8:37 AM

PimpThisBlog.com said:

Thank you for submitting this cool story - Trackback from PimpThisBlog.com

# December 5, 2009 8:39 AM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# December 5, 2009 8:40 AM

progg.ru said:

Thank you for submitting this cool story - Trackback from progg.ru

# December 5, 2009 8:41 AM

9eFish said:

9efish.感谢你的文章 - Trackback from 9eFish

# December 5, 2009 8:43 AM

Servefault.com said:

Thank you for submitting this cool story - Trackback from Servefault.com

# December 5, 2009 8:44 AM

Gunnar Peipman's ASP.NET blog said:

Here you can find my list of Windows Azure postings Using Windows Azure BLOB storage with PHP Windows

# December 5, 2009 9:01 AM

Twitter Trackbacks for Using Windows Azure BLOB storage with PHP - Gunnar Peipman's ASP.NET blog [asp.net] on Topsy.com said:

Pingback from  Twitter Trackbacks for                 Using Windows Azure BLOB storage with PHP - Gunnar Peipman's ASP.NET blog         [asp.net]        on Topsy.com

# December 5, 2009 10:17 AM

uberVU - social comments said:

This post was mentioned on Twitter by gpeipman: New blog post: http://tinyurl.com/ycnbfkg - Using Windows Azure BLOB storage with PHP

# December 5, 2009 1:00 PM

Andy said:

I always enjoy learning what other people think about Microsoft Azure and how they use it. Check out my very own tool CloudBerry Explorer that helps to

managing Azure Blog Storage . It is a freeware. azure.cloudberrylab.com

# December 8, 2009 2:42 AM

Jerry said:

I like a scripting language and its flexibility. Myself not a PHP programmer but was a Perl programmer before.

One tool to help is Gladinet Cloud Desktop that can map Azure Blob Storage as a network drive. So after you write the PHP code, you can use Gladinet to verify or use Gladinet to upload/download files and use PHP code to verify.

See http://gladinet.blogspot.com for some articles related to Azure.

# December 30, 2009 2:14 PM

Open Source Role Call for Windows Azure « SyntaxC4 said:

Pingback from  Open Source Role Call for Windows Azure &laquo;  SyntaxC4

# July 24, 2012 11:57 AM