使用PHP/LARAVEL更改Azure Blob存储的DefaulServiceVersion &



有没有人知道如何通过php/laravel设置blob服务属性?我在c#上看到了他们使用集成SDK的答案。我试过了,但我无法开始,因为关于如何使用azure PHP SDK for Laravel的信息很少,或者我不明白。

我尝试过但失败了:

$date = gmdate('D, d M Y H:i:s GMT');
$account_name = "**************";
$containername = "media";
$access_key = "access_key_from_portal";
$canonicalizedHeaders  = "x-ms-date:$datenx-ms-version:2017-11-09";
// $canonicalizedResource = "/$account_name/$containernamencomp:listnrestype:container";
// $canonicalizedResource = "/$account_namenrestype:servicencomp:properties";
$canonicalizedResource = "/$account_name /$containernamencomp:metadatanrestype:containerntimeout:20";

$arraysign = array();
$arraysign[] = 'PUT';                     /*HTTP Verb*/
$arraysign[] = '';                        /*Content-Encoding*/
$arraysign[] = '';                        /*Content-Language*/
$arraysign[] = '';                        /*Content-Length (include value when zero)*/
$arraysign[] = '';                        /*Content-MD5*/
$arraysign[] = '';                        /*Content-Type*/
$arraysign[] = '';                        /*Date*/
$arraysign[] = '';                        /*If-Modified-Since */
$arraysign[] = '';                        /*If-Match*/
$arraysign[] = '';                        /*If-None-Match*/
$arraysign[] = '';                        /*If-Unmodified-Since*/
$arraysign[] = '';                        /*Range*/
$arraysign[] = $canonicalizedHeaders;     /*CanonicalizedHeaders*/
$arraysign[] = $canonicalizedResource;    /*CanonicalizedResource*/
$stringtosign = implode("n", $arraysign);
$signature = 'SharedKey' . ' ' . $account_name . ':' . base64_encode(hash_hmac('sha256', utf8_encode($stringtosign), base64_decode($access_key)));
$endpoint = 'https://' . $account_name . '.blob.core.windows.net';
// $endpoint = 'https://management.azure.com/subscriptions/df4efeca-7f1e-44cd-b037-5ced337b68b0/resourceGroups/PiHealthcare/providers/Microsoft.Storage/storageAccounts/preceptorshipblobstorage?api-version=2019-06-01';
$url = $endpoint . '/' . '?restype=service&comp=properties';
$headers = [
"Content-Length:0",
"x-ms-date:{$date}",
'x-ms-version:2017-11-09',
'Accept:application/json;odata=nometadata',
"Authorization:{$signature}"
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response  = curl_exec($ch);
echo curl_error($ch);
curl_close($ch);
echo '<pre>';
print_r($response);
echo '<hr>';
print_r($signature);
echo '<pre>';
print_r($date);
echo '<hr>';
print_r($stringtosign);
echo '<hr>';
print_r($canonicalizedHeaders);

我一直得到相同的错误:在HTTP请求中找到的MAC签名与任何计算的签名都不相同

我想我没有以适当的方式编码ShareKey ?

人吗?

如果你想在PHP应用程序中设置blob服务属性,你可以使用Azure存储PHP SDK来实现它。

例如

$connectionString = 'DefaultEndpointsProtocol=https;AccountName=<yourAccount>;AccountKey=<yourKey>';
$blobClient = BlobRestProxy::createBlobService($connectionString);
echo "Get Blob Service properties" . PHP_EOL;
$originalProperties = $blobClient->getServiceProperties();
// Set blob service properties
echo "Set Blob Service properties" . PHP_EOL;
$retentionPolicy = new RetentionPolicy();
$retentionPolicy->setEnabled(true);
$retentionPolicy->setDays(10);
$logging = new Logging();
$logging->setRetentionPolicy($retentionPolicy);
$logging->setVersion('1.0');
$logging->setDelete(true);
$logging->setRead(true);
$logging->setWrite(true);
$metrics = new Metrics();
$metrics->setRetentionPolicy($retentionPolicy);
$metrics->setVersion('1.0');
$metrics->setEnabled(true);
$metrics->setIncludeAPIs(true);
$serviceProperties = new ServiceProperties();
$serviceProperties->setLogging($logging);
$serviceProperties->setHourMetrics($metrics);
$blobClient->setServiceProperties($serviceProperties);
// revert back to original properties
echo "Revert back to original service properties" . PHP_EOL;
$blobClient->setServiceProperties($originalProperties->getValue());
echo "Service properties sample completed" . PHP_EOL;

相关内容

  • 没有找到相关文章

最新更新