gpt4 book ai didi

php - 将程序代码转换为 Blob 的类

转载 作者:行者123 更新时间:2023-12-03 03:57:05 25 4
gpt4 key购买 nike

因此,我构建了以下代码,可以从 Azure 存储库中获取一些图像:

$accountName = 'teststorageaccount';
$accountKey = '**';
$containerName = 'users';

$connectionString = "DefaultEndpointsProtocol=http;AccountName={$accountName};AccountKey={$accountKey}";
$blobClient = ServicesBuilder::getInstance()->createBlobService($connectionString);

try {
$blob_list = $blobClient->listBlobs($containerName);
$blobs = $blob_list->getBlobs();

// Grab all the blob links
foreach ($blobs as $blob) {
echo $blob->getUrl() . "</br>";
}
} catch(ServiceException $e) {
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code.": ".$error_message."<br />";
}

然后我得到以下结果:

http://teststorageaccount.blob.core.windows.net/users/ABREUG.jpg
http://teststorageaccount.blob.core.windows.net/users/ABUKHADA.jpg
http://teststorageaccount.blob.core.windows.net/users/ACHANT.jpg
http://teststorageaccount.blob.core.windows.net/users/ACQUISTE.jpg
<小时/>

现在这是我需要一些帮助的地方..我正在练习构建类里面的所有内容,这就是我已经走了多远:

class AzureStorage
{
private $accountName;
private $accountKey;
private $containerName;

public static function init()
{
return new AzureStorage([
'accountName' => 'teststorageaccount',
'accountKey' => '***',
'containerName' => 'users',
]);
}

/**************************************************************************/

public function __construct(array $data = [])
{
if (count($data) === 0) {
return;
}
$this->load($data);
}

public function load(array $data) : void
{
if (isset($data['accountName'])) {
$this->accountName = $data['accountName'];
}
if (isset($data['accountKey'])) {
$this->accountKey = $data['accountKey'];
}
if (isset($data['containerName'])) {
$this->containerName = $data['containerName'];
}
}

public function connect()
{
$connectionString = "DefaultEndpointsProtocol=https;AccountName={$this->accountName};AccountKey={$this->accountKey}";
$blobClient = ServicesBuilder::getInstance()->createBlobService($connectionString);

return $blobClient;
}

public function getContainers() : array
{
$containers = $this->connect()->listContainers();
return $containers->getContainers();
}

public function getBlobURLs()
{
try {
$blob_list = $this->connect()->listBlobs($this->containerName);
$blobs = $blob_list->getBlobs();

// Grab all the blob links
foreach ($blobs as $blob) {
echo $blob->getUrl() . "</br>";
}
} catch (ServiceException $e) {
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code.": ".$error_message."<br />";
}
}
}
<小时/>

问题:我如何在 getBlobURLs 方法中使用 try 和 catch,然后输出结果?我在类外调用它时无法获得结果。

这就是我正在做的事情:

$test2 = new \AzureStorage\AzureStorage([
'accountName' => 'teststorageaccount',
'accountKey' => '***',
'containerName' => 'users',
]);

现在,如果我调用以下命令(我得到一个容器数组,它工作得很好):

$containers = $test2->getContainers();
//var_dump($containers);

但是如果我执行以下操作(我没有得到任何结果输出):

$blobs = $test2->getBlobURLs();
var_dump($blobs);

有谁知道为什么我可能无法取回网址?

最佳答案

您在 getContainers 函数中有 return 语句,但在 getBlobURLs 中没有。

public function getBlobURLs(){
try {
$blob_list = $this->connect()->listBlobs($this->containerName);
$blobs = $blob_list->getBlobs();

$blobUrls = [];
// Grab all the blob links
foreach ($blobs as $blob) {
$blobUrls[] = $blob->getUrl();
}
return $blobUrls;
} catch (ServiceException $e) {
return false;
}
}

现在,如果你想显示 blob url 列表,那么

$blobs = $test2->getBlobURLs();

if($blobs === false){
echo 'Error while getting blob urls.';
}
else{
foreach($blobs as $blobUrl){
echo $blobUrl . "</br>";
}
}

关于php - 将程序代码转换为 Blob 的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64148861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com