Skip to content

Commit b99adf8

Browse files
committed
Allow using an application key not just the master key
1 parent 42549be commit b99adf8

File tree

4 files changed

+36
-17
lines changed

4 files changed

+36
-17
lines changed

README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,14 @@ $adapter = new BackblazeAdapter($client,$bucketName);
3232

3333
$filesystem = new Filesystem($adapter);
3434
```
35-
## *ApplicationKey is not supported yet, please use MasterKey only*
35+
## Using ApplicationKey instead of MasterKey
36+
If you specify only the $bucketName when creating the BackblazeAdapter, your application key must be the master key.
37+
However, if you specify both bucket name and bucket id, you do not need the master key and can use a single-bucket key.
38+
Fetch your bucket id using the [b2 command line tool](https://www.backblaze.com/b2/docs/quick_command_line.html) `b2 get-bucket <bucketName>`
39+
``` php
40+
$client = new Client($accountId, $applicationKey);
41+
$adapter = new BackblazeAdapter($client, $bucketName, $bucketId);
42+
```
3643

3744

3845
## Doccumentation

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"require": {
1717
"php": "^7.1.8",
1818
"league/flysystem": "~1.0",
19-
"gliterd/backblaze-b2": "*",
19+
"gliterd/backblaze-b2": ">=1.5.0",
2020
"psr/http-message-implementation": "*",
2121
"mikey179/vfsStream": "*"
2222
},

src/BackblazeAdapter.php

+19-7
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,21 @@ class BackblazeAdapter extends AbstractAdapter
1616

1717
protected $bucketName;
1818

19-
public function __construct(Client $client, $bucketName)
19+
protected $bucketId;
20+
21+
public function __construct(Client $client, $bucketName, $bucketId = null)
2022
{
2123
$this->client = $client;
2224
$this->bucketName = $bucketName;
25+
$this->bucketId = $bucketId;
2326
}
2427

2528
/**
2629
* {@inheritdoc}
2730
*/
2831
public function has($path)
2932
{
30-
return $this->getClient()->fileExists(['FileName' => $path, 'BucketName' => $this->bucketName]);
33+
return $this->getClient()->fileExists(['FileName' => $path, 'BucketId' => $this->bucketId, 'BucketName' => $this->bucketName]);
3134
}
3235

3336
/**
@@ -36,6 +39,7 @@ public function has($path)
3639
public function write($path, $contents, Config $config)
3740
{
3841
$file = $this->getClient()->upload([
42+
'BucketId' => $this->bucketId,
3943
'BucketName' => $this->bucketName,
4044
'FileName' => $path,
4145
'Body' => $contents,
@@ -50,6 +54,7 @@ public function write($path, $contents, Config $config)
5054
public function writeStream($path, $resource, Config $config)
5155
{
5256
$file = $this->getClient()->upload([
57+
'BucketId' => $this->bucketId,
5358
'BucketName' => $this->bucketName,
5459
'FileName' => $path,
5560
'Body' => $resource,
@@ -64,6 +69,7 @@ public function writeStream($path, $resource, Config $config)
6469
public function update($path, $contents, Config $config)
6570
{
6671
$file = $this->getClient()->upload([
72+
'BucketId' => $this->bucketId,
6773
'BucketName' => $this->bucketName,
6874
'FileName' => $path,
6975
'Body' => $contents,
@@ -78,6 +84,7 @@ public function update($path, $contents, Config $config)
7884
public function updateStream($path, $resource, Config $config)
7985
{
8086
$file = $this->getClient()->upload([
87+
'BucketId' => $this->bucketId,
8188
'BucketName' => $this->bucketName,
8289
'FileName' => $path,
8390
'Body' => $resource,
@@ -92,6 +99,7 @@ public function updateStream($path, $resource, Config $config)
9299
public function read($path)
93100
{
94101
$file = $this->getClient()->getFile([
102+
'BucketId' => $this->bucketId,
95103
'BucketName' => $this->bucketName,
96104
'FileName' => $path,
97105
]);
@@ -109,6 +117,7 @@ public function readStream($path)
109117
{
110118
$stream = Psr7\stream_for();
111119
$download = $this->getClient()->download([
120+
'BucketId' => $this->bucketId,
112121
'BucketName' => $this->bucketName,
113122
'FileName' => $path,
114123
'SaveAs' => $stream,
@@ -138,6 +147,7 @@ public function rename($path, $newpath)
138147
public function copy($path, $newPath)
139148
{
140149
return $this->getClient()->upload([
150+
'BucketId' => $this->bucketId,
141151
'BucketName' => $this->bucketName,
142152
'FileName' => $newPath,
143153
'Body' => @file_get_contents($path),
@@ -149,15 +159,15 @@ public function copy($path, $newPath)
149159
*/
150160
public function delete($path)
151161
{
152-
return $this->getClient()->deleteFile(['FileName' => $path, 'BucketName' => $this->bucketName]);
162+
return $this->getClient()->deleteFile(['FileName' => $path, 'BucketId' => $this->bucketId, 'BucketName' => $this->bucketName]);
153163
}
154164

155165
/**
156166
* {@inheritdoc}
157167
*/
158168
public function deleteDir($path)
159169
{
160-
return $this->getClient()->deleteFile(['FileName' => $path, 'BucketName' => $this->bucketName]);
170+
return $this->getClient()->deleteFile(['FileName' => $path, 'BucketId' => $this->bucketId, 'BucketName' => $this->bucketName]);
161171
}
162172

163173
/**
@@ -166,6 +176,7 @@ public function deleteDir($path)
166176
public function createDir($path, Config $config)
167177
{
168178
return $this->getClient()->upload([
179+
'BucketId' => $this->bucketId,
169180
'BucketName' => $this->bucketName,
170181
'FileName' => $path,
171182
'Body' => '',
@@ -193,7 +204,7 @@ public function getMimetype($path)
193204
*/
194205
public function getSize($path)
195206
{
196-
$file = $this->getClient()->getFile(['FileName' => $path, 'BucketName' => $this->bucketName]);
207+
$file = $this->getClient()->getFile(['FileName' => $path, 'BucketId' => $this->bucketId, 'BucketName' => $this->bucketName]);
197208

198209
return $this->getFileInfo($file);
199210
}
@@ -203,7 +214,7 @@ public function getSize($path)
203214
*/
204215
public function getTimestamp($path)
205216
{
206-
$file = $this->getClient()->getFile(['FileName' => $path, 'BucketName' => $this->bucketName]);
217+
$file = $this->getClient()->getFile(['FileName' => $path, 'BucketId' => $this->bucketId, 'BucketName' => $this->bucketName]);
207218

208219
return $this->getFileInfo($file);
209220
}
@@ -222,6 +233,7 @@ public function getClient()
222233
public function listContents($directory = '', $recursive = false)
223234
{
224235
$fileObjects = $this->getClient()->listFiles([
236+
'BucketId' => $this->bucketId,
225237
'BucketName' => $this->bucketName,
226238
]);
227239
if ($recursive === true && $directory === '') {
@@ -235,7 +247,7 @@ public function listContents($directory = '', $recursive = false)
235247
} else {
236248
throw new \InvalidArgumentException();
237249
}
238-
$fileObjects = array_filter($fileObjects, function ($fileObject) use ($directory, $regex) {
250+
$fileObjects = array_filter($fileObjects, function ($fileObject) use ($regex) {
239251
return 1 === preg_match($regex, $fileObject->getName());
240252
});
241253
$normalized = array_map(function ($fileObject) {

tests/BackblazeAdapterTests.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function backblazeProvider()
3737
*/
3838
public function testHas($adapter, $mock)
3939
{
40-
$mock->fileExists(['BucketName' => 'my_bucket', 'FileName' => 'something'])->willReturn(true);
40+
$mock->fileExists(['BucketId' => null, 'BucketName' => 'my_bucket', 'FileName' => 'something'])->willReturn(true);
4141
$result = $adapter->has('something');
4242
$this->assertTrue($result);
4343
}
@@ -47,7 +47,7 @@ public function testHas($adapter, $mock)
4747
*/
4848
public function testWrite($adapter, $mock)
4949
{
50-
$mock->upload(['BucketName' => 'my_bucket', 'FileName' => 'something', 'Body' => 'contents'])->willReturn(new File('something', '', '', '', ''), false);
50+
$mock->upload(['BucketId' => null, 'BucketName' => 'my_bucket', 'FileName' => 'something', 'Body' => 'contents'])->willReturn(new File('something', '', '', '', ''), false);
5151
$result = $adapter->write('something', 'contents', new Config());
5252
$this->assertInternalType('array', $result);
5353
$this->assertArrayHasKey('type', $result);
@@ -59,7 +59,7 @@ public function testWrite($adapter, $mock)
5959
*/
6060
public function testWriteStream($adapter, $mock)
6161
{
62-
$mock->upload(['BucketName' => 'my_bucket', 'FileName' => 'something', 'Body' => 'contents'])->willReturn(new File('something', '', '', '', ''), false);
62+
$mock->upload(['BucketId' => null, 'BucketName' => 'my_bucket', 'FileName' => 'something', 'Body' => 'contents'])->willReturn(new File('something', '', '', '', ''), false);
6363
$result = $adapter->writeStream('something', 'contents', new Config());
6464
$this->assertInternalType('array', $result);
6565
$this->assertArrayHasKey('type', $result);
@@ -71,7 +71,7 @@ public function testWriteStream($adapter, $mock)
7171
*/
7272
public function testUpdate($adapter, $mock)
7373
{
74-
$mock->upload(['BucketName' => 'my_bucket', 'FileName' => 'something', 'Body' => 'contents'])->willReturn(new File('something', '', '', '', ''), false);
74+
$mock->upload(['BucketId' => null, 'BucketName' => 'my_bucket', 'FileName' => 'something', 'Body' => 'contents'])->willReturn(new File('something', '', '', '', ''), false);
7575
$result = $adapter->update('something', 'contents', new Config());
7676
$this->assertInternalType('array', $result);
7777
$this->assertArrayHasKey('type', $result);
@@ -83,7 +83,7 @@ public function testUpdate($adapter, $mock)
8383
*/
8484
public function testUpdateStream($adapter, $mock)
8585
{
86-
$mock->upload(['BucketName' => 'my_bucket', 'FileName' => 'something', 'Body' => 'contents'])->willReturn(new File('something', '', '', '', ''), false);
86+
$mock->upload(['BucketId' => null, 'BucketName' => 'my_bucket', 'FileName' => 'something', 'Body' => 'contents'])->willReturn(new File('something', '', '', '', ''), false);
8787
$result = $adapter->updateStream('something', 'contents', new Config());
8888
$this->assertInternalType('array', $result);
8989
$this->assertArrayHasKey('type', $result);
@@ -96,7 +96,7 @@ public function testUpdateStream($adapter, $mock)
9696
public function testRead($adapter, $mock)
9797
{
9898
$file = new File('something', 'something4', '', '', '', '', 'my_bucket');
99-
$mock->getFile(['BucketName' => 'my_bucket', 'FileName' => 'something'])->willReturn($file, false);
99+
$mock->getFile(['BucketId' => null, 'BucketName' => 'my_bucket', 'FileName' => 'something'])->willReturn($file, false);
100100
$mock->download(['FileId' => 'something'])->willReturn($file, false);
101101
$result = $adapter->read('something');
102102
$this->assertEquals(['contents' => $file], $result);
@@ -148,7 +148,7 @@ public function testGetMimetype($adapter, $mock)
148148
public function testCopy($adapter, $mock)
149149
{
150150
$this->fileSetUp();
151-
$mock->upload(['BucketName' => 'my_bucket', 'FileName' => 'something_new', 'Body' => ''])->willReturn(new File('something_new', '', '', '', ''), false);
151+
$mock->upload(['BucketId' => null, 'BucketName' => 'my_bucket', 'FileName' => 'something_new', 'Body' => ''])->willReturn(new File('something_new', '', '', '', ''), false);
152152
$result = $adapter->copy($this->file_mock->url(), 'something_new');
153153
$this->assertObjectHasAttribute('id', $result, 'something_new');
154154
}
@@ -158,7 +158,7 @@ public function testCopy($adapter, $mock)
158158
*/
159159
public function testListContents($adapter, $mock)
160160
{
161-
$mock->listFiles(['BucketName' => 'my_bucket'])->willReturn([new File('random_id', 'file1.txt'), new File('random_id', 'some_folder/file2.txt'), new File('random_id', 'some_folder/another_folder/file3.txt')]);
161+
$mock->listFiles(['BucketId' => null, 'BucketName' => 'my_bucket'])->willReturn([new File('random_id', 'file1.txt'), new File('random_id', 'some_folder/file2.txt'), new File('random_id', 'some_folder/another_folder/file3.txt')]);
162162
$normalized_files = [
163163
[
164164
'type' => 'file',

0 commit comments

Comments
 (0)