Import files from Backblaze in PHP with open-source SDKs

Backblaze B2 is a popular cloud-storage service valued for its affordability, straightforward REST
API, and dependable performance. In this DevTip you’ll learn how to import objects from B2 into a
PHP app with two open-source SDKs: gliterd/backblaze-b2
and obregonco/backblaze-b2
. We’ll cover
installation, code samples, and practical tips for production use.
Requirements
- PHP 7.4 + with the
ext-curl
extension - Composer
- A Backblaze B2 account with an App Key (key ID + application key)
Choose an SDK
Two community SDKs receive regular maintenance and cover most B2 features:
gliterd/backblaze-b2
– lightweight, B2 API v1obregonco/backblaze-b2
– fuller feature set, B2 API v2
Install the one you prefer:
composer require gliterd/backblaze-b2:^1.5 # v1 API, simple
# Or
composer require obregonco/backblaze-b2:^2.0 # v2 API, advanced
Import a file with gliterd/backblaze-b2
<?php
require 'vendor/autoload.php';
use BackblazeB2\Client;
try {
$client = new Client(
getenv('B2_ACCOUNT_ID'),
getenv('B2_APPLICATION_KEY')
);
$client->download([
'BucketName' => 'my-bucket',
'FileName' => 'photos/sunset.jpg',
'SaveAs' => __DIR__.'/sunset.jpg', // remove key to keep in memory
]);
echo 'Downloaded!';
} catch (\Throwable $e) {
error_log($e->getMessage());
}
Import a file with obregonco/backblaze-b2
<?php
require 'vendor/autoload.php';
use obregonco\B2\Client;
try {
$client = new Client(getenv('B2_ACCOUNT_ID'), [
'keyId' => getenv('B2_KEY_ID'),
'applicationKey' => getenv('B2_APPLICATION_KEY'),
]);
$client->version = 2; // use B2 API v2
$file = $client->download([
'BucketName' => 'my-bucket',
'FileName' => 'photos/sunset.jpg',
]);
file_put_contents(__DIR__.'/sunset.jpg', $file);
} catch (\Throwable $e) {
error_log($e->getMessage());
}
Common tasks
List objects in a bucket
gliterd/backblaze-b2
:
$files = $client->listFiles([
'BucketId' => 'your-bucket-id',
'MaxFileCount' => 100,
]);
obregonco/backblaze-b2
:
$files = $client->listFiles([
'BucketName' => 'your-bucket-name',
'MaxFileCount' => 100,
]);
Check whether a file exists
obregonco/backblaze-b2
makes this especially simple:
$exists = $client->fileExists([
'BucketName' => 'my-bucket',
'FileName' => 'photos/sunset.jpg',
]);
Which SDK should you pick?
Need | Pick this SDK |
---|---|
Minimal footprint, v1 API | gliterd/backblaze-b2 |
Advanced bucket management | obregonco/backblaze-b2 |
Latest B2 features (v2) | obregonco/backblaze-b2 |
Small learning curve | gliterd/backblaze-b2 |
Secure and efficient file handling
- Store credentials in environment variables and load them via
getenv()
or a secrets manager. - Use application keys with the least privileges necessary.
- Stream large downloads directly to disk (
SaveAs
) to avoid memory spikes. - Implement exponential back-off on
429 Too Many Requests
responses. - Cache
listFiles
results if you query the same paths frequently.
Conclusion
Integrating Backblaze B2 into PHP is straightforward with these two open-source SDKs—pick the one
that best matches your project’s complexity and start importing files today. If you later need to
import entire directories or chain processing steps, Transloadit’s /backblaze/import
Robot can take that work off your hands.