Skip to content

Commit b4071a4

Browse files
Merge pull request #17 from Azure-OSS/use-actual-public-urls
use actual public urls
2 parents faec5c7 + 430b114 commit b4071a4

File tree

3 files changed

+74
-6
lines changed

3 files changed

+74
-6
lines changed

README.md

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,42 @@
2121
composer require azure-oss/storage-blob-flysystem
2222
```
2323

24-
## Quickstart
24+
## Usage
2525

2626
```php
2727
use AzureOss\FlysystemAzureBlobStorage\AzureBlobStorageAdapter;
2828
use AzureOss\Storage\Blob\BlobServiceClient;
2929
use League\Flysystem\Filesystem;
3030

31-
$blobServiceClient = BlobServiceClient::fromConnectionString('<connection-string>');
32-
$containerClient = $blobServiceClient->getContainerClient('quickstart');
31+
// Create a BlobContainerClient
32+
$containerClient = BlobServiceClient::fromConnectionString($connectionString)
33+
->getContainerClient('your-container-name');
3334

34-
$adapter = new AzureBlobStorageAdapter($containerClient, "optional/prefix");
35+
// Create the adapter
36+
$adapter = new AzureBlobStorageAdapter(
37+
$containerClient,
38+
'optional-prefix',
39+
useDirectPublicUrl: false, // Set to true to use direct public URLs instead of SAS tokens
40+
);
41+
42+
// Create the filesystem
3543
$filesystem = new Filesystem($adapter);
44+
```
45+
46+
### Public URLs
3647

37-
$filesystem->write('hello', 'world!');
48+
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`:
49+
50+
```php
51+
$adapter = new AzureBlobStorageAdapter(
52+
$containerClient,
53+
'optional-prefix',
54+
useDirectPublicUrl: true,
55+
);
3856
```
3957

58+
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.
59+
4060
## Documentation
4161

4262
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();
@@ -287,10 +288,16 @@ public function copy(string $source, string $destination, Config $config): void
287288
}
288289

289290
/**
290-
* @description Azure doesn't support permanent URLs. Instead, we create one that lasts 1000 years.
291+
* @description If useDirectPublicUrl is true, returns the direct public URL.
292+
* Otherwise, Azure doesn't support permanent URLs, so we create one that lasts 1000 years.
291293
*/
292294
public function publicUrl(string $path, Config $config): string
293295
{
296+
if ($this->useDirectPublicUrl) {
297+
$blobClient = $this->containerClient->getBlobClient($this->prefixer->prefixPath($path));
298+
return (string) $blobClient->uri;
299+
}
300+
294301
return $this->temporaryUrl($path, (new \DateTimeImmutable())->modify("+1000 years"), $config);
295302
}
296303

tests/AzureBlobStorageTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,45 @@ public function listing_contents_deep(): void
214214
$this->assertContains('dir1/dir2/dir3/file3.txt', $paths);
215215
});
216216
}
217+
218+
#[Test]
219+
public function public_url_uses_direct_uri_when_enabled(): void
220+
{
221+
$this->givenWeHaveAnExistingFile('test-file.txt');
222+
223+
$adapter = new AzureBlobStorageAdapter(
224+
self::createContainerClient(),
225+
'flysystem',
226+
useDirectPublicUrl: true,
227+
);
228+
229+
$url = $adapter->publicUrl('test-file.txt', new Config());
230+
231+
// Direct URL should not contain SAS token parameters
232+
$this->assertStringNotContainsString('sig=', $url);
233+
$this->assertStringNotContainsString('se=', $url);
234+
$this->assertStringNotContainsString('sp=', $url);
235+
236+
// But should contain the container and blob name
237+
$this->assertStringContainsString('flysystem', $url);
238+
$this->assertStringContainsString('test-file.txt', $url);
239+
}
240+
241+
#[Test]
242+
public function public_url_uses_sas_token_by_default(): void
243+
{
244+
$this->givenWeHaveAnExistingFile('test-file.txt');
245+
246+
$adapter = new AzureBlobStorageAdapter(
247+
self::createContainerClient(),
248+
'flysystem',
249+
);
250+
251+
$url = $adapter->publicUrl('test-file.txt', new Config());
252+
253+
// URL with SAS token should contain these parameters
254+
$this->assertStringContainsString('sig=', $url);
255+
$this->assertStringContainsString('se=', $url);
256+
$this->assertStringContainsString('sp=', $url);
257+
}
217258
}

0 commit comments

Comments
 (0)