PHP code example of slivka-b / selectel-cloud-storage
1. Go to this page and download the library: Download slivka-b/selectel-cloud-storage library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
slivka-b / selectel-cloud-storage example snippets
// Retrieve single container.
$container = $storage->getContainer('my-container');
// Create new container.
$type = 'public'; // Can be 'public', 'private' or 'gallery'.
$newContainer = $storage->createContainer('new-container-name', $type);
// Retrieve containers list.
$containers = $storage->containers();
$containers = $storage->containers();
// Check if container exists.
if ($containers->has('my-container')) {
// Container exists.
}
// Get containers count.
$containersCount = count($containers); // Or $containers->count();
// Access specific container.
$container = $containers['my-container']; // Or $containers->get('my-container');
// Iterate through containers.
foreach ($containers as $container) {
echo 'Container "'.$container->name().'" has size of '.$container->size().' bytes';
}
// Send JSON representation of collection.
header('Content-Type: application/json;charset=utf-8');
echo json_encode($containers);
$container = $containers->get('my-container');
// Get container attributes.
$name = $container->name(); // 'container-name'.
$type = $container->type(); // 'private', 'public' or 'gallery'.
$filesCount = $container->filesCount(); // or count($container); // or $container->count();
$sizeInBytes = $container->size();
$uploadedBytes = $container->uploadedBytes(); // Total number of bytes uploaded to container (rx_bytes).
$downloadedBytes = $container->downloadedBytes(); // Total number of bytes downloaded from container (tx_bytes).
$json = json_encode($container); // JSON representation of container.
// Change container type.
$container->setType('private'); // Set container visiblity to 'public', 'private' or 'gallery'.
// Check if file exists in container.
$fileExists = $container->files()->exists('/path/to/file.txt'); // true or false.
// Get single file instance.
$file = $container->files()->find('/path/to/file.txt');
// Delete container.
// Note: container must be empty!
$container->delete();
// All files (first 10000 by default).
$files = $container->files()->get(); // or $container->files()->all();
// Files from specific directory.
$filesFromDirectory = $container->files()->fromDirectory('/directory')->get();
// Files that satisfies given prefix.
// Useful for retrieving files with same name pattern:
// image-001.jpg, image-002.jpg, image-003.jpg, etc.
$filesWithPrefix = $container->files()->withPrefix('image-')->get();
// You can also combine fromDirectory() and withPrefix() methods to load prefixed files
// from a specific directory:
$filesFromDirectoryWithPrefix = $container->files()->fromDirectory('/directory')->withPrefix('image-')->get();
// You can apply limit/marker values to any results set (before calling get() method).
// Marker file is a filename of last file from previous request (pagination).
// If you have 100 files with names like 'image-001.jpg', 'image-002.jpg', ... , 'image-100.jpg'
// and you need to load 50 files from 'image-051.jpg', you can do this:
$files = $container->files()
->fromDirectory('photos')
->withPrefix('image-')
->limit(50, 'image-050.jpg')
->get();
// Note: if you're working inside a directory (fromDirectory('photos')), then you can omit
// its path when using withPrefix() or marker file. If you're not in a directory, then
// full path to prefixed files and/or marker file is
use ArgentCrusade\Selectel\CloudStorage\Api\ApiClient;
use ArgentCrusade\Selectel\CloudStorage\FluentFilesLoader;
$api = new ApiClient('username', 'password');
$filesLoader = new FluentFilesLoader($api, 'container-name', '/container-name');
$files = $filesLoader->fromDirectory('photos')->limit(10)->asFileObjects()->get();
$params = [
'contentType' => 'application/json',
'contentDisposition' => 'attachment; filename="filename.json"',
'deleteAfter' => $seconds, // File will be deleted in X seconds after upload.
'deleteAt' => strtotime('next monday'), // File will be deleted at given UNIX timestamp.
];