Last updated: May 20, 2025

<span aria-hidden="true" id="import-files-from-backblaze-in-php-with-open-source-sdks"></span>

# Import files from Backblaze in PHP with open-source SDKs

![Kevin van Zonneveld](/assets/images/teammates/avatar-kvz-4.jpg?dpl=dpl_B2d4XACVUtA1h8m6kRxZhWsda77Q)

#### Kevin van Zonneveld

Co-founder · Amsterdam, The Netherlands · Show bio

[](https://x.com/kvz)[](https://github.com/kvz)

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.

<span aria-hidden="true" id="requirements"></span>

## Requirements

* PHP 7.4 + with the `ext-curl` extension
* Composer
* A Backblaze B2 account with an App Key (key ID + application key)

<span aria-hidden="true" id="choose-an-sdk"></span>

## Choose an SDK

Two community SDKs receive regular maintenance and cover most B2 features:

* [gliterd/backblaze-b2⁠](https://github.com/gliterd/backblaze-b2) – lightweight, B2 API v1
* [obregonco/backblaze-b2⁠](https://github.com/obregonco/backblaze-b2) – fuller feature set, B2 API v2

Install the one you prefer:

```bash
composer require gliterd/backblaze-b2:^1.5  # v1 API, simple
# Or
composer require obregonco/backblaze-b2:^2.0 # v2 API, advanced

```

***

<span aria-hidden="true" id="import-a-file-with-gliterdbackblaze-b2"></span>

## Import a file with `gliterd/backblaze-b2`

```php
<?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());
}

```

<span aria-hidden="true" id="import-a-file-with-obregoncobackblaze-b2"></span>

## Import a file with `obregonco/backblaze-b2`

```php
<?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());
}

```

<span aria-hidden="true" id="common-tasks"></span>

## Common tasks

<span aria-hidden="true" id="list-objects-in-a-bucket"></span>

### List objects in a bucket

`gliterd/backblaze-b2`:

```php
<?php
$files = $client->listFiles([
    'BucketId'     => 'your-bucket-id',
    'MaxFileCount' => 100,
]);

```

`obregonco/backblaze-b2`:

```php
<?php
$files = $client->listFiles([
    'BucketName'   => 'your-bucket-name',
    'MaxFileCount' => 100,
]);

```

<span aria-hidden="true" id="check-whether-a-file-exists"></span>

### Check whether a file exists

`obregonco/backblaze-b2` makes this especially simple:

```php
<?php
$exists = $client->fileExists([
    'BucketName' => 'my-bucket',
    'FileName'   => 'photos/sunset.jpg',
]);

```

<span aria-hidden="true" id="which-sdk-should-you-pick"></span>

## 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|

<span aria-hidden="true" id="secure-and-efficient-file-handling"></span>

## 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.

<span aria-hidden="true" id="conclusion"></span>

## 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.

\#php#backblaze-b2#cloud-storage#open-source#file-importing-service

### 👩‍💻 Join 20k+ developers

Sign up for our [monthly newsletter](/newsletters.md) to receive direct links to 3 exclusive tech — and 2 product updates. No less, no more.

Your email:

Get access

## File uploading and encoding. Made simple.

Transloadit streamlines file handling for developers, trusted by brands like Coursera and The New York Times. We’re known for a reliable API, top-notch support, and a strong commitment to open source, with projects like [Uppy⁠](https://uppy.io) and [Tus⁠](https://tus.io) setting standards in file processing.

[Sign up](/c/)[Book a Demo](https://survey.typeform.com/to/kRg47Xi5)

No credit card needed · 5 GB included in the free plan

Cancel anytime
