From a82f656986d0cd14ac4802353cde4f93d7cb7ac3 Mon Sep 17 00:00:00 2001 From: Brecht Date: Thu, 14 Nov 2024 14:24:03 +0100 Subject: [PATCH] add quickstart (#46) --- README.md | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/README.md b/README.md index 22b578d..91f5b6a 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,11 @@ [![Packagist Downloads](https://img.shields.io/packagist/dm/azure-oss/storage)](https://packagist.org/packages/azure-oss/storage) [![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/azure-oss/azure-storage-php/tests.yml?branch=main)](https://github.com/azure-oss/azure-storage-php/actions) +> [!TIP] +> If you’re working with Laravel or Flysystem, check out our dedicated drivers. +> * [Azure Storage Laravel Adapter](https://github.com/Azure-OSS/azure-storage-php-adapter-laravel) +> * [Azure Storage Flysystem Adapter](https://github.com/Azure-OSS/azure-storage-php-adapter-flysystem) + ## Minimum Requirements * PHP 8.1 or above @@ -17,6 +22,90 @@ composer require azure-oss/storage ``` +## Quickstart + +Create the BlobServiceClient +```php +$blobServiceClient = BlobServiceClient::fromConnectionString(""); +``` + +Create a container +```php +$containerClient = $blobServiceClient->getContainerClient('quickstart'); +$containerClient->create(); +``` + +List containers in a storage account +```php +$containers = $blobServiceClient->getBlobContainers(); +``` + +Upload a blob to a container +```php +$blobClient = $containerClient->getBlobClient("hello.txt"); +$blobClient->upload("world!"); + +// or using streams +$file = fopen('hugefile.txt', 'r'); +$blobClient->upload($file); +``` + +List blobs in a container +```php +$blobs = $containerClient->getBlobs(); + +// or with a prefix +$blobs = $containerClient->getBlobs('some/virtual/directory'); + +// and if you want both the blobs and virtual directories +$blobs = $containerClient->getBlobsByHierarchy('some/virtual/directory'); +``` + +Download a blob +```php +$result = $blobClient->downloadStreaming(); + +$props = $result->properties; +$content = $result->content->getContents(); +``` + +Copy a blob +```php +$sourceBlobClient = $containerClient->getBlobClient("source.txt"); + +// Can also be a different container +$targetBlobClient = $containerClient->getBlobClient("target.txt"); +$targetBlobClient->copyFromUri($sourceBlobClient->uri); +``` + +Generate and use a [Service SAS](https://learn.microsoft.com/en-us/azure/storage/common/storage-sas-overview#service-sas) +```php +$sas = $blobClient->generateSasUri( + BlobSasBuilder::new() + ->setPermissions(new BlobSasPermissions(read: true)) + ->setExpiresOn((new \DateTime())->modify("+ 15min")), +); + +$sasBlobClient = new BlobClient($sas); +``` + +Generate and use an [Account SAS](https://learn.microsoft.com/en-us/azure/storage/common/storage-sas-overview#account-sas) +```php +$sas = $blobServiceClient->generateAccountSasUri( + AccountSasBuilder::new() + ->setPermissions(new AccountSasPermissions(list: true)) + ->setResourceTypes(new AccountSasResourceTypes(service: true)) + ->setExpiresOn((new \DateTime())->modify("+ 15min")), +); + +$sasServiceClient = new BlobServiceClient($sas); +``` + +Delete a container +```php +$containerClient->delete(); +``` + ## Documentation For more information visit the documentation at [azure-oss.github.io](https://azure-oss.github.io).