Skip to content

Commit

Permalink
Merge 1713182 into 8aebde2
Browse files Browse the repository at this point in the history
  • Loading branch information
macbookandrew committed Feb 5, 2021
2 parents 8aebde2 + 1713182 commit c1f9eac
Show file tree
Hide file tree
Showing 8 changed files with 303 additions and 19 deletions.
23 changes: 22 additions & 1 deletion src/Bucket.php
Expand Up @@ -10,19 +10,25 @@ class Bucket
protected $id;
protected $name;
protected $type;
protected $options;
protected $corsRules;

/**
* Bucket constructor.
*
* @param $id
* @param $name
* @param $type
* @param $options
* @param $corsRules
*/
public function __construct($id, $name, $type)
public function __construct($id, $name, $type, $options, $corsRules)
{
$this->id = $id;
$this->name = $name;
$this->type = $type;
$this->options = $options;
$this->corsRules = $corsRules;
}

public function getId()
Expand All @@ -39,4 +45,19 @@ public function getType()
{
return $this->type;
}

public function getOptions()
{
return $this->options;
}

public function getCorsRules()
{
return $this->corsRules;
}

public function isS3Compatible()
{
return in_array('s3', $this->getOptions());
}
}
99 changes: 96 additions & 3 deletions src/Client.php
Expand Up @@ -75,7 +75,7 @@ public function createBucket(array $options)
'bucketType' => $options['BucketType'],
]);

return new Bucket($response['bucketId'], $response['bucketName'], $response['bucketType']);
return new Bucket($response['bucketId'], $response['bucketName'], $response['bucketType'], $response['options'], $response['corsRules']);
}

/**
Expand Down Expand Up @@ -107,7 +107,7 @@ public function updateBucket(array $options)
'bucketType' => $options['BucketType'],
]);

return new Bucket($response['bucketId'], $response['bucketName'], $response['bucketType']);
return new Bucket($response['bucketId'], $response['bucketName'], $response['bucketType'], $response['options'], $response['corsRules']);
}

/**
Expand All @@ -127,7 +127,7 @@ public function listBuckets()
]);

foreach ($response['buckets'] as $bucket) {
$buckets[] = new Bucket($bucket['bucketId'], $bucket['bucketName'], $bucket['bucketType']);
$buckets[] = new Bucket($bucket['bucketId'], $bucket['bucketName'], $bucket['bucketType'], $bucket['options'], $bucket['corsRules']);
}

return $buckets;
Expand Down Expand Up @@ -773,6 +773,99 @@ protected function finishLargeFile($fileId, array $sha1s)
);
}

/**
* List key pairs.
*
* @param array $options
*
* @throws ValidationException
* @throws GuzzleException If the request fails.
* @throws B2Exception If the B2 server replies with an error.
*
* @return Key
*/
public function listKeys(array $options = [])
{
$request = [
'accountId' => $this->accountId,
];

if (array_key_exists('MaxKeyCount', $options)) {
$request['maxKeyCount'] = $options['MaxKeyCount'];
}

if (array_key_exists('StartApplicationKeyId', $options)) {
$request['startApplicationKeyId'] = $options['StartApplicationKeyId'];
}

$response = $this->sendAuthorizedRequest('POST', 'b2_list_keys', $request);

$keys = [];
foreach ($response['keys'] as $key) {
$keys[] = new Key($key['applicationKeyId'], $key['keyName'], null, $key['capabilities'], $key['bucketId'], $key['namePrefix'], $key['expirationTimestamp']);
}

return $keys;
}

/**
* Create a key pair for the given bucket and permissions.
*
* @param array $options
*
* @throws ValidationException
* @throws GuzzleException If the request fails.
* @throws B2Exception If the B2 server replies with an error.
*
* @return Key
*/
public function createKey(array $options)
{
$request = [
'accountId' => $this->accountId,
'capabilities' => $options['Capabilities'],
'keyName' => $options['KeyName'],
];

if (array_key_exists('BucketId', $options)) {
$request['bucketId'] = $options['BucketId'];
}

if (array_key_exists('NamePrefix', $options)) {
$request['namePrefix'] = $options['NamePrefix'];
}

if (array_key_exists('ValidDurationInSeconds', $options)) {
$request['validDurationInSeconds'] = $options['ValidDurationInSeconds'];
}

$response = $this->sendAuthorizedRequest('POST', 'b2_create_key', $request);

return new Key($response['applicationKeyId'], $response['keyName'], $response['applicationKey'], $response['capabilities'], $response['bucketId'], $response['namePrefix'], $response['expirationTimestamp']);
}

/**
* Delete a key pair.
*
* @param array $options
*
* @throws ValidationException
* @throws GuzzleException If the request fails.
* @throws B2Exception If the B2 server replies with an error.
*
* @return Key
*/
public function deleteKey(array $options)
{
$request = [
'applicationKeyId' => $options['ApplicationKeyId'],
];

$response = $this->sendAuthorizedRequest('POST', 'b2_delete_key', $request);

return new Key($response['applicationKeyId'], $response['keyName'], null, $response['capabilities'], $response['bucketId'], $response['namePrefix'], $response['expirationTimestamp']);
}

/**
* Sends a authorized request to b2 API.
*
Expand Down
79 changes: 79 additions & 0 deletions src/Key.php
@@ -0,0 +1,79 @@
<?php

namespace BackblazeB2;

class Key
{
const PERMISSION_LIST_KEYS = 'listKeys';
const PERMISSION_WRITE_KEYS = 'writeKeys';
const PERMISSION_DELETE_KEYS = 'deleteKeys';
const PERMISSION_LIST_BUCKETS = 'listBuckets';
const PERMISSION_WRITE_BUCKETS = 'writeBuckets';
const PERMISSION_DELETE_BUCKETS = 'deleteBuckets';
const PERMISSION_LIST_FILES = 'listFiles';
const PERMISSION_READ_FILES = 'readFiles';
const PERMISSION_SHARE_FILES = 'shareFiles';
const PERMISSION_WRITE_FILES = 'writeFiles';
const PERMISSION_DELETE_FILES = 'deleteFiles';

protected $id;
protected $name;
protected $secret;
protected $capabilities;
protected $bucketId;
protected $namePrefix;
protected $expirationTimestamp;

/**
* Key constructor.
*
* @param $id
* @param $name
* @param $secret
*/
public function __construct($id, $name, $secret, $capabilities, $bucketId, $namePrefix, $expirationTimestamp)
{
$this->id = $id;
$this->name = $name;
$this->secret = $secret;
$this->capabilities = $capabilities;
$this->bucketId = $bucketId;
$this->namePrefix = $namePrefix;
$this->expirationTimestamp = $expirationTimestamp;
}

public function getId()
{
return $this->id;
}

public function getName()
{
return $this->name;
}

public function getSecret()
{
return $this->secret;
}

public function getCapabilities()
{
return $this->capabilities;
}

public function getBucketId()
{
return $this->bucketId;
}

public function getNamePrefix()
{
return $this->namePrefix;
}

public function getExpirationTimestamp()
{
return $this->expirationTimestamp;
}
}
17 changes: 15 additions & 2 deletions tests/responses/create_bucket_private.json
Expand Up @@ -2,5 +2,18 @@
"bucketId" : "4a48fe8875c6214145260818",
"accountId" : "010203040506",
"bucketName" : "Test bucket",
"bucketType" : "allPrivate"
}
"bucketType" : "allPrivate",

"bucketInfo": [],
"corsRules": [],
"defaultFileLockConfiguration": {
"mode": null,
"period": null,
"status": "disabled"
},
"lifecycleRules": [],
"options": [
"s3"
],
"revision": 2
}
23 changes: 18 additions & 5 deletions tests/responses/create_bucket_public.json
@@ -1,6 +1,19 @@
{
"bucketId" : "4a48fe8875c6214145260818",
"accountId" : "010203040506",
"bucketName" : "Test bucket",
"bucketType" : "allPublic"
}
"bucketId": "4a48fe8875c6214145260818",
"accountId": "010203040506",
"bucketName": "Test bucket",
"bucketType": "allPublic",

"bucketInfo": [],
"corsRules": [],
"defaultFileLockConfiguration": {
"mode": null,
"period": null,
"status": "disabled"
},
"lifecycleRules": [],
"options": [
"s3"
],
"revision": 2
}
47 changes: 43 additions & 4 deletions tests/responses/list_buckets_3.json
Expand Up @@ -4,19 +4,58 @@
"bucketId": "4a48fe8875c6214145260818",
"accountId": "30f20426f0b1",
"bucketName" : "Kitten Videos",
"bucketType": "allPrivate"
"bucketType": "allPrivate",

"bucketInfo": [],
"corsRules": [],
"defaultFileLockConfiguration": {
"mode": null,
"period": null,
"status": "disabled"
},
"lifecycleRules": [],
"options": [
"s3"
],
"revision": 2
},
{
"bucketId" : "5b232e8875c6214145260818",
"accountId": "30f20426f0b1",
"bucketName": "Puppy Videos",
"bucketType": "allPublic"
"bucketType": "allPublic",

"bucketInfo": [],
"corsRules": [],
"defaultFileLockConfiguration": {
"mode": null,
"period": null,
"status": "disabled"
},
"lifecycleRules": [],
"options": [
"s3"
],
"revision": 2
},
{
"bucketId": "87ba238875c6214145260818",
"accountId": "30f20426f0b1",
"bucketName": "Vacation Pictures",
"bucketType" : "allPrivate"
"bucketType" : "allPrivate",

"bucketInfo": [],
"corsRules": [],
"defaultFileLockConfiguration": {
"mode": null,
"period": null,
"status": "disabled"
},
"lifecycleRules": [],
"options": [
"s3"
],
"revision": 2
}
]
}
}
17 changes: 15 additions & 2 deletions tests/responses/update_bucket_to_private.json
Expand Up @@ -2,5 +2,18 @@
"accountId": "accountId",
"bucketId": "bucketId",
"bucketName": "test-bucket",
"bucketType": "allPrivate"
}
"bucketType": "allPrivate",

"bucketInfo": [],
"corsRules": [],
"defaultFileLockConfiguration": {
"mode": null,
"period": null,
"status": "disabled"
},
"lifecycleRules": [],
"options": [
"s3"
],
"revision": 2
}

0 comments on commit c1f9eac

Please sign in to comment.