-
Notifications
You must be signed in to change notification settings - Fork 41
/
Client.php
472 lines (401 loc) · 14.2 KB
/
Client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
<?php
namespace ChrisWhite\B2;
use ChrisWhite\B2\Exceptions\NotFoundException;
use ChrisWhite\B2\Exceptions\ValidationException;
use ChrisWhite\B2\Http\Client as HttpClient;
class Client
{
protected $accountId;
protected $applicationKey;
protected $authToken;
protected $apiUrl;
protected $downloadUrl;
protected $client;
/**
* Client constructor. Accepts the account ID, application key and an optional array of options.
*
* @param $accountId
* @param $applicationKey
* @param array $options
*/
public function __construct($accountId, $applicationKey, array $options = [])
{
$this->accountId = $accountId;
$this->applicationKey = $applicationKey;
if (isset($options['client'])) {
$this->client = $options['client'];
} else {
$this->client = new HttpClient(['exceptions' => false]);
}
$this->authorizeAccount();
}
/**
* Create a bucket with the given name and type.
*
* @param array $options
* @return Bucket
* @throws ValidationException
*/
public function createBucket(array $options)
{
if (!in_array($options['BucketType'], [Bucket::TYPE_PUBLIC, Bucket::TYPE_PRIVATE])) {
throw new ValidationException(
sprintf('Bucket type must be %s or %s', Bucket::TYPE_PRIVATE, Bucket::TYPE_PUBLIC)
);
}
$response = $this->client->request('POST', $this->apiUrl.'/b2_create_bucket', [
'headers' => [
'Authorization' => $this->authToken,
],
'json' => [
'accountId' => $this->accountId,
'bucketName' => $options['BucketName'],
'bucketType' => $options['BucketType']
]
]);
return new Bucket($response['bucketId'], $response['bucketName'], $response['bucketType']);
}
/**
* Updates the type attribute of a bucket by the given ID.
*
* @param array $options
* @return Bucket
* @throws ValidationException
*/
public function updateBucket(array $options)
{
if (!in_array($options['BucketType'], [Bucket::TYPE_PUBLIC, Bucket::TYPE_PRIVATE])) {
throw new ValidationException(
sprintf('Bucket type must be %s or %s', Bucket::TYPE_PRIVATE, Bucket::TYPE_PUBLIC)
);
}
if (!isset($options['BucketId']) && isset($options['BucketName'])) {
$options['BucketId'] = $this->getBucketIdFromName($options['BucketName']);
}
$response = $this->client->request('POST', $this->apiUrl.'/b2_update_bucket', [
'headers' => [
'Authorization' => $this->authToken,
],
'json' => [
'accountId' => $this->accountId,
'bucketId' => $options['BucketId'],
'bucketType' => $options['BucketType']
]
]);
return new Bucket($response['bucketId'], $response['bucketName'], $response['bucketType']);
}
/**
* Returns a list of bucket objects representing the buckets on the account.
*
* @return array
*/
public function listBuckets()
{
$buckets = [];
$response = $this->client->request('POST', $this->apiUrl.'/b2_list_buckets', [
'headers' => [
'Authorization' => $this->authToken,
],
'json' => [
'accountId' => $this->accountId
]
]);
foreach ($response['buckets'] as $bucket) {
$buckets[] = new Bucket($bucket['bucketId'], $bucket['bucketName'], $bucket['bucketType']);
}
return $buckets;
}
/**
* Deletes the bucket identified by its ID.
*
* @param array $options
* @return bool
*/
public function deleteBucket(array $options)
{
if (!isset($options['BucketId']) && isset($options['BucketName'])) {
$options['BucketId'] = $this->getBucketIdFromName($options['BucketName']);
}
$this->client->request('POST', $this->apiUrl.'/b2_delete_bucket', [
'headers' => [
'Authorization' => $this->authToken
],
'json' => [
'accountId' => $this->accountId,
'bucketId' => $options['BucketId']
]
]);
return true;
}
/**
* Uploads a file to a bucket and returns a File object.
*
* @param array $options
* @return File
*/
public function upload(array $options)
{
// Clean the path if it starts with /.
if (substr($options['FileName'], 0, 1) === '/') {
$options['FileName'] = ltrim($options['FileName'], '/');
}
if (!isset($options['BucketId']) && isset($options['BucketName'])) {
$options['BucketId'] = $this->getBucketIdFromName($options['BucketName']);
}
// Retrieve the URL that we should be uploading to.
$response = $this->client->request('POST', $this->apiUrl.'/b2_get_upload_url', [
'headers' => [
'Authorization' => $this->authToken
],
'json' => [
'bucketId' => $options['BucketId']
]
]);
$uploadEndpoint = $response['uploadUrl'];
$uploadAuthToken = $response['authorizationToken'];
if (is_resource($options['Body'])) {
// We need to calculate the file's hash incrementally from the stream.
$context = hash_init('sha1');
hash_update_stream($context, $options['Body']);
$hash = hash_final($context);
// Similarly, we have to use fstat to get the size of the stream.
$size = fstat($options['Body'])['size'];
// Rewind the stream before passing it to the HTTP client.
rewind($options['Body']);
} else {
// We've been given a simple string body, it's super simple to calculate the hash and size.
$hash = sha1($options['Body']);
$size = mb_strlen($options['Body']);
}
if (!isset($options['FileLastModified'])) {
$options['FileLastModified'] = round(microtime(true) * 1000);
}
if (!isset($options['FileContentType'])) {
$options['FileContentType'] = 'b2/x-auto';
}
$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']
],
'body' => $options['Body']
]);
return new File(
$response['fileId'],
$response['fileName'],
$response['contentSha1'],
$response['contentLength'],
$response['contentType'],
$response['fileInfo']
);
}
/**
* 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
],
'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';
} else {
if (!isset($options['BucketName']) && isset($options['BucketId'])) {
$options['BucketName'] = $this->getBucketNameFromId($options['BucketId']);
}
$requestUrl = sprintf('%s/file/%s/%s', $this->downloadUrl, $options['BucketName'], $options['FileName']);
}
$response = $this->client->request('GET', $requestUrl, $requestOptions, false);
return isset($options['SaveAs']) ? true : $response;
}
/**
* Retrieve a collection of File objects representing the files stored inside a bucket.
*
* @param array $options
* @return array
*/
public function listFiles(array $options)
{
// if FileName is set, we only attempt to retrieve information about that single file.
$fileName = !empty($options['FileName']) ? $options['FileName'] : null;
$nextFileName = null;
$maxFileCount = 1000;
$files = [];
if (!isset($options['BucketId']) && isset($options['BucketName'])) {
$options['BucketId'] = $this->getBucketIdFromName($options['BucketName']);
}
if ($fileName) {
$nextFileName = $fileName;
$maxFileCount = 1;
}
// 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', [
'headers' => [
'Authorization' => $this->authToken
],
'json' => [
'bucketId' => $options['BucketId'],
'startFileName' => $nextFileName,
'maxFileCount' => $maxFileCount,
]
]);
foreach ($response['files'] as $file) {
// if we have a file name set, only retrieve information if the file name matches
if (!$fileName || ($fileName === $file['fileName'])) {
$files[] = new File($file['fileId'], $file['fileName'], null, $file['size']);
}
}
if ($fileName || $response['nextFileName'] === null) {
// We've got all the files - break out of loop.
break;
}
$nextFileName = $response['nextFileName'];
}
return $files;
}
/**
* Test whether a file exists in B2 for the given bucket.
*
* @param array $options
* @return boolean
*/
public function fileExists(array $options)
{
$files = $this->listFiles($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)
{
if (!isset($options['FileId']) && isset($options['BucketName']) && isset($options['FileName'])) {
$options['FileId'] = $this->getFileIdFromBucketAndFileName($options['BucketName'], $options['FileName']);
if (!$options['FileId']) {
throw new NotFoundException();
}
}
$response = $this->client->request('POST', $this->apiUrl.'/b2_get_file_info', [
'headers' => [
'Authorization' => $this->authToken
],
'json' => [
'fileId' => $options['FileId']
]
]);
return new File(
$response['fileId'],
$response['fileName'],
$response['contentSha1'],
$response['contentLength'],
$response['contentType'],
$response['fileInfo'],
$response['bucketId'],
$response['action'],
$response['uploadTimestamp']
);
}
/**
* Deletes the file identified by ID from Backblaze B2.
*
* @param array $options
* @return bool
*/
public function deleteFile(array $options)
{
if (!isset($options['FileName'])) {
$file = $this->getFile($options);
$options['FileName'] = $file->getName();
}
if (!isset($options['FileId']) && isset($options['BucketName']) && isset($options['FileName'])) {
$file = $this->getFile($options);
$options['FileId'] = $file->getId();
}
$this->client->request('POST', $this->apiUrl.'/b2_delete_file_version', [
'headers' => [
'Authorization' => $this->authToken
],
'json' => [
'fileName' => $options['FileName'],
'fileId' => $options['FileId']
]
]);
return true;
}
/**
* Authorize the B2 account in order to get an auth token and API/download URLs.
*
* @throws \Exception
*/
protected function authorizeAccount()
{
$response = $this->client->request('GET', 'https://api.backblazeb2.com/b2api/v1/b2_authorize_account', [
'auth' => [$this->accountId, $this->applicationKey]
]);
$this->authToken = $response['authorizationToken'];
$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)
{
$buckets = $this->listBuckets();
foreach ($buckets as $bucket) {
if ($bucket->getName() === $name) {
return $bucket->getId();
}
}
return null;
}
/**
* Maps the provided bucket ID to the appropriate bucket name.
*
* @param $id
* @return null
*/
protected function getBucketNameFromId($id)
{
$buckets = $this->listBuckets();
foreach ($buckets as $bucket) {
if ($bucket->getId() === $id) {
return $bucket->getName();
}
}
return null;
}
protected function getFileIdFromBucketAndFileName($bucketName, $fileName)
{
$files = $this->listFiles([
'BucketName' => $bucketName,
'FileName' => $fileName,
]);
foreach ($files as $file) {
if ($file->getName() === $fileName) {
return $file->getId();
}
}
return null;
}
}