簡體   English   中英

使用 php 從子目錄中獲取 azure blob 文件

[英]Get azure blob files from inside Sub Directories using php

我想嘗試從 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);

我已經測試了您的代碼,但您的代碼似乎沒問題,我可以下載 my.jpg 文件,該文件位於存儲容器的某個子目錄中: 在此處輸入圖像描述

結果: 在此處輸入圖像描述

我注意到在您的代碼中,您正在將.png file內容下載到.txt file

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

也許這就是您沒有得到異常結果的原因。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM