Development

Bunny CDN storage API with PHP

Like all great services Bunny CDN has an API. Making use theoretically easier.  The storage API allows you to upload files, delete files and list files/directories. I’m interested in the listing of files in the storage zone. With the API I can check files have already been uploaded, their size, format and even the date/time they got uploaded.

The default Bunny CDN API returns the uploaded (created) time in ISO 8601 time format, which isn’t compatible with PHP date and time functions. Also returned is the file size as raw bytes not quite as handy to having it in formatted MB and GB.

I addressed these issues in this PHP file. Thanks to a format size units function i got off stack overflow I made a simple function to remove the unnecessary data returned from the API and format it so you can see the simple file name and its extension. Converting the ISO 8601 time stamp into a PHP compatible one makes sorting by upload date easy.

bunnycdn_storage.php
= 1073741824)
    {
        $bytes = number_format($bytes / 1073741824, 2) . ' GB';
    }
    elseif ($bytes >= 1048576)
    {
        $bytes = number_format($bytes / 1048576, 2) . ' MB';
    }
    elseif ($bytes >= 1024)
    {
        $bytes = number_format($bytes / 1024, 2) . ' KB';
    }
    elseif ($bytes > 1)
    {
        $bytes = $bytes . ' bytes';
    }
    elseif ($bytes == 1)
    {
        $bytes = $bytes . ' byte';
    }
    else
    {
        $bytes = '0 bytes';
    }
    return $bytes;
}
$array = storage('STORAGE-PASSWORD-HERE','STORAGENAME/');
$items = array('data' => array());
foreach ($array as $value){
    $date = $value['DateCreated'];
    $fixed_date = date('Y-m-d H:i:s', strtotime($date));
    $file_name = explode('.', $value['ObjectName']);
    $file_name = $file_name[0];
    $file_type = substr($value['ObjectName'], strpos($value['ObjectName'], ".") + 1);
    $size = formatSizeUnits($value['Length']);
    $raw_size = $value['Length'];
    $guid = $value['Guid'];
    $items['data'][] = array('name' => $file_name, 'file_type' => $file_type, 'size' => $size, 'raw_size' => $raw_size, 'datetime' => $fixed_date, 'guid' => $guid);
}
echo json_encode($items);

 

Share

Recent Posts

Kennington reservoir drained drone images

A drained and empty Kennington reservoir images from a drone in early July 2024. The…

1 year ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

1 year ago

FTP getting array of file details such as size using PHP

Using FTP and PHP to get an array of file details such as size and…

2 years ago

Creating Laravel form requests

Creating and using Laravel form requests to create cleaner code, separation and reusability for your…

2 years ago

Improving the default Laravel login and register views

Improving the default Laravel login and register views in such a simple manner but making…

2 years ago

Laravel validation for checking if value exists in the database

Laravel validation for checking if a field value exists in the database. The validation rule…

2 years ago