错误 404:azure storage create a new Block
Error 404: azure storage create a new Block
我正在尝试在 Azure 存储上上传特定的大图像文件(从 1 MB 到 5 GB)。
对于最大 2MB 的小文件,其他文件没有问题我有 "The resource you are looking for has been removed, had its name changed, or is temporarily unavailable." 答案。
因此,我尝试使用以下代码将文件分成几个块:
require_once 'vendor/autoload.php';
use WindowsAzure\Common\ServicesBuilder;
use WindowsAzure\Common\ServiceException;
use WindowsAzure\Blob\Models\CreateContainerOptions;
use WindowsAzure\Blob\Models\PublicAccessType;
use WindowsAzure\Blob\Models\Block;
use WindowsAzure\Blob\Models\BlobBlockType;
define('CHUNK_SIZE', 1024*1024); //Block Size = 1MB
$connectionString = "DefaultEndpointsProtocol=....";
$blobRestProxy = ServicesBuilder::getInstance()->createBlobService($connectionString);
$uploadfile =$_FILES['fileToUpload']['tmp_name'];
$content = fopen($uploadfile, "r");
$blob_name = $_FILES['fileToUpload']['name'];
$container_name = "natives";
try{
$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);
$data = fread($content, CHUNK_SIZE);
echo " \ n";
echo "Read ". strlen($data) . "of data from file <br />";
echo "Uploading block #:". $blockId . "into blob storage. Please wait. <br />";
$blobRestProxy -> createBlobBlock($container_name, $blob_name, base64_encode($blockIds), $data);
echo "Uploaded block: ".$blockId . "into blob storage. <br />";
$counter = $counter + 1;
}
echo "Now committing block list. Please wait. <br />";
$blobRestProxy -> commitBlobBlocks($container_name, $blob_name, $blockIds);
echo "Blob created successfully. <br />";
}
catch(Exception $e){
echo "Error <br />";
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code.": ".$error_message."<br />";
echo "Files : ".$_FILES['fileToUpload']['name']."<br />";
echo "content " . $content."<br />";
}
fclose($content);
使用调试模式,问题来自
$block = new Block();
然而,我包含了 "Block" 类,所以我没有理解这个问题,我不知道如何获得有关此错误的更多信息。我在开发者页面 (POST) 上只有一个白页和一个错误 404。
所以我尝试了您的代码,但没有收到您遇到的 404 错误。但是我确实发现您的代码存在问题。本质上,您需要更改以下代码行:
$blobRestProxy -> createBlobBlock($container_name, $blob_name, base64_encode($blockIds), $data);
至
$blobRestProxy -> createBlobBlock($container_name, $blob_name, base64_encode($blockId), $data);
完成后,我就可以在我的存储帐户中正确上传文件了。
你的代码有误:
$blobRestProxy -> createBlobBlock($container_name, $blob_name, base64_encode($blockIds), $data);
应该是:
$blobRestProxy -> createBlobBlock($container_name, $blob_name, base64_encode($blockId), $data);
在您的 while 上下文中将 $blockIds
修改为 $blockId
。
我已经测试了您的代码,可以通过修改将数十 MB 大小的文件上传到 blob 存储。效果不错。
顺便说一句,如果你得到最大执行时间异常,你可以在PHP脚本中设置set_time_limit()
函数或者在[=28]中设置max_execution_time
配置=].
我进行了更正并且有效,它适用于最大 10Mbs 的文件。所以我尝试使用另一个 62 Mbs 的文件,我得到以下答案:
Read 0 of data from file
Uploading block #:000001 into blob storage. Please wait.
Error
0: 'blob' can't be NULL or empty.
Files :
content
这是因为 $_FILES 中文件的大小?或者还有别的东西?
我正在尝试在 Azure 存储上上传特定的大图像文件(从 1 MB 到 5 GB)。
对于最大 2MB 的小文件,其他文件没有问题我有 "The resource you are looking for has been removed, had its name changed, or is temporarily unavailable." 答案。
因此,我尝试使用以下代码将文件分成几个块:
require_once 'vendor/autoload.php';
use WindowsAzure\Common\ServicesBuilder;
use WindowsAzure\Common\ServiceException;
use WindowsAzure\Blob\Models\CreateContainerOptions;
use WindowsAzure\Blob\Models\PublicAccessType;
use WindowsAzure\Blob\Models\Block;
use WindowsAzure\Blob\Models\BlobBlockType;
define('CHUNK_SIZE', 1024*1024); //Block Size = 1MB
$connectionString = "DefaultEndpointsProtocol=....";
$blobRestProxy = ServicesBuilder::getInstance()->createBlobService($connectionString);
$uploadfile =$_FILES['fileToUpload']['tmp_name'];
$content = fopen($uploadfile, "r");
$blob_name = $_FILES['fileToUpload']['name'];
$container_name = "natives";
try{
$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);
$data = fread($content, CHUNK_SIZE);
echo " \ n";
echo "Read ". strlen($data) . "of data from file <br />";
echo "Uploading block #:". $blockId . "into blob storage. Please wait. <br />";
$blobRestProxy -> createBlobBlock($container_name, $blob_name, base64_encode($blockIds), $data);
echo "Uploaded block: ".$blockId . "into blob storage. <br />";
$counter = $counter + 1;
}
echo "Now committing block list. Please wait. <br />";
$blobRestProxy -> commitBlobBlocks($container_name, $blob_name, $blockIds);
echo "Blob created successfully. <br />";
}
catch(Exception $e){
echo "Error <br />";
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code.": ".$error_message."<br />";
echo "Files : ".$_FILES['fileToUpload']['name']."<br />";
echo "content " . $content."<br />";
}
fclose($content);
使用调试模式,问题来自
$block = new Block();
然而,我包含了 "Block" 类,所以我没有理解这个问题,我不知道如何获得有关此错误的更多信息。我在开发者页面 (POST) 上只有一个白页和一个错误 404。
所以我尝试了您的代码,但没有收到您遇到的 404 错误。但是我确实发现您的代码存在问题。本质上,您需要更改以下代码行:
$blobRestProxy -> createBlobBlock($container_name, $blob_name, base64_encode($blockIds), $data);
至
$blobRestProxy -> createBlobBlock($container_name, $blob_name, base64_encode($blockId), $data);
完成后,我就可以在我的存储帐户中正确上传文件了。
你的代码有误:
$blobRestProxy -> createBlobBlock($container_name, $blob_name, base64_encode($blockIds), $data);
应该是:
$blobRestProxy -> createBlobBlock($container_name, $blob_name, base64_encode($blockId), $data);
在您的 while 上下文中将 $blockIds
修改为 $blockId
。
我已经测试了您的代码,可以通过修改将数十 MB 大小的文件上传到 blob 存储。效果不错。
顺便说一句,如果你得到最大执行时间异常,你可以在PHP脚本中设置set_time_limit()
函数或者在[=28]中设置max_execution_time
配置=].
我进行了更正并且有效,它适用于最大 10Mbs 的文件。所以我尝试使用另一个 62 Mbs 的文件,我得到以下答案:
Read 0 of data from file
Uploading block #:000001 into blob storage. Please wait.
Error
0: 'blob' can't be NULL or empty.
Files :
content
这是因为 $_FILES 中文件的大小?或者还有别的东西?