Azure / azure-storage-php

Microsoft Azure Storage Library for PHP
MIT License
217 stars 198 forks source link

Microsoft Azure Storage PHP Client Libraries (Deprecated)

This project will be in Community Support until 17 March 2024. After this date the project and associated client libraries will be retired permanently. For more details on the retirement and alternatives to using this project, visit Retirement notice: The Azure Storage PHP client libraries will be retired on 17 March 2024.


This project provides a set of PHP client libraries that make it easy to access Microsoft Azure Storage services (blobs, tables, queues and files). For documentation on how to host PHP applications on Microsoft Azure, please see the Microsoft Azure PHP Developer Center.

Note

  • If you are looking for the Service Bus, Service Runtime, Service Management or Media Services libraries, please visit Azure SDK for PHP.
  • If you need big file (larger than 2GB) or 64-bit integer support, please install PHP 7 64-bit version.

Features

Please check details on API reference documents.

Minimum Requirements

Download Source Code

To get the source code from GitHub, type

git clone https://github.com/Azure/azure-storage-php.git
cd ./azure-storage-php

Install via Composer

  1. Create a file named composer.json in the root of your project and add the following code to it:
{
  "require": {
    "microsoft/azure-storage-blob": "*",
    "microsoft/azure-storage-table": "*",
    "microsoft/azure-storage-queue": "*",
    "microsoft/azure-storage-file": "*"
  }
}
  1. Download composer.phar in your project root.

  2. Open a command prompt and execute this in your project root

php composer.phar install

Usage

There are four basic steps that have to be performed before you can make a call to any Microsoft Azure Storage API when using the libraries.

require_once "vendor/autoload.php"; 
use MicrosoftAzure\Storage\Blob\BlobRestProxy;

To process exceptions you need:

use MicrosoftAzure\Storage\Common\ServiceException;
DefaultEndpointsProtocol=[http|https];AccountName=[yourAccount];AccountKey=[yourKey]

Or:

BlobEndpoint=myBlobEndpoint;QueueEndpoint=myQueueEndpoint;TableEndpoint=myTableEndpoint;FileEndpoint=myFileEndpoint;SharedAccessSignature=sasToken

Or if AAD authentication is used:

BlobEndpoint=myBlobEndpoint;QueueEndpoint=myQueueEndpoint;TableEndpoint=myTableEndpoint;FileEndpoint=myFileEndpoint;AccountName=[yourAccount]

Note that account name is required.

$blobClient = BlobRestProxy::createBlobService($connectionString);
$tableClient = TableRestProxy::createTableService($connectionString);
$queueClient = QueueRestProxy::createQueueService($connectionString);
$fileClient = FileRestProxy::createFileService($connectionString);

Or for AAD authentication:

$blobClient = BlobRestProxy::createBlobServiceWithTokenCredential($token, $connectionString);
$queueClient = QueueRestProxy::createQueueServiceWithTokenCredential($token, $connectionString);

Note that Blob and Queue service supports AAD authentication.

Using Middlewares

To specify the middlewares, user have to create an array with middlewares and put it in the $requestOptions with key 'middlewares'. The sequence of the array will affect the sequence in which the middleware is invoked. The $requestOptions can usually be set in the options of an API call, such as MicrosoftAzure\Storage\Blob\Models\ListBlobOptions.

The user can push the middleware into the array with key 'middlewares' in services' $_options instead when creating them if the middleware is to be applied to each of the API call for a rest proxy. These middlewares will always be invoked after the middlewares in the $requestOptions. e.g.:

$tableClient = TableRestProxy::createTableService(
    $connectionString,
    $optionsWithMiddlewares
);

Each of the middleware should be either an instance of a sub-class that implements MicrosoftAzure\Storage\Common\Internal\IMiddleware, or a callable that follows the Guzzle middleware implementation convention.

User can create self-defined middleware that inherits from MicrosoftAzure\Storage\Common\Internal\Middlewares\MiddlewareBase.

Retrying failures

You can use bundled middlewares to retry requests in case they fail for some reason. First you create the middleware:

$retryMiddleware = RetryMiddlewareFactory::create(
    RetryMiddlewareFactory::GENERAL_RETRY_TYPE,  // Specifies the retry logic
    3,  // Number of retries
    1000,  // Interval
    RetryMiddlewareFactory::EXPONENTIAL_INTERVAL_ACCUMULATION,  // How to increase the wait interval
    true  // Whether to retry connection failures too, default false
);

Then you add the middleware when creating the service as explained above:

$optionsWithMiddlewares = [
    'middlewares' = [
        $retryMiddleware
    ],
];
$tableClient = TableRestProxy::createTableService(
    $connectionString,
    $optionsWithMiddlewares
);

Or by pushing it to the existing service:

$tableClient->pushMiddleware($retryMiddleware);

Following errors are not retried in current retry middleware:

Retry types

Interval accumulations

Using proxies

To use proxies during HTTP requests, set system variable HTTP_PROXY and the proxy will be used.

Troubleshooting

Error: Unable to get local issuer certificate

cURL can't verify the validity of Microsoft certificate when trying to issue a request call to Azure Storage Services. You must configure cURL to use a certificate when issuing https requests by the following steps:

  1. Download the cacert.pem file from cURL site.

  2. Then either:

    • Open your php.ini file and add the following line:

      curl.cainfo = "<absolute path to cacert.pem>"

      OR

    • Point to the cacert in the options when creating the Relevant Proxy.

      //example of creating the FileRestProxy
      $options["http"] = ["verify" => "<absolute path to cacert.pem>"];
      FileRestProxy::createFileService($connectionString, $options);

Code samples

You can find samples in the samples folder.

Migrate from Azure SDK for PHP

If you are using Azure SDK for PHP to access Azure Storage Service, we highly recommend you to migrate to this SDK for faster issue resolution and quicker feature implementation. We are working on supporting the latest service features as well as improvement on existing APIs.

For now, Microsoft Azure Storage PHP client libraries share almost the same interface as the storage blobs, tables, queues and files APIs in Azure SDK for PHP. However, there are some minor breaking changes need to be addressed during your migration. You can find the details in BreakingChanges.md.

Need Help?

Be sure to check out the Microsoft Azure Developer Forums on Stack Overflow and github issues if you have trouble with the provided code.

Please note this project will be in Community Support and Azure Storage team commits to validate and release every quarter, as long as there are PRs from community. Azure Storage team is unable to continue to add new features or provide bugfixes.

Contribute Code or Provide Feedback

If you would like to become an active contributor to this project please follow the instructions provided in Azure Projects Contribution Guidelines. You can find more details for contributing in the CONTRIBUTING.md.

If you encounter any bugs with the library please file an issue in the Issues section of the project.