Skip to content

Commit eb234ea

Browse files
author
brecht.vermeersch
committed
use actual public urls
1 parent 4053e23 commit eb234ea

File tree

3 files changed

+85
-1
lines changed

3 files changed

+85
-1
lines changed

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,42 @@
1414
composer require azure-oss/storage-blob-flysystem
1515
```
1616

17+
## Usage
18+
19+
```php
20+
use AzureOss\FlysystemAzureBlobStorage\AzureBlobStorageAdapter;
21+
use AzureOss\Storage\Blob\BlobServiceClient;
22+
use League\Flysystem\Filesystem;
23+
24+
// Create a BlobContainerClient
25+
$containerClient = BlobServiceClient::fromConnectionString($connectionString)
26+
->getContainerClient('your-container-name');
27+
28+
// Create the adapter
29+
$adapter = new AzureBlobStorageAdapter(
30+
$containerClient,
31+
'optional-prefix',
32+
useDirectPublicUrl: false, // Set to true to use direct public URLs instead of SAS tokens
33+
);
34+
35+
// Create the filesystem
36+
$filesystem = new Filesystem($adapter);
37+
```
38+
39+
### Public URLs
40+
41+
By default, the adapter generates public URLs using SAS tokens with a 1000-year expiration. If you prefer to use direct public URLs without SAS tokens, you can set the `useDirectPublicUrl` parameter to `true`:
42+
43+
```php
44+
$adapter = new AzureBlobStorageAdapter(
45+
$containerClient,
46+
'optional-prefix',
47+
useDirectPublicUrl: true,
48+
);
49+
```
50+
51+
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.
52+
1753
## Documentation
1854

1955
For more information visit the documentation at [azure-oss.github.io](https://azure-oss.github.io/storage/flysystem/).

src/AzureBlobStorageAdapter.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public function __construct(
4646
string $prefix = "",
4747
?MimeTypeDetector $mimeTypeDetector = null,
4848
private readonly string $visibilityHandling = self::ON_VISIBILITY_THROW_ERROR,
49+
private readonly bool $useDirectPublicUrl = false,
4950
) {
5051
$this->prefixer = new PathPrefixer($prefix);
5152
$this->mimeTypeDetector = $mimeTypeDetector ?? new FinfoMimeTypeDetector();
@@ -284,10 +285,16 @@ public function copy(string $source, string $destination, Config $config): void
284285
}
285286

286287
/**
287-
* @description Azure doesn't support permanent URLs. Instead, we create one that lasts 1000 years.
288+
* @description If useDirectPublicUrl is true, returns the direct public URL.
289+
* Otherwise, Azure doesn't support permanent URLs, so we create one that lasts 1000 years.
288290
*/
289291
public function publicUrl(string $path, Config $config): string
290292
{
293+
if ($this->useDirectPublicUrl) {
294+
$blobClient = $this->containerClient->getBlobClient($this->prefixer->prefixPath($path));
295+
return (string) $blobClient->uri;
296+
}
297+
291298
return $this->temporaryUrl($path, (new \DateTimeImmutable())->modify("+1000 years"), $config);
292299
}
293300

tests/AzureBlobStorageTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,45 @@ public function setting_visibility_causes_errors(): void
190190

191191
$adapter->setVisibility('some-file.md', 'public');
192192
}
193+
194+
#[Test]
195+
public function public_url_uses_direct_uri_when_enabled(): void
196+
{
197+
$this->givenWeHaveAnExistingFile('test-file.txt');
198+
199+
$adapter = new AzureBlobStorageAdapter(
200+
self::createContainerClient(),
201+
'flysystem',
202+
useDirectPublicUrl: true,
203+
);
204+
205+
$url = $adapter->publicUrl('test-file.txt', new Config());
206+
207+
// Direct URL should not contain SAS token parameters
208+
$this->assertStringNotContainsString('sig=', $url);
209+
$this->assertStringNotContainsString('se=', $url);
210+
$this->assertStringNotContainsString('sp=', $url);
211+
212+
// But should contain the container and blob name
213+
$this->assertStringContainsString('flysystem', $url);
214+
$this->assertStringContainsString('test-file.txt', $url);
215+
}
216+
217+
#[Test]
218+
public function public_url_uses_sas_token_by_default(): void
219+
{
220+
$this->givenWeHaveAnExistingFile('test-file.txt');
221+
222+
$adapter = new AzureBlobStorageAdapter(
223+
self::createContainerClient(),
224+
'flysystem',
225+
);
226+
227+
$url = $adapter->publicUrl('test-file.txt', new Config());
228+
229+
// URL with SAS token should contain these parameters
230+
$this->assertStringContainsString('sig=', $url);
231+
$this->assertStringContainsString('se=', $url);
232+
$this->assertStringContainsString('sp=', $url);
233+
}
193234
}

0 commit comments

Comments
 (0)