2013-07-12 49 views
0

我想通過使用Azure PHP SDK將大塊blob上傳到Azure存儲,但我找不到如何在任何位置執行此操作的示例,那麼如何使用Azure PHP SDK執行此操作?如何通過分塊將大塊blob與Azure PHP SDK一起上傳?

+0

請參閱此問題:http://stackoverflow.com/questions/14889413/how-to-upload-a-blob-to-windows-azure-by-splitting-it-into-chunks-and-calling- PU。 HTH。 –

+0

@GauravMantri答案沒有用,因爲Azure SDK for PHP不提供答案中提到的API。此外,我已經看過源代碼,但不清楚如何使用API​​ - 似乎需要調用特定的工作流程/方法鏈。 –

回答

1

下面是一個工作示例。我使用了上面鏈接中提到的相同的兩個函數。

<?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 />"; 
} 
?> 

希望這會有所幫助。

+0

感謝您的示例!我會盡快查看並在接受測試後接受答案。 –

+0

嗨,這是行不通的。上傳5-6塊後,它會掛起,然後引發錯誤的響應錯誤。還有一個小錯字 - >'count(blockIds)'應該是'count($ blockIds)'。想法? –

+0

你能分享你得到的確切的錯誤嗎?如果您通過像Fiddler這樣的工具來追蹤您的請求/響應,那麼這也將有所幫助,以便您確切知道通過電話發送的內容。不幸的是,我對PHP的知識僅限於無。我用存儲模擬器嘗試了這一點,並使用上面的代碼上傳了大約4 MB的文件,它對我來說工作正常。 –

相關問題