Skip to content

Commit

Permalink
Add integration with B2 cloud storage
Browse files Browse the repository at this point in the history
  • Loading branch information
InnerFlameFact authored and Sergey Moldachev committed Oct 13, 2018
1 parent ed1adf0 commit 6683fda
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -86,6 +86,12 @@ It's stable enough, you'll need to understand permissions.
'bucket' => '',
'root' => '',
],
'b2' => [
'type' => 'B2',
'key' => '',
'accountId' => '',
'bucket' => '',
],
'gcs' => [
'type' => 'Gcs',
'key' => '',
Expand Down Expand Up @@ -179,6 +185,9 @@ Then, you'll need to select the appropriate packages for the adapters that you w
# to support s3
composer require league/flysystem-aws-s3-v3

# to support b2
composer require mhetreramesh/flysystem-backblaze

# to support google cs
composer require league/flysystem-aws-s3-v2

Expand Down
2 changes: 2 additions & 0 deletions composer.json
Expand Up @@ -26,6 +26,7 @@
"srmklive/flysystem-dropbox-v2": "~1.0",
"league/flysystem-rackspace": "~1.0",
"league/flysystem-sftp": "~1.0",
"mhetreramesh/flysystem-backblaze" : "~1.0",
"dropbox/dropbox-sdk": "~1.1",
"aws/aws-sdk-php": "~3.0",
"phpspec/phpspec": "~2.1"
Expand All @@ -35,6 +36,7 @@
},
"suggest": {
"league/flysystem-aws-s3-v3": "AwsS3 adapter support.",
"mhetreramesh/flysystem-backblaze": "B2 adapter support.",
"league/flysystem-aws-s3-v2": "GoogleCS adapter support.",
"srmklive/flysystem-dropbox-v2": "Dropbox API v2 adapter support.",
"league/flysystem-rackspace": "Rackspace adapter support.",
Expand Down
56 changes: 56 additions & 0 deletions spec/Filesystems/BackblazeFilesystemSpec.php
@@ -0,0 +1,56 @@
<?php

namespace spec\BackupManager\Filesystems;

use BackblazeB2\Http\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use PhpSpec\ObjectBehavior;

class BackblazeFilesystemSpec extends ObjectBehavior {

function it_is_initializable() {
$this->shouldHaveType('BackupManager\Filesystems\BackblazeFilesystem');
}

function it_should_recognize_its_type_with_case_insensitivity() {
foreach (['b2', 'B2'] as $type) {
$this->handles($type)->shouldBe(true);
}

foreach ([null, 'foo'] as $type) {
$this->handles($type)->shouldBe(false);
}
}

function it_should_provide_an_instance_of_an_b2_filesystem() {
$this->get($this->getConfig())->getAdapter()->shouldHaveType('Mhetreramesh\Flysystem\BackblazeAdapter');
}

function getConfig() {
return [
'key' => 'test_key',
'accountId' => 'test_id',
'bucket' => 'bucket',
'options' => ['client' => $this->getMockClient()]
];
}

function getMockClient() {
$handler = new HandlerStack(new MockHandler([$this->getMockAuthorizationResponse()]));

return new Client(['handler' => $handler]);
}

function getMockAuthorizationResponse() {
$body = '{
"accountId: "test_id",
"apiUrl": "https://api900.backblaze.com",
"authorizationToken": "testAuthToken,
"downloadUrl": "https://f900.backblaze.com"
}';

return new Response(200, [], $body);
}
}
34 changes: 34 additions & 0 deletions src/Filesystems/BackblazeFilesystem.php
@@ -0,0 +1,34 @@
<?php namespace BackupManager\Filesystems;

use BackblazeB2\Client;
use League\Flysystem\Filesystem as Flysystem;
use Mhetreramesh\Flysystem\BackblazeAdapter;

/**
* Class BackblazeFilesystem
* @package BackupManager\Filesystems
*/
class BackblazeFilesystem implements Filesystem {

/**
* Test fitness of visitor.
* @param $type
* @return bool
*/
public function handles($type) {
return strtolower($type) == 'b2';
}

/**
* @param array $config
* @return Flysystem
*/
public function get(array $config) {
if (!isset($config['options'])) {
$config['options'] = [];
}

$client = new Client($config['accountId'], $config['key'], $config['options']);
return new Flysystem(new BackblazeAdapter($client, $config['bucket']));
}
}

0 comments on commit 6683fda

Please sign in to comment.