From 6a2925a34f4e30ea7040fba87f4c9b7d57e54e48 Mon Sep 17 00:00:00 2001 From: "brecht.vermeersch" Date: Fri, 8 Aug 2025 15:23:14 +0200 Subject: [PATCH 1/3] add support for temporary upload urls --- src/AzureStorageBlobAdapter.php | 39 +++++++++++++++++++++++++-- tests/AzureStorageBlobAdapterTest.php | 22 ++++++++++++++- 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/src/AzureStorageBlobAdapter.php b/src/AzureStorageBlobAdapter.php index 40f7d0f..7ec8a12 100644 --- a/src/AzureStorageBlobAdapter.php +++ b/src/AzureStorageBlobAdapter.php @@ -36,13 +36,48 @@ public function url($path) return $this->adapter->publicUrl($path, new Config); } - /** @phpstan-ignore-next-line */ + /** + * Get a temporary URL for the file at the given path. + * + * @param string $path + * @param \DateTimeInterface $expiration + * @param array $options + * @return string + */ public function temporaryUrl($path, $expiration, array $options = []) { return $this->adapter->temporaryUrl( $path, $expiration, - new Config(['permissions' => 'r']) + new Config(array_merge(['permissions' => 'r'], $options)) ); } + + /** + * Get a temporary upload URL for the file at the given path. + * + * @param string $path + * @param \DateTimeInterface $expiration + * @param array $options + * @return array{url: string, headers: array} + */ + public function temporaryUploadUrl($path, $expiration, array $options = []) + { + $url = $this->adapter->temporaryUrl( + $path, + $expiration, + new Config(array_merge(['permissions' => 'cw'], $options)) + ); + $contentType = isset($options['content-type']) && is_string($options['content-type']) + ? $options['content-type'] + : 'application/octet-stream'; + + return [ + 'url' => $url, + 'headers' => [ + 'x-ms-blob-type' => 'BlockBlob', + 'Content-Type' => $contentType, + ], + ]; + } } diff --git a/tests/AzureStorageBlobAdapterTest.php b/tests/AzureStorageBlobAdapterTest.php index 3f672f6..e7829b6 100644 --- a/tests/AzureStorageBlobAdapterTest.php +++ b/tests/AzureStorageBlobAdapterTest.php @@ -88,7 +88,27 @@ public function driver_works(): void self::assertFalse($driver->exists('file2.txt')); self::assertTrue($driver->exists('file3.txt')); - self::assertCount(2, $driver->allFiles()); + // Test temporary upload URL functionality + // Generate a temporary upload URL + $uploadData = $driver->temporaryUploadUrl('temp-upload-test.txt', now()->addMinutes(5), [ + 'content-type' => 'text/plain', + ]); + + // Upload content directly to the URL + $content = 'This content was uploaded directly to a temporary URL'; + $response = Http::withHeaders($uploadData['headers']) + ->withBody($content, 'text/plain') + ->put($uploadData['url']); + + // Verify the upload was successful + self::assertTrue($response->successful()); + + // Verify the file exists and has the correct content + self::assertTrue($driver->exists('temp-upload-test.txt')); + self::assertEquals($content, $driver->get('temp-upload-test.txt')); + + // Count files and clean up + self::assertCount(3, $driver->allFiles()); // file.txt, file3.txt, and temp-upload-test.txt $driver->deleteDirectory(''); From 67772cce1d731cfbf7de7a7acae0e2adb1a8c122 Mon Sep 17 00:00:00 2001 From: brecht-vermeersch <20967950+brecht-vermeersch@users.noreply.github.com> Date: Fri, 8 Aug 2025 13:26:15 +0000 Subject: [PATCH 2/3] Fixes coding style --- src/AzureStorageBlobAdapter.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/AzureStorageBlobAdapter.php b/src/AzureStorageBlobAdapter.php index 7ec8a12..836c6a8 100644 --- a/src/AzureStorageBlobAdapter.php +++ b/src/AzureStorageBlobAdapter.php @@ -41,7 +41,6 @@ public function url($path) * * @param string $path * @param \DateTimeInterface $expiration - * @param array $options * @return string */ public function temporaryUrl($path, $expiration, array $options = []) @@ -58,7 +57,6 @@ public function temporaryUrl($path, $expiration, array $options = []) * * @param string $path * @param \DateTimeInterface $expiration - * @param array $options * @return array{url: string, headers: array} */ public function temporaryUploadUrl($path, $expiration, array $options = []) From 1de682430524e4ce084ff11f7a2bc3534cd853c3 Mon Sep 17 00:00:00 2001 From: "brecht.vermeersch" Date: Fri, 8 Aug 2025 15:30:13 +0200 Subject: [PATCH 3/3] add support for temporary upload urls --- src/AzureStorageBlobAdapter.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/AzureStorageBlobAdapter.php b/src/AzureStorageBlobAdapter.php index 836c6a8..4fd30e3 100644 --- a/src/AzureStorageBlobAdapter.php +++ b/src/AzureStorageBlobAdapter.php @@ -43,6 +43,7 @@ public function url($path) * @param \DateTimeInterface $expiration * @return string */ + /** @phpstan-ignore-next-line */ public function temporaryUrl($path, $expiration, array $options = []) { return $this->adapter->temporaryUrl( @@ -59,6 +60,7 @@ public function temporaryUrl($path, $expiration, array $options = []) * @param \DateTimeInterface $expiration * @return array{url: string, headers: array} */ + /** @phpstan-ignore-next-line */ public function temporaryUploadUrl($path, $expiration, array $options = []) { $url = $this->adapter->temporaryUrl(