This repository has been archived by the owner on May 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathb2_api.php
409 lines (333 loc) · 14.4 KB
/
b2_api.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
<?php
/**
* Backblaze B2 API wrapper for PHP
*
* @author Aidhan Dossel
* @copyright aidhan.net
* @version dev-master
*
*/
// Base function for further calls
function b2_call($call_url, $headers, $data = NULL)
{
$session = curl_init($call_url);
if(!empty($data)) // Check if POST data exists
{
if(is_array($data)) // Check if the data is an array
{
$data = json_encode($data); // Encode the data as JSON
}
curl_setopt($session, CURLOPT_POST, true); // Make the request a POST
curl_setopt($session, CURLOPT_POSTFIELDS, $data); // Add the data to the request
}
else
{
curl_setopt($session, CURLOPT_HTTPGET, true); // Make the request a GET
}
curl_setopt($session, CURLOPT_HTTPHEADER, $headers); // Include the headers
curl_setopt($session, CURLOPT_RETURNTRANSFER, true); // Receive server response
$http_result = curl_exec($session); // Execute the request
curl_close($session); // Clean up
json_decode($http_result);
if(json_last_error() == JSON_ERROR_NONE || json_last_error() == 0) // Check if the response is JSON
{
return json_decode($http_result); // Return the result, as an array.
}
else
{
return $http_result; // Return the result as it was recieved
}
}
class b2_api
{
// Account authorization
public function b2_authorize_account($account_id, $app_key)
{
$call_url = "https://api.backblaze.com/b2api/v1/b2_authorize_account";
$account_id = $account_id;
$app_key = $app_key;
$credentials = base64_encode($account_id.":".$app_key);
// Add headers
$headers = array(
"Accept: application/json",
"Authorization: Basic {$credentials}"
);
$result = b2_call($call_url, $headers);
return $result; // Return the result
}
// Cancel large file
// Part of the large files API, not functional at time of writing
public function b2_cancel_large_file()
{
}
// Create bucket
public function b2_create_bucket($api_url, $account_id, $auth_token, $bucket_name, $bucket_type)
{
$call_url = $api_url."/b2api/v1/b2_create_bucket";
$account_id = $account_id; // Obtained from your B2 account page
$auth_token = $auth_token; // Obtained from the b2_authorize_account call
$bucket_name = $bucket_name; // The new bucket's name. 6 char min, 50 char max, letters, digits, - and _ are allowed
$bucket_type = $bucket_type; // Type to change to, either allPublic or allPrivate
// Add POST fields
$data = array(
"accountId" => $account_id,
"bucketName" => $bucket_name,
"bucketType" => $bucket_type
);
// Add headers
$headers = array(
"Authorization: {$auth_token}"
);
$result = b2_call($call_url, $headers, $data);
return $result; // Return the result
}
// Delete bucket
public function b2_delete_bucket($api_url, $account_id, $auth_token, $bucket_id)
{
$call_url = $api_url."/b2api/v1/b2_delete_bucket";
$account_id = $account_id; // Obtained from your B2 account page
$auth_token = $auth_token; // Obtained from the b2_authorize_account call
$bucket_id = $bucket_id; // The ID of the bucket you want to delete
// Add POST fields
$data = array(
"accountId" => $account_id,
"bucketId" => $bucket_id
);
// Add headers
$headers = array(
"Authorization: {$auth_token}"
);
$result = b2_call($call_url, $headers, $data);
return $result; // Return the result
}
// Delete file version
public function b2_delete_file_version($api_url, $auth_token, $file_id, $file_name)
{
$call_url = $api_url."/b2api/v1/b2_delete_file_version";
$auth_token = $auth_token; // Obtained from the b2_authorize_account call
$file_id = $file_id; // The ID of the file you want to delete
$file_name = $file_name; // The file name of the file you want to delete
// Add POST fields
$data = array(
"fileId" => $file_id,
"fileName" => $file_name
);
// Add headers
$headers = array(
"Authorization: {$auth_token}"
);
$result = b2_call($call_url, $headers, $data);
return $result; // Return the result
}
// Download file by ID
public function b2_download_file_by_id($download_url, $file_id, $auth_token = NULL)
{
$call_url = $download_url."/b2api/v1/b2_download_file_by_id?fileId=".$file_id;
$auth_token = $auth_token; // Obtained from the b2_authorize_account call
$download_url = $download_url; // Obtained from the b2_authorize_account call
$file_id = $file_id; // The ID of the file you wish to download
// Add headers
$headers = array(
"Authorization: {$auth_token}"
);
$result = b2_call($call_url, $headers);
return $result; // Return the result
}
// Download file by name
public function b2_download_file_by_name($download_url, $bucket_name, $file_name, $auth_token = NULL)
{
$call_url = $download_url."/file/".$bucket_name."/".$file_name;
$auth_token = $auth_token; // Obtained from the b2_authorize_account call
$download_url = $download_url; // Obtained from the b2_authorize_account call
$bucket_name = $bucket_name; // The name of the bucket you wish to download from
$file_name = $file_name; // The name of the file you wish to download
// Add headers
$headers = array(
"Authorization: {$auth_token}"
);
$result = b2_call($call_url, $headers);
return $result; // Return the result
}
// Finish large file
// Part of the large files API, not functional at time of writing
public function b2_finish_large_file()
{
}
// Get file info
public function b2_get_file_info($api_url, $auth_token, $file_id)
{
$call_url = $api_url."/b2api/v1/b2_get_file_info";
$auth_token = $auth_token; // Obtained from the b2_authorize_account call
$file_id = $file_id; // The ID of the file you wish to recieve the info of
// Add POST fields
$data = array(
"fileId" => $file_id
);
// Add headers
$headers = array(
"Authorization: {$auth_token}"
);
$result = b2_call($call_url, $headers, $data);
return $result; // Return the result
}
// Get upload URL
public function b2_get_upload_url($api_url, $account_id, $auth_token, $bucket_id)
{
$call_url = $api_url."/b2api/v1/b2_get_upload_url";
$account_id = $account_id; // Obtained from your B2 account page
$auth_token = $auth_token; // Obtained from the b2_authorize_account call
$bucket_id = $bucket_id; // The ID of the bucket you want to upload to
// Add POST fields
$data = array(
"bucketId" => $bucket_id
);
// Add headers
$headers = array(
"Authorization: {$auth_token}"
);
$result = b2_call($call_url, $headers, $data);
return $result; // Return the result
}
// Hide file
public function b2_hide_file($api_url, $auth_token, $bucket_id, $file_name)
{
$call_url = $api_url."/b2api/v1/b2_hide_file";
$auth_token = $auth_token; // Obtained from the b2_authorize_account call
$bucket_id = $bucket_id; // The ID of the bucket containing the file you wish to hide
$file_name = $file_name; // The name of the file you wish to hide
// Add POST fields
$data = array(
"bucketId" => $bucket_id,
"fileName" => $file_name
);
// Add headers
$headers = array(
"Authorization: {$auth_token}"
);
$result = b2_call($call_url, $headers, $data);
return $result; // Return the result
}
// List buckets
public function b2_list_buckets($api_url, $account_id, $auth_token)
{
$call_url = $api_url."/b2api/v1/b2_list_buckets";
$auth_token = $auth_token; // Obtained from the b2_authorize_account call
$account_id = $account_id; // Obtained from your B2 account page
// Add POST fields
$data = array(
"accountId" => $account_id
);
// Add headers
$headers = array(
"Authorization: {$auth_token}"
);
$result = b2_call($call_url, $headers, $data);
return $result; // Return the result
}
// List file names
public function b2_list_file_names($api_url, $auth_token, $bucket_id, $options = NULL)
{
$call_url = $api_url."/b2api/v1/b2_list_file_names";
$auth_token = $auth_token; // Obtained from the b2_authorize_account call
$bucket_id = $bucket_id; // The ID of the bucket containing the files you wish to list
$max_count = $options["max_count"]; // The maxiumum amount of file names to list in a call
$start_name = $options["start_name"]; // If the specified file name exists, it's the first listed
// Add POST fields
$data = array(
"bucketId" => $bucket_id,
"startFileName" => $start_name,
"maxFileCount" => $max_count
);
// Add headers
$headers = array(
"Authorization: {$auth_token}"
);
$result = b2_call($call_url, $headers, $data);
return $result; // Return the result
}
// List file versions
public function b2_list_file_versions($api_url, $auth_token, $bucket_id, $options = NULL)
{
$call_url = $api_url."/b2api/v1/b2_list_file_versions";
$auth_token = $auth_token; // Obtained from the b2_authorize_account call
$bucket_id = $bucket_id; // The ID of the bucket containing the files you wish to list
$max_count = $options["max_count"]; // The maxiumum amount of file names to list in a call
$start_id = $options["start_id"]; // If the specified file ID exists, it's the first listed
$start_name = $options["start_name"]; // If the specified file name exists, it's the first listed
// Add POST fields
$data = array(
"bucketId" => $bucket_id,
"startFileId" => $start_id,
"startFileName" => $start_name,
"maxFileCount" => $max_count
);
// Add headers
$headers = array(
"Authorization: {$auth_token}"
);
$result = b2_call($call_url, $headers, $data);
return $result; // Return the result
}
// List parts
// Part of the large files API, not functional at time of writing
public function b2_list_parts()
{
}
// List unfinished large files
// Part of the large files API, not functional at time of writing
public function b2_list_unfinished_large_files()
{
}
// Start large file
// Part of the large files API, not functional at time of writing
public function b2_start_large_file()
{
}
// List update bucket
public function b2_update_bucket($api_url, $account_id, $auth_token, $bucket_id, $bucket_type)
{
$call_url = $api_url."/b2api/v1/b2_update_bucket";
$account_id = $account_id; // Obtained from your B2 account page
$auth_token = $auth_token; // Obtained from the b2_authorize_account call
$bucket_id = $bucket_id; // The ID of the bucket you want to update
$bucket_type = $bucket_type; // Type to change to, either allPublic or allPrivate
// Add POST fields
$data = array(
"accountId" => $account_id,
"bucketId" => $bucket_id,
"bucketType" => $bucket_type
);
// Add headers
$headers = array(
"Authorization: {$auth_token}"
);
$result = b2_call($call_url, $headers, $data);
return $result; // Return the result
}
// List upload file
public function b2_upload_file($upload_url, $auth_token, $file_path)
{
$call_url = $upload_url; // Upload URL, obtained from the b2_get_upload_url call
$auth_token = $auth_token; // Obtained from the b2_authorize_account call
$file_path = $file_path; // The path to the file you wish to upload
$handle = fopen($file_path, 'r');
$read_file = fread($handle, filesize($file_path));
$file_name = basename($file_path);
$file_type = mime_content_type($file_path);
$file_hash = sha1_file($file_path);
// Add headers
$headers = array(
"Authorization: {$auth_token}",
"X-Bz-File-Name: {$file_name}",
"Content-Type: {$file_type}",
"X-Bz-Content-Sha1: {$file_hash}"
);
$result = b2_call($call_url, $headers, $read_file);
return $result; // Return the result
}
// Upload part
// Part of the large files API, not functional at time of writing
public function b2_upload_part()
{
}
}