Edge Storage API
The Edge Storage API provides a simple RESTful interface for managing files in your storage zones.
Setup
<?php
require 'vendor/autoload.php';
use ToshY\BunnyNet\BunnyHttpClient;
use ToshY\BunnyNet\Enum\Endpoint;
$bunnyHttpClient = new BunnyHttpClient(
client: new \Symfony\Component\HttpClient\Psr18Client(),
// Provide the password of the specific storage zone.
apiKey: '6bf3d93a-5078-4d65-a437-501c44576fe6',
// Use the Edge Storage endpoint for your storage zone.
baseUrl: Endpoint::EDGE_STORAGE_FS
);
Usage
Manage Files
Download File
// Root directory.
$bunnyHttpClient->request(
new \ToshY\BunnyNet\Model\Api\EdgeStorage\ManageFiles\DownloadFile(
storageZoneName: 'my-storage-zone-1',
fileName: 'bunny.jpg',
)
);
// Subdirectory.
$bunnyHttpClient->request(
new \ToshY\BunnyNet\Model\Api\EdgeStorage\ManageFiles\DownloadFile(
storageZoneName: 'my-storage-zone-1',
fileName: 'custom.css',
path: 'css',
)
);
Download Zip
// Root directory.
$bunnyHttpClient->request(
new \ToshY\BunnyNet\Model\Api\EdgeStorage\ManageFiles\DownloadZip(
storageZoneName: 'my-storage-zone-1',
body: [
'RootPath' => '/my-storage-zone-1/',
'Paths' => [
'/my-storage-zone-1/',
],
],
)
);
// Subdirectory.
$bunnyHttpClient->request(
new \ToshY\BunnyNet\Model\Api\EdgeStorage\ManageFiles\DownloadZip(
storageZoneName: 'my-storage-zone-1',
body: [
'RootPath' => '/my-storage-zone-1/',
'Paths' => [
'/my-storage-zone-1/images/',
],
],
)
);
Note
- Make sure your
RootPathandPathscontain leading and trailing slashes.- If you omit the slashes in
RootPaththis will result in a400status code. - If you omit the slashes in
Pathsthis will result in a200status code with an empty ZIP file.
- If you omit the slashes in
Warning
- This endpoint (with method
POST) is currently not documented in the API specifications. - This request may fail or timeout if the requested directory has too many files or is too big.
Upload File
/*
* File contents read into string from the local filesystem.
*/
$content = file_get_contents('./local-bunny.jpg');
/*
* File contents handle from a `$filesystem` (e.g. Flysystem FtpAdapter).
*/
$content = $filesystem->readStream('./remote-custom.css');
// Root directory.
$bunnyHttpClient->request(
new \ToshY\BunnyNet\Model\Api\EdgeStorage\ManageFiles\UploadFile(
storageZoneName: 'my-storage-zone-1',
path: '',
fileName: 'remote-bunny.jpg',
body: $content,
)
);
// Subdirectory.
$bunnyHttpClient->request(
new \ToshY\BunnyNet\Model\Api\EdgeStorage\ManageFiles\UploadFile(
storageZoneName: 'my-storage-zone-1',
path: 'css',
fileName: 'remote-custom.css',
body: $content,
)
);
// Subdirectory with additional SHA256 checksum header.
$bunnyHttpClient->request(
new \ToshY\BunnyNet\Model\Api\EdgeStorage\ManageFiles\UploadFile(
storageZoneName: 'my-storage-zone-1',
path: 'css',
fileName: 'remote-custom.css',
body: $content,
headers: [
'Checksum' => '253852201067799F637D8BB144F32D7AAEEF3182BEAA61168E0AA87DBE336D7C',
],
)
);
Warning
- While a hash value in hexidecimal string representation is case insensitive, the value for the
Checksumheader must be in uppercase characters to ensure a successful upload. - If an incorrect
Checksumis provided, the response will still be201but the file will not be uploaded.
Delete File
// Root directory.
$bunnyHttpClient->request(
new \ToshY\BunnyNet\Model\Api\EdgeStorage\ManageFiles\DeleteFile(
storageZoneName: 'my-storage-zone-1',
path: '',
fileName: 'bunny.jpg',
)
);
// Subdirectory.
$bunnyHttpClient->request(
new \ToshY\BunnyNet\Model\Api\EdgeStorage\ManageFiles\DeleteFile(
storageZoneName: 'my-storage-zone-1',
path: 'css',
fileName: 'custom.css',
)
);
Browse Files
List Files
// Root directory.
$bunnyHttpClient->request(
new \ToshY\BunnyNet\Model\Api\EdgeStorage\BrowseFiles\ListFiles(
storageZoneName: 'my-storage-zone-1',
path: '',
)
);
// Subdirectory.
$bunnyHttpClient->request(
new \ToshY\BunnyNet\Model\Api\EdgeStorage\BrowseFiles\ListFiles(
storageZoneName: 'my-storage-zone-1',
path: 'css',
)
);