So I'm using the azure-storage-php
library (https://github.com/Azure/azure-storage-php) and everything works perfectly and all of my scripts work, but I'm getting the following error from time to time:
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)
Does anyone know what I might be doing wrong? Below is the code that I have that is mainly based on the library:
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;
}
This seemed to have fixed the issue:
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;
}
}
I've have a method that checks if blobs exist, make sure to try/catch your connector method and also include catch (ConnectException)
.