Skip to content

Commit

Permalink
Merge pull request #14 from gliterd/develop
Browse files Browse the repository at this point in the history
Notice added
  • Loading branch information
mhetreramesh authored Aug 24, 2018
2 parents ddb51cb + 4cd1b89 commit e53df57
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 139 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ use BackblazeB2\Bucket;

$client = new Client('accountId', 'applicationKey');
```
## *ApplicationKey is not supported yet, please use MasterKey only*

#### Returns a bucket details
``` php
$bucket = $client->createBucket([
Expand Down Expand Up @@ -138,4 +140,4 @@ The MIT License (MIT). Please see [License File](LICENSE.md) for more informatio
[link-code-quality]: https://scrutinizer-ci.com/g/gliterd/backblaze-b2
[link-downloads]: https://packagist.org/packages/gliterd/backblaze-b2
[link-author]: https://github.com/gliterd
[link-contributors]: ../../contributors
[link-contributors]: ../../contributors
2 changes: 1 addition & 1 deletion phpunit.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?php

require __DIR__.'/vendor/autoload.php';
require __DIR__.'/vendor/autoload.php';
119 changes: 63 additions & 56 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ public function __construct($accountId, $applicationKey, array $options = [])
* Create a bucket with the given name and type.
*
* @param array $options
* @return Bucket
*
* @throws ValidationException
*
* @return Bucket
*/
public function createBucket(array $options)
{
Expand All @@ -53,15 +55,15 @@ public function createBucket(array $options)
);
}

$response = $this->client->request('POST', $this->apiUrl . '/b2_create_bucket', [
$response = $this->client->request('POST', $this->apiUrl.'/b2_create_bucket', [
'headers' => [
'Authorization' => $this->authToken,
],
'json' => [
'accountId' => $this->accountId,
'accountId' => $this->accountId,
'bucketName' => $options['BucketName'],
'bucketType' => $options['BucketType']
]
'bucketType' => $options['BucketType'],
],
]);

return new Bucket($response['bucketId'], $response['bucketName'], $response['bucketType']);
Expand All @@ -71,8 +73,10 @@ public function createBucket(array $options)
* Updates the type attribute of a bucket by the given ID.
*
* @param array $options
* @return Bucket
*
* @throws ValidationException
*
* @return Bucket
*/
public function updateBucket(array $options)
{
Expand All @@ -86,15 +90,15 @@ public function updateBucket(array $options)
$options['BucketId'] = $this->getBucketIdFromName($options['BucketName']);
}

$response = $this->client->request('POST', $this->apiUrl . '/b2_update_bucket', [
$response = $this->client->request('POST', $this->apiUrl.'/b2_update_bucket', [
'headers' => [
'Authorization' => $this->authToken,
],
'json' => [
'accountId' => $this->accountId,
'bucketId' => $options['BucketId'],
'bucketType' => $options['BucketType']
]
'accountId' => $this->accountId,
'bucketId' => $options['BucketId'],
'bucketType' => $options['BucketType'],
],
]);

return new Bucket($response['bucketId'], $response['bucketName'], $response['bucketType']);
Expand All @@ -109,13 +113,13 @@ public function listBuckets()
{
$buckets = [];

$response = $this->client->request('POST', $this->apiUrl . '/b2_list_buckets', [
$response = $this->client->request('POST', $this->apiUrl.'/b2_list_buckets', [
'headers' => [
'Authorization' => $this->authToken,
],
'json' => [
'accountId' => $this->accountId
]
'accountId' => $this->accountId,
],
]);

foreach ($response['buckets'] as $bucket) {
Expand All @@ -129,6 +133,7 @@ public function listBuckets()
* Deletes the bucket identified by its ID.
*
* @param array $options
*
* @return bool
*/
public function deleteBucket(array $options)
Expand All @@ -137,14 +142,14 @@ public function deleteBucket(array $options)
$options['BucketId'] = $this->getBucketIdFromName($options['BucketName']);
}

$this->client->request('POST', $this->apiUrl . '/b2_delete_bucket', [
$this->client->request('POST', $this->apiUrl.'/b2_delete_bucket', [
'headers' => [
'Authorization' => $this->authToken
'Authorization' => $this->authToken,
],
'json' => [
'accountId' => $this->accountId,
'bucketId' => $options['BucketId']
]
'bucketId' => $options['BucketId'],
],
]);

return true;
Expand All @@ -154,6 +159,7 @@ public function deleteBucket(array $options)
* Uploads a file to a bucket and returns a File object.
*
* @param array $options
*
* @return File
*/
public function upload(array $options)
Expand All @@ -168,13 +174,13 @@ public function upload(array $options)
}

// Retrieve the URL that we should be uploading to.
$response = $this->client->request('POST', $this->apiUrl . '/b2_get_upload_url', [
$response = $this->client->request('POST', $this->apiUrl.'/b2_get_upload_url', [
'headers' => [
'Authorization' => $this->authToken
'Authorization' => $this->authToken,
],
'json' => [
'bucketId' => $options['BucketId']
]
'bucketId' => $options['BucketId'],
],
]);

$uploadEndpoint = $response['uploadUrl'];
Expand Down Expand Up @@ -207,14 +213,14 @@ public function upload(array $options)

$response = $this->client->request('POST', $uploadEndpoint, [
'headers' => [
'Authorization' => $uploadAuthToken,
'Content-Type' => $options['FileContentType'],
'Content-Length' => $size,
'X-Bz-File-Name' => $options['FileName'],
'X-Bz-Content-Sha1' => $hash,
'X-Bz-Info-src_last_modified_millis' => $options['FileLastModified']
'Authorization' => $uploadAuthToken,
'Content-Type' => $options['FileContentType'],
'Content-Length' => $size,
'X-Bz-File-Name' => $options['FileName'],
'X-Bz-Content-Sha1' => $hash,
'X-Bz-Info-src_last_modified_millis' => $options['FileLastModified'],
],
'body' => $options['Body']
'body' => $options['Body'],
]);

return new File(
Expand All @@ -231,21 +237,22 @@ public function upload(array $options)
* Download a file from a B2 bucket.
*
* @param array $options
*
* @return bool|mixed|string
*/
public function download(array $options)
{
$requestUrl = null;
$requestOptions = [
'headers' => [
'Authorization' => $this->authToken
'Authorization' => $this->authToken,
],
'sink' => isset($options['SaveAs']) ? $options['SaveAs'] : null
'sink' => isset($options['SaveAs']) ? $options['SaveAs'] : null,
];

if (isset($options['FileId'])) {
$requestOptions['query'] = ['fileId' => $options['FileId']];
$requestUrl = $this->downloadUrl . '/b2api/v1/b2_download_file_by_id';
$requestUrl = $this->downloadUrl.'/b2api/v1/b2_download_file_by_id';
} else {
if (!isset($options['BucketName']) && isset($options['BucketId'])) {
$options['BucketName'] = $this->getBucketNameFromId($options['BucketId']);
Expand All @@ -263,6 +270,7 @@ public function download(array $options)
* Retrieve a collection of File objects representing the files stored inside a bucket.
*
* @param array $options
*
* @return array
*/
public function listFiles(array $options)
Expand All @@ -285,15 +293,15 @@ public function listFiles(array $options)

// B2 returns, at most, 1000 files per "page". Loop through the pages and compile an array of File objects.
while (true) {
$response = $this->client->request('POST', $this->apiUrl . '/b2_list_file_names', [
$response = $this->client->request('POST', $this->apiUrl.'/b2_list_file_names', [
'headers' => [
'Authorization' => $this->authToken
'Authorization' => $this->authToken,
],
'json' => [
'bucketId' => $options['BucketId'],
'bucketId' => $options['BucketId'],
'startFileName' => $nextFileName,
'maxFileCount' => $maxFileCount,
]
'maxFileCount' => $maxFileCount,
],
]);

foreach ($response['files'] as $file) {
Expand All @@ -318,7 +326,8 @@ public function listFiles(array $options)
* Test whether a file exists in B2 for the given bucket.
*
* @param array $options
* @return boolean
*
* @return bool
*/
public function fileExists(array $options)
{
Expand All @@ -327,12 +336,13 @@ public function fileExists(array $options)
return !empty($files);
}


/**
* Returns a single File object representing a file stored on B2.
*
* @param array $options
*
* @throws NotFoundException If no file id was provided and BucketName + FileName does not resolve to a file, a NotFoundException is thrown.
*
* @return File
*/
public function getFile(array $options)
Expand All @@ -345,13 +355,13 @@ public function getFile(array $options)
}
}

$response = $this->client->request('POST', $this->apiUrl . '/b2_get_file_info', [
$response = $this->client->request('POST', $this->apiUrl.'/b2_get_file_info', [
'headers' => [
'Authorization' => $this->authToken
'Authorization' => $this->authToken,
],
'json' => [
'fileId' => $options['FileId']
]
'fileId' => $options['FileId'],
],
]);

return new File(
Expand All @@ -371,6 +381,7 @@ public function getFile(array $options)
* Deletes the file identified by ID from Backblaze B2.
*
* @param array $options
*
* @return bool
*/
public function deleteFile(array $options)
Expand All @@ -387,14 +398,14 @@ public function deleteFile(array $options)
$options['FileId'] = $file->getId();
}

$this->client->request('POST', $this->apiUrl . '/b2_delete_file_version', [
$this->client->request('POST', $this->apiUrl.'/b2_delete_file_version', [
'headers' => [
'Authorization' => $this->authToken
'Authorization' => $this->authToken,
],
'json' => [
'fileName' => $options['FileName'],
'fileId' => $options['FileId']
]
'fileId' => $options['FileId'],
],
]);

return true;
Expand All @@ -408,18 +419,19 @@ public function deleteFile(array $options)
protected function authorizeAccount()
{
$response = $this->client->request('GET', 'https://api.backblazeb2.com/b2api/v1/b2_authorize_account', [
'auth' => [$this->accountId, $this->applicationKey]
'auth' => [$this->accountId, $this->applicationKey],
]);

$this->authToken = $response['authorizationToken'];
$this->apiUrl = $response['apiUrl'] . '/b2api/v1';
$this->apiUrl = $response['apiUrl'].'/b2api/v1';
$this->downloadUrl = $response['downloadUrl'];
}

/**
* Maps the provided bucket name to the appropriate bucket ID.
*
* @param $name
*
* @return null
*/
protected function getBucketIdFromName($name)
Expand All @@ -431,14 +443,13 @@ protected function getBucketIdFromName($name)
return $bucket->getId();
}
}

return null;
}

/**
* Maps the provided bucket ID to the appropriate bucket name.
*
* @param $id
*
* @return null
*/
protected function getBucketNameFromId($id)
Expand All @@ -450,23 +461,19 @@ protected function getBucketNameFromId($id)
return $bucket->getName();
}
}

return null;
}

protected function getFileIdFromBucketAndFileName($bucketName, $fileName)
{
$files = $this->listFiles([
'BucketName' => $bucketName,
'FileName' => $fileName,
'FileName' => $fileName,
]);

foreach ($files as $file) {
if ($file->getName() === $fileName) {
return $file->getId();
}
}

return null;
}
}
14 changes: 7 additions & 7 deletions src/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
class ErrorHandler
{
protected static $mappings = [
'bad_json' => BadJsonException::class,
'bad_value' => BadValueException::class,
'duplicate_bucket_name' => BucketAlreadyExistsException::class,
'not_found' => NotFoundException::class,
'file_not_present' => FileNotPresentException::class,
'cannot_delete_non_empty_bucket' => BucketNotEmptyException::class
'bad_json' => BadJsonException::class,
'bad_value' => BadValueException::class,
'duplicate_bucket_name' => BucketAlreadyExistsException::class,
'not_found' => NotFoundException::class,
'file_not_present' => FileNotPresentException::class,
'cannot_delete_non_empty_bucket' => BucketNotEmptyException::class,
];

public static function handleErrorResponse(Response $response)
Expand All @@ -33,6 +33,6 @@ public static function handleErrorResponse(Response $response)
$exceptionClass = B2Exception::class;
}

throw new $exceptionClass('Received error from B2: ' . $responseJson['message']);
throw new $exceptionClass('Received error from B2: '.$responseJson['message']);
}
}
Loading

0 comments on commit e53df57

Please sign in to comment.