【问题标题】:Get azure blob files from inside Sub Directories using php使用 php 从子目录中获取 azure blob 文件
【发布时间】:2021-03-12 07:05:25
【问题描述】:

我想尝试从 blob 存储中获取文件。我从没有子目录的 blob 存储中成功获得,但是当我尝试从子目录中获取文件时,它不起作用。在子目录的情况下,我使用 $blobName like that test/1.png 其中 test 是文件夹名称和 1.jpg 文件名。

<?php

$storageAccount = 'XXXXXXX';
$containerName = 'XXXXXXXX';
$blobName = 'test/1.png';
$account_key = 'XXXXXXXXXX';

$date = gmdate('D, d M Y H:i:s \G\M\T');
$version = "2019-12-12";

$stringtosign = "GET\n\n\n\n\n\n\n\n\n\n\n\nx-ms-date:". $date . "\nx-ms-version:".$version."\n/".$storageAccount."/".$containerName."/".$blobName;
$signature = 'SharedKey'.' '.$storageAccount.':'.base64_encode(hash_hmac('sha256', $stringtosign, base64_decode($account_key), true));
echo "\n\n" . $signature;

$header = array (
    "x-ms-date: " . $date,       
    "x-ms-version: " . $version,       
    "Authorization: " . $signature
);

$url="https://$storageAccount.blob.core.windows.net/$containerName/$blobName";
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, 'GET' );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $header);
curl_exec ( $ch );
$result = curl_exec($ch);
echo "\n\n" . $result;

if(curl_errno($ch)){
    throw new Exception(curl_error($ch));
}

file_put_contents('D://demo//downloaded.txt', $result); // save the string to a file

curl_close($ch);

【问题讨论】:

  • 怎么样?您的问题解决了吗?
  • @StanleyGong 谢谢,但尚未解决。会不会是权限问题?
  • 不确定根本原因,它对我有用,没有问题。如果这个问题仍然存在,我想你可以尝试使用 storage blob PHP SDK 下载文件,你可以在这里找到演示代码:github.com/Azure/azure-storage-php/blob/…

标签: php azure curl azure-storage azure-storage-files


【解决方案1】:

我已经测试了您的代码,但您的代码似乎没问题,我可以下载我的 .jpg 文件,该文件位于存储容器的某个子目录中:

结果:

我在您的代码中注意到,您正在将 .png file 内容下载到 .txt file 中:

file_put_contents('D://demo//downloaded.txt', $result);

也许这就是你没有得到异常结果的原因。

【讨论】:

猜你喜欢
  • 2019-01-22
  • 1970-01-01
  • 2013-11-28
  • 2017-02-07
  • 2015-06-20
  • 2012-03-11
  • 1970-01-01
  • 2021-10-21
  • 2020-11-29
相关资源
最近更新 更多