【问题标题】:Uncaught GuzzleHttp\Exception\ConnectException: cURL error 7未捕获的 GuzzleHttp\Exception\ConnectException:cURL 错误 7
【发布时间】:2021-02-20 23:35:02
【问题描述】:

所以我正在使用 azure-storage-php 库 (https://github.com/Azure/azure-storage-php) 并且一切正常,我的所有脚本都可以正常工作,但我不时收到以下错误:

PHP Fatal error:  Uncaught GuzzleHttp\Exception\ConnectException: cURL error 7: Failed to connect to fz.blob.core.windows.net port 443: Connection refused (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)

有谁知道我可能做错了什么?以下是我主要基于库的代码:

public function connect()
{
    $connection_string = "DefaultEndpointsProtocol=https;
    AccountName={$this->account_name};
    AccountKey={$this->account_key}";
    $blob_client = BlobRestProxy::createBlobService($connection_string);

    return $blob_client;
}

public function generate_sas_url(string $blob, string $duration): string
    {
        $sas_helper = new BlobSharedAccessSignatureHelper(
            $this->account_name,
            $this->account_key,
        );

        $token = $sas_helper->generateBlobServiceSharedAccessSignatureToken(
            Resources::RESOURCE_TYPE_BLOB,
            "$this->container_name/{$blob}",
            'r',
            (new \DateTime())->modify($duration),
            (new \DateTime()),
            '',
            'https',
        );

        $connection_string_sas = Resources::BLOB_ENDPOINT_NAME .
            '=' .
            'https://' .
            $this->account_name .
            '.' .
            Resources::BLOB_BASE_DNS_NAME . ';' .
            Resources::SAS_TOKEN_NAME . '=' .
            $token;

        $blob_client_sas = BlobRestProxy::createBlobService($connection_string_sas);

        $blob_url_sas = sprintf(
            '%s%s?%s',
            (string)$blob_client_sas->getPsrPrimaryUri(),
            "$this->container_name/{$blob}",
            $token,
        );

        return $blob_url_sas;
    }

【问题讨论】:

  • 它有时会在那个 URL 上工作,有时会失败吗?或者它对某些 URL 有效但在其他 URL 上失败?
  • 也有这个问题,遗憾的是我再也找不到那个代码了。检查它是否在没有 https 的情况下工作。如果没记错的话,在 azure 中的某处有一个 https 设置。
  • shelly1337 Greg 谢谢大家!我最终找到了问题,看起来我需要在我调用它的任何地方尝试/捕获我的连接。

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


【解决方案1】:

这似乎解决了问题:

public function blob_exists(string $name, string $prefix = ''): int
{
    try {
        $list_blobs_options = new ListBlobsOptions();
        $list_blobs_options->setPrefix($prefix . $name . '.jpg');
        $results = $this->connect()->listBlobs(
            $this->container_name, $list_blobs_options
        );
        return count($results->getBlobs());
    } catch (ConnectException $e) {
        $code = $e->getCode();
        $error_message = $e->getMessage();
        echo $code . ": " . $error_message . PHP_EOL;
        return 0;
    }
}

我有一个方法可以检查 blob 是否存在,请确保尝试/捕获您的连接器方法并包括 catch (ConnectException)

【讨论】:

    猜你喜欢
    • 2023-01-18
    • 2018-07-06
    • 1970-01-01
    • 2021-02-06
    • 1970-01-01
    • 1970-01-01
    • 2017-07-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多