【问题标题】:Code: 403 Value: This request is not authorized to perform this operation. details (if any): AuthorizationFailure代码:403 值:此请求无权执行此操作。详细信息(如果有): AuthorizationFailure
【发布时间】:2020-09-09 23:56:11
【问题描述】:

我得到“Fail: Code: 403 Value: This request is not authorized to perform this operation. details (if any): AuthorizationFailureThis request is not authorized to perform this operation. RequestId:a433a473-e01e-005a- 2fa7-30f90a000000 时间:2020-05-23T02:12:36.6988696Z.”错误。请在下面查看我的代码。

require_once 'vendor/autoload.php';

use MicrosoftAzure\Storage\Blob\BlobRestProxy;
use MicrosoftAzure\Storage\Blob\BlobSharedAccessSignatureHelper;
use MicrosoftAzure\Storage\Blob\Models\CreateBlockBlobOptions;
use MicrosoftAzure\Storage\Common\ServicesBuilder;
use MicrosoftAzure\Storage\Common\Exceptions\ServiceException;
use MicrosoftAzure\Storage\Common\Exceptions\InvalidArgumentTypeException;
use MicrosoftAzure\Storage\Common\Internal\Resources;
use MicrosoftAzure\Storage\Common\Internal\StorageServiceSettings;
use MicrosoftAzure\Storage\Common\Models\Range;
use MicrosoftAzure\Storage\Common\Models\Logging;
use MicrosoftAzure\Storage\Common\Models\Metrics;
use MicrosoftAzure\Storage\Common\Models\RetentionPolicy;
use MicrosoftAzure\Storage\Common\Models\ServiceProperties;
use MicrosoftAzure\Storage\Blob\Models\Block;
use MicrosoftAzure\Storage\Blob\Models\BlockList;
use MicrosoftAzure\Storage\Blob\Models\BlobBlockType;

$blobendpoint = "<Blob service SAS URL>"

$connectionString = "BlobEndpoint=".$blobendpoint.";SharedAccessSignature=sv=2019-10-10&ss=bfqt&srt=sco&sp=rwdlacupx&se=2020-06-06T08:58:51Z&st=2020-05-23T00:58:51Z&sip=52.175.254.10&spr=https,http&sig=<Signature>";


$containerName = "samples";
$blobRestProxy = BlobRestProxy::createBlobService($connectionString);

if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$file_name = $_FILES["resFile"]["name"];

$blob_name = basename($file_name);

$block_list = new BlockList();

define('CHUNK_SIZE', 4 * 1024 * 1024);

try {
    $fptr = fopen($file_name, "rb");
    $index = 1;
    while (!feof($fptr)) {
        $block_id = base64_encode(str_pad($index, 6, "0", STR_PAD_LEFT));
        $block_list->addUncommittedEntry($block_id);
        $data = fread($fptr, CHUNK_SIZE);
        $blobRestProxy->createBlobBlock($containerName, $blob_name, $block_id, $data);
        ++$index;
    }
echo "Index :".$index;
    $blobRestProxy->commitBlobBlocks($containerName, $blob_name, $block_list);
} catch (ServiceException $e) {
    $code = $e->getCode();
    $error_message = $e->getMessage();
    echo $code.": ".$error_message."<br />";
}
}   
?>

<form action="fileuploadblock.php" method="POST" enctype="multipart/form-data">
<input type="file" class="btn btn-default" name="resFile" id="resFile" value="" />
<input type="submit" class="btn btn-default" name = "share "value="Submit" data-inline="true"/>

</form>

编辑: 从 SAS 中删除 IP 后,我得到不同的错误:HTTP 标头之一的值格式不正确。详细信息(如果有):InvalidHeaderValue HTTP 标头之一的值格式不正确。 RequestId:cdb1b3c1-b01e-0078-29fd-303c15000000 时间:2020-05-23T12:28:57.6060563ZContent-Length0.

这是新的 $connectionString

BlobEndpoint=https://<account>.blob.core.windows.net/;SharedAccessSignature=sv=2019-10-10&ss=bfqt&srt=sco&sp=rwdlacupx&se=2020-05-23T20:17:23Z&st=2020-05-23T12:17:23Z&spr=https,http&sig=<SignatureValue>

【问题讨论】:

  • 您的$blobendpoint 是什么样的?是不是像https://account.blob.core.windows.net
  • @GauravMantri-AIS,是的,它是:https://.blob.core.windows.net
  • 我注意到您在 SAS 令牌 (52.175.254.10) 中应用了 IP 地址限制。请确保运行此代码的服务器具有此 IP 地址。
  • 是的,我正在使用这个服务器(52.175.254.10)来运行代码。
  • 我能想到的另一个原因是您的存储帐户位于防火墙后面。

标签: php azure-blob-storage azure-sas


【解决方案1】:

我从文件上传发送空数据..现在我将数据存储在托管服务器中并上传到 blob..

require_once 'vendor/autoload.php';

use MicrosoftAzure\Storage\Blob\BlobRestProxy;
use MicrosoftAzure\Storage\Blob\BlobSharedAccessSignatureHelper;
use MicrosoftAzure\Storage\Blob\Models\CreateBlockBlobOptions;
use MicrosoftAzure\Storage\Common\ServicesBuilder;
use MicrosoftAzure\Storage\Common\Exceptions\ServiceException;
use MicrosoftAzure\Storage\Common\Exceptions\InvalidArgumentTypeException;
use MicrosoftAzure\Storage\Common\Internal\Resources;
use MicrosoftAzure\Storage\Common\Internal\StorageServiceSettings;
use MicrosoftAzure\Storage\Common\Models\Range;
use MicrosoftAzure\Storage\Common\Models\Logging;
use MicrosoftAzure\Storage\Common\Models\Metrics;
use MicrosoftAzure\Storage\Common\Models\RetentionPolicy;
use MicrosoftAzure\Storage\Common\Models\ServiceProperties;
use MicrosoftAzure\Storage\Blob\Models\Block;
use MicrosoftAzure\Storage\Blob\Models\BlockList;
use MicrosoftAzure\Storage\Blob\Models\BlobBlockType;


 $connectionString = "BlobEndpoint=https://<account>.blob.core.windows.net/;SharedAccessSignature=sv=2019-10-10&ss=bfqt&srt=sco&sp=rwdlacupx&se=2021-05-26T17:39:04Z&st=2020-05-27T09:39:04Z&spr=https,http&sig=<Signature>";
$containerName = "samples";
$blobRestProxy = BlobRestProxy::createBlobService($connectionString);

/*****************Upload file to hosting ********/
function uploadFile($fieldName, $fileType)
{
    $flg = 0;
    $MaxID = "";
    $ext = "";
    $uploadfile = "";
    if (isset($fieldName) AND $fieldName['name'] != '')
    {
        $flg = 1;
        $allowed_filetypes = $fileType;
        $max_filesize = 40548576;
        $filename = $fieldName['name'];
        $ext = substr($filename, strpos($filename, '.'), strlen($filename) - 1);
        if($ext==".xlsx")
            $ext=".xls";
        if (!in_array($ext, $allowed_filetypes))
            echo "<h1>The file you attempted to upload is not allowed...</h1>";
        else if (filesize($fieldName['tmp_name']) > $max_filesize)
            echo "<h1>The file you attempted to upload is too large...</h1>";
        else 
        {
            $uploadfile =  $filename;//$MaxID . $ext;
            if (move_uploaded_file($fieldName['tmp_name'], $uploadfile) == FALSE)
            {
                echo "<h1>Error in Uploading File...</h1>";
                $MaxID = "";
            }
            else
                $MaxID = $filename;
            echo "file uploaded success!!";
        }
    }
    return $MaxID;
}
/***************************************/   
if(isset($_POST['share']))
{
    if($_FILES['resFile']['name']!="")
    {
      $file_name=uploadFile($_FILES['resFile'],array(".xls",".xlsx",".csv"));
    }
    if ($file_name != '')
    {
    $blob_name = basename($file_name);

$block_list = new BlockList();

define('CHUNK_SIZE', 1 * 1024 * 1024);

try {
    $fptr = fopen($file_name, "rb");
    $index = 1;
    while (!feof($fptr)) {

        $block_id = base64_encode(str_pad($index, 6, "0", STR_PAD_LEFT));

        $block_list->addUncommittedEntry($block_id);
        $data = fread($fptr, CHUNK_SIZE);
        //echo "Data :".$block_id;
        $blobRestProxy->createBlobBlock($containerName, $blob_name, $block_id, $data);
        //echo "ID :".$block_id;
        ++$index;
    }

    $blobRestProxy->commitBlobBlocks($containerName, $blob_name, $block_list);
} catch (ServiceException $e) {
    $code = $e->getCode();
    $error_message = $e->getMessage();
    echo "Error Code :".$code."Error Message : ".$error_message."<br />";
}
}
else 
{
    echo " No file selected";
}
}   
?>
<html lang="en">
<head>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="upload_excel" 
enctype="multipart/form-data">
<input type="file" class="btn btn-default" name="resFile" id="resFile" value="" />
<input type="submit" class="btn btn-default" name = "share" value="Submit" data- 
inline="true"/>
<!--<div id="res"></div>-->

</form>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</body>
</html>

【讨论】:

    猜你喜欢
    • 2020-08-16
    • 2019-07-24
    • 2019-10-01
    • 2020-11-29
    • 2021-03-28
    • 2020-06-21
    • 1970-01-01
    • 1970-01-01
    • 2023-02-01
    相关资源
    最近更新 更多