Skip to content

Commit

Permalink
add quickstart (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
brecht-vermeersch authored Nov 14, 2024
1 parent 85c2994 commit a82f656
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,6 +22,90 @@
composer require azure-oss/storage
```

## Quickstart

Create the BlobServiceClient
```php
$blobServiceClient = BlobServiceClient::fromConnectionString("<connection string>");
```

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).
Expand Down

0 comments on commit a82f656

Please sign in to comment.