Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,40 @@ $adapter = new AzureBlobStorageAdapter(
);
```

### Upload transfer tuning

When writing files, you can control the upload behavior via Flysystem's Config on write/writeStream calls:

- initialTransferSize: int (bytes) — size threshold for first transfer; smaller blobs are uploaded in a single request; larger ones switch to chunked upload.
- maximumTransferSize: int (bytes) — chunk size for block uploads.
- maximumConcurrency: int — number of concurrent workers for parallel uploads.

Example:

```php
use League\Flysystem\Config;

$filesystem->write('path/to/file.txt', $contents, new Config([
'initialTransferSize' => 64 * 1024 * 1024, // 64MB
'maximumTransferSize' => 8 * 1024 * 1024, // 8MB
'maximumConcurrency' => 8,
]));
```

### HTTP headers

```php
$filesystem->write('path/to/file.txt', $contents, new Config([
'httpHeaders' => [
'cacheControl' => 'public, max-age=31536000',
'contentDisposition' => 'inline',
'contentEncoding' => 'gzip',
'contentLanguage' => 'en',
'contentType' => 'text/plain',
],
]));
```

Note that for direct public URLs to work, your container must be configured with public access. If your container is private, you should use the default SAS token approach.

## Documentation
Expand Down
80 changes: 71 additions & 9 deletions src/AzureBlobStorageAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use League\Flysystem\UrlGeneration\TemporaryUrlGenerator;
use League\MimeTypeDetection\FinfoMimeTypeDetector;
use League\MimeTypeDetection\MimeTypeDetector;
use AzureOss\FlysystemAzureBlobStorage\Support\ConfigArrayParser;

final class AzureBlobStorageAdapter implements FilesystemAdapter, ChecksumProvider, TemporaryUrlGenerator, PublicUrlGenerator
{
Expand Down Expand Up @@ -85,35 +86,96 @@ public function directoryExists(string $path): bool

public function write(string $path, string $contents, Config $config): void
{
$this->upload($path, $contents);
$this->upload($path, $contents, $config);
}

public function writeStream(string $path, $contents, Config $config): void
{
$this->upload($path, $contents);
$this->upload($path, $contents, $config);
}

/**
* @param string|resource $contents
*/
private function upload(string $path, $contents): void
private function upload(string $path, $contents, ?Config $config = null): void
{
try {
$path = $this->prefixer->prefixPath($path);
$mimetype = $this->mimeTypeDetector->detectMimetype($path, $contents);
$options = $this->buildUploadOptionsFromConfig($config);

$options = new UploadBlobOptions(
contentType: $mimetype,
);
if ($options->httpHeaders->contentType === "" && is_string($contents)) {
$options->httpHeaders->contentType = $this->mimeTypeDetector->detectMimeTypeFromBuffer($contents) ?? "";
}

$this->containerClient
->getBlobClient($path)
->getBlobClient($this->prefixer->prefixPath($path))
->upload($contents, $options);
} catch (\Throwable $e) {
throw UnableToWriteFile::atLocation($path, previous: $e);
}
}

private function buildUploadOptionsFromConfig(?Config $config): UploadBlobOptions
{
$options = new UploadBlobOptions();

if ($config === null) {
return $options;
}

$data = $config->toArray();

$initialTransferSize = ConfigArrayParser::parseIntFromArray($data, 'initialTransferSize');
if ($initialTransferSize !== null) {
$options->initialTransferSize = $initialTransferSize;
}

$maximumTransferSize = ConfigArrayParser::parseIntFromArray($data, 'maximumTransferSize');
if ($maximumTransferSize !== null) {
$options->maximumTransferSize = $maximumTransferSize;
}

$maximumConcurrency = ConfigArrayParser::parseIntFromArray($data, 'maximumConcurrency');
if ($maximumConcurrency !== null) {
$options->maximumConcurrency = $maximumConcurrency;
}

$headers = ConfigArrayParser::parseArrayFromArray($data, 'httpHeaders');
if ($headers !== null) {
$cacheControl = ConfigArrayParser::parseStringFromArray($headers, 'cacheControl', 'httpHeaders.');
if ($cacheControl !== null) {
$options->httpHeaders->cacheControl = $cacheControl;
}

$contentDisposition = ConfigArrayParser::parseStringFromArray($headers, 'contentDisposition', 'httpHeaders.');
if ($contentDisposition !== null) {
$options->httpHeaders->contentDisposition = $contentDisposition;
}

$contentEncoding = ConfigArrayParser::parseStringFromArray($headers, 'contentEncoding', 'httpHeaders.');
if ($contentEncoding !== null) {
$options->httpHeaders->contentEncoding = $contentEncoding;
}

$contentHash = ConfigArrayParser::parseStringFromArray($headers, 'contentHash', 'httpHeaders.');
if ($contentHash !== null) {
$options->httpHeaders->contentHash = $contentHash;
}

$contentLanguage = ConfigArrayParser::parseStringFromArray($headers, 'contentLanguage', 'httpHeaders.');
if ($contentLanguage !== null) {
$options->httpHeaders->contentLanguage = $contentLanguage;
}

$contentType = ConfigArrayParser::parseStringFromArray($headers, 'contentType', 'httpHeaders.');
if ($contentType !== null) {
$options->httpHeaders->contentType = $contentType;
}
}

return $options;
}


public function read(string $path): string
{
try {
Expand Down
54 changes: 54 additions & 0 deletions src/Support/ConfigArrayParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace AzureOss\FlysystemAzureBlobStorage\Support;

final class ConfigArrayParser
{
/**
* @param array<string, mixed> $data
*/
public static function parseIntFromArray(array $data, string $key): ?int
{
if (!array_key_exists($key, $data) || $data[$key] === null) {
return null;
}
if (!is_int($data[$key])) {
throw new \RuntimeException(sprintf('%s must be an int.', $key));
}
return $data[$key];
}

/**
* @param array<string, mixed> $data
* @return array<string, mixed>|null
*/
public static function parseArrayFromArray(array $data, string $key): ?array
{
$value = $data[$key] ?? null;
if ($value === null) {
return null;
}
if (!is_array($value)) {
throw new \RuntimeException(sprintf('%s must be an array.', $key));
}
return $value;
}

/**
* @param array<string, mixed> $data
*/
public static function parseStringFromArray(array $data, string $key, string $contextPrefix = ''): ?string
{
$value = $data[$key] ?? null;
if ($value === null) {
return null;
}
if (!is_string($value)) {
$fullKey = $contextPrefix !== '' ? $contextPrefix . $key : $key;
throw new \RuntimeException(sprintf('%s must be a string.', $fullKey));
}
return $value;
}
}