Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to upload large block blobs to Azure storage by chunking them using the Azure PHP SDK, but I can't find a sample of how to do this anywhere, so how do I do it using the Azure PHP SDK?

share|improve this question
 
Please see this question: stackoverflow.com/questions/14889413/…. HTH. –  Gaurav Mantri Jul 12 at 12:43
 
@GauravMantri The answer is not useful, because the Azure SDK for PHP doesn't provide the API mentioned in the answer. Also I've already looked at the source, but it's not clear how the API is to be used - there appears to be a specific workflow/chain of methods that needs to be called. –  Ivan Zlatev Jul 12 at 12:52

1 Answer

up vote 1 down vote accepted

Here's a working example. I used the same two functions mentioned in my link above.

<?php 
require_once 'WindowsAzure/WindowsAzure.php';
use WindowsAzure\Common\ServicesBuilder;
use WindowsAzure\Common\ServiceException;
use WindowsAzure\Blob\Models\Block;
use WindowsAzure\Blob\Models\BlobBlockType;
define('CHUNK_SIZE', 1024*1024);//Block Size = 1 MB
try {

    $connectionString = "UseDevelopmentStorage=true";
    $instance = ServicesBuilder::getInstance();
    $blobRestProxy = $instance -> createBlobService($connectionString);
    $containerName = "mycontainer";
    $blobName = "DSC01166.jpg";
    $content = fopen("d:\DSC01166.jpg", "rb");
    $index = 0;
    $continue = True;
    $counter = 1;
    $blockIds = array();
    while (!feof($content))
    {
        $blockId = str_pad($counter, 6, "0", STR_PAD_LEFT);
        $block = new Block();
        $block -> setBlockId(base64_encode($blockId));
        $block -> setType("Uncommitted");
        array_push($blockIds, $block);
        echo $blockId . " | " . base64_encode($blockId) . " | " . count($blockIds);
        echo " \n ";
        echo " -----------------------------------------";
        $data=fread($content, CHUNK_SIZE);
        echo "Read " . strlen($data) . " of data from file";
        echo " \n ";
        echo " -----------------------------------------";
        echo " \n ";
        echo " -----------------------------------------";
        echo "Uploading block #: " . $blockId + " into blob storage. Please wait.";
        echo " -----------------------------------------";
        echo " \n ";
        $blobRestProxy -> createBlobBlock($containerName, $blobName, base64_encode($blockId), $data);
        echo "Uploaded block: " . $blockId . " into blob storage. Please wait";
        echo " \n ";
        echo " -----------------------------------------";
        echo " \n ";
        $counter = $counter + 1;
    }
    fclose($content); 
    echo "Now committing block list. Please wait.";
    echo " -----------------------------------------";
    echo " \n ";
    echo "hello";
    $blobRestProxy -> commitBlobBlocks($containerName, $blobName, $blockIds);
    echo " -----------------------------------------";
    echo " \n ";
    echo "Blob created successfully.";
}
catch(Exception $e){
    // Handle exception based on error codes and messages.
    // Error codes and messages are here: 
    // http://msdn.microsoft.com/en-us/library/windowsazure/dd179439.aspx
    $code = $e->getCode();
    $error_message = $e->getMessage();
    echo $code.": ".$error_message."<br />";
}
?>

Hope this helps.

share|improve this answer
 
Thanks for the sample! I will look at it as soon as I can and accept the answer once I test it. –  Ivan Zlatev Jul 12 at 17:16
 
Hi, this doesn't work. After uploading 5-6 blocks it hangs and then it throws a malformed response error. Also there is a small typo -> count(blockIds) should be count($blockIds). Ideas? –  Ivan Zlatev Jul 15 at 15:12
 
Can you share the exact error you're getting? It would also help if you trace your request/responses through a tool like Fiddler so that you know exactly what is being sent across the wire. Unfortunately my knowledge about PHP is limited to none. I tried this with storage emulator and uploaded about 4 MB file using the code above and it worked ok for me. –  Gaurav Mantri Jul 15 at 16:25
 
I figured out that HTTP works but HTTPS doesn't, so I am investigating potential CA/certificate issues. –  Ivan Zlatev Jul 17 at 17:52
 
Any thoughts on what might be the HTTPS issue on linux? I've added the blob storage certificate as trusted just in case and still isn't working (same error) –  Ivan Zlatev Jul 17 at 18:53
show 1 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.