The BunnyCDN PHP API class can do many tasks without having to log into BunnyCDN and use the browser GUI.
In this post I go through creating a Storage Zone, connecting to it then creating directories and uploading images into it.
The only requirements being that you have the class and your BunnyCDN account API key.
Start by creating a file that will run your code in the same directory as bunnyAPI.php. Add your API key.
<?php require_once('bunnyAPI.php'); $bunny = new BunnyAPI(); $bunny->apiKey('MY-API-KEY-HERE');
Create a new Storage zone with:
echo $bunny->addStorageZone('imageBackups');
If the name isn’t in use this returns:
{ "Id": 24289, "UserId": "OMITTED", "Name": "imagebackups", "Password": "OMITTED", "DateModified": "2019-11-26T04:16:56.1991174Z", "Deleted": false, "StorageUsed": 0, "FilesStored": 0, "Region": "DE", "ReplicationRegions": null, "PullZones": null, "ReadOnlyPassword": "OMITTED" }
Grab the Password because that will be used to create a connection with the Storage zone next. You can always review all this data with:
echo $bunny->listStorageZones();
Create the Storage zone connection with the name and password from the returned data above:
$bunny->zoneConnect('imagebackups', 'THEPASSWORD');
The above zoneConnect() is needed to be set before all of the following examples!
Let’s create a folder in the Storage zone imageBackups
echo $bunny->createFolder('Drone');
View what exists in imageBackups
$bunny->jsonHeader(); echo $bunny->listAll();
returns:
{ "storage_name": "imagebackups", "current_dir": "", "data": [ { "name": "Drone", "file_type": null, "size": null, "is_dir": true, "created": "2019-11-26 04:30:20", "last_changed": "2019-11-26 04:30:20", "guid": "906cde44-aaa5-464d-8d92-3a019c1a2854" } ] }
As you can see our newly created Drone folder exists.
I want to make another folder inside of Drone/:
$bunny->createFolder('Drone/theLake');
Output the contents of the Drone folder:
$bunny->jsonHeader(); echo $bunny->listAll('/Drone');
{ "storage_name": "imagebackups", "current_dir": "/Drone", "data": [{ "name": "theLake", "file_type": null, "size": null, "is_dir": true, "created": "2019-11-26 04:32:49", "last_changed": "2019-11-26 04:32:49", "guid": "d99fd6a8-f60b-4dbb-a0c2-4a78cb24df1c" }] }
Success! Now to upload files into /Drone/theLake
In my current working directory I have created a folder called uploads and in it sits tullaroop-reservoir-drone-13.jpg (The drone image).
Uploading this file is done with:
$bunny->uploadFile('upload/tullaroop-reservoir-drone-13.jpg', 'Drone/theLake/adroneimage.jpg');
$bunny->jsonHeader(); echo $bunny->listAll('/Drone/theLake');
{ "storage_name": "imagebackups", "current_dir": "/Drone/theLake", "data": [ { "name": "adroneimage.jpg", "file_type": "jpg", "size": 876.76171875, "is_dir": false, "created": "2019-11-26 04:43:51", "last_changed": "2019-11-26 04:43:51", "guid": "a8d8a1f0-5915-4cf2-b538-a305fe4d440e" } ] }
From the returned data you can see the image is 876KB.
Another feature is uploading all the files in a folder. I added tullaroop-reservoir-drone-15.jpg and tullaroop-reservoir-drone-15.jpg to uploads and ran
$bunny->uploadAllFiles('upload/', 'Drone/theLake/');
Which uploads all the files in upload/ to Drone/theLake/ whilst keeping their original names
$bunny->jsonHeader(); echo $bunny->listAll('/Drone/theLake');
{ "storage_name": "imagebackups", "current_dir": "/Drone/theLake", "data": [ { "name": "adroneimage.jpg", "file_type": "jpg", "size": 876.76171875, "is_dir": false, "created": "2019-11-26 04:43:51", "last_changed": "2019-11-26 04:43:51", "guid": "a8d8a1f0-5915-4cf2-b538-a305fe4d440e" }, { "name": "tullaroop-reservoir-drone-15.jpg", "file_type": "jpg", "size": 984.2802734375, "is_dir": false, "created": "2019-11-26 04:53:38", "last_changed": "2019-11-26 04:53:38", "guid": "c559636e-7d14-4ff5-97a7-e9cc6247e5cd" }, { "name": "tullaroop-reservoir-drone-16.jpg", "file_type": "jpg", "size": 1118.3857421875, "is_dir": false, "created": "2019-11-26 04:53:49", "last_changed": "2019-11-26 04:53:49", "guid": "981b68b3-4b53-4717-aa3a-78b714cf2481" } ] }
This post showcases just a small fraction of what the class can do.
A drained and empty Kennington reservoir images from a drone in early July 2024. The…
Merrimu Reservoir from drone. Click images to view larger.
Using FTP and PHP to get an array of file details such as size and…
Creating and using Laravel form requests to create cleaner code, separation and reusability for your…
Improving the default Laravel login and register views in such a simple manner but making…
Laravel validation for checking if a field value exists in the database. The validation rule…