gpt4 book ai didi

php - 如何使用 Azure PHP SDK 通过分块来上传大块 blob?

转载 作者:行者123 更新时间:2023-12-03 01:56:14 25 4
gpt4 key购买 nike

我想使用 Azure PHP SDK 将大块 blob 分块上传到 Azure 存储,但我无法在任何地方找到如何执行此操作的示例,那么如何使用 Azure PHP SDK 执行此操作?

最佳答案

这是一个工作示例。我使用了上面链接中提到的相同的两个函数。

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

希望这有帮助。

关于php - 如何使用 Azure PHP SDK 通过分块来上传大块 blob?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17612325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com