Skip to content

Commit 2da1975

Browse files
authored
Merge pull request #16 from Azure-OSS/add-support-for-temporary-upload-urls
add support for temporary upload urls
2 parents c2dba67 + 1de6824 commit 2da1975

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

src/AzureStorageBlobAdapter.php

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,48 @@ public function url($path)
3636
return $this->adapter->publicUrl($path, new Config);
3737
}
3838

39-
/** @phpstan-ignore-next-line */
39+
/**
40+
* Get a temporary URL for the file at the given path.
41+
*
42+
* @param string $path
43+
* @param \DateTimeInterface $expiration
44+
* @return string
45+
*/
46+
/** @phpstan-ignore-next-line */
4047
public function temporaryUrl($path, $expiration, array $options = [])
4148
{
4249
return $this->adapter->temporaryUrl(
4350
$path,
4451
$expiration,
45-
new Config(['permissions' => 'r'])
52+
new Config(array_merge(['permissions' => 'r'], $options))
4653
);
4754
}
55+
56+
/**
57+
* Get a temporary upload URL for the file at the given path.
58+
*
59+
* @param string $path
60+
* @param \DateTimeInterface $expiration
61+
* @return array{url: string, headers: array<string, string>}
62+
*/
63+
/** @phpstan-ignore-next-line */
64+
public function temporaryUploadUrl($path, $expiration, array $options = [])
65+
{
66+
$url = $this->adapter->temporaryUrl(
67+
$path,
68+
$expiration,
69+
new Config(array_merge(['permissions' => 'cw'], $options))
70+
);
71+
$contentType = isset($options['content-type']) && is_string($options['content-type'])
72+
? $options['content-type']
73+
: 'application/octet-stream';
74+
75+
return [
76+
'url' => $url,
77+
'headers' => [
78+
'x-ms-blob-type' => 'BlockBlob',
79+
'Content-Type' => $contentType,
80+
],
81+
];
82+
}
4883
}

tests/AzureStorageBlobAdapterTest.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,27 @@ public function driver_works(): void
8888
self::assertFalse($driver->exists('file2.txt'));
8989
self::assertTrue($driver->exists('file3.txt'));
9090

91-
self::assertCount(2, $driver->allFiles());
91+
// Test temporary upload URL functionality
92+
// Generate a temporary upload URL
93+
$uploadData = $driver->temporaryUploadUrl('temp-upload-test.txt', now()->addMinutes(5), [
94+
'content-type' => 'text/plain',
95+
]);
96+
97+
// Upload content directly to the URL
98+
$content = 'This content was uploaded directly to a temporary URL';
99+
$response = Http::withHeaders($uploadData['headers'])
100+
->withBody($content, 'text/plain')
101+
->put($uploadData['url']);
102+
103+
// Verify the upload was successful
104+
self::assertTrue($response->successful());
105+
106+
// Verify the file exists and has the correct content
107+
self::assertTrue($driver->exists('temp-upload-test.txt'));
108+
self::assertEquals($content, $driver->get('temp-upload-test.txt'));
109+
110+
// Count files and clean up
111+
self::assertCount(3, $driver->allFiles()); // file.txt, file3.txt, and temp-upload-test.txt
92112

93113
$driver->deleteDirectory('');
94114

0 commit comments

Comments
 (0)