PHP SDK for Sirv S3
https://s3.sirv.com No account yet? Create one freeShort answer
Use the AWS SDK for PHP with the endpoint s3.sirv.com and your Sirv S3 credentials, or the Laravel package for the Sirv S3 API. The examples check if a file exists, list objects, upload, download, copy, rename and delete files, and create folders. The Sirv REST API is another option.
After reading this you will know
- Which API to choose for managing Sirv files with PHP
- How to use the S3 API from PHP or Laravel
- How to upload, download and manage files with PHP
There are multiple ways to use PHP to manage your files on Sirv.
Choose an API
Section titled “Choose an API”Before you start, choose which API you will use. Sirv provides two APIs for managing your files:
- REST API - we recommend using the Sirv REST API. It has more than 60 API methods, it is designed and support by the Sirv team.
- S3 API - the S3 API has fewer API methods and requires some more effort to integrate with but AWS SDKs can be used.
This page describes how to use the S3 API for PHP.
S3 API for PHP
Section titled “S3 API for PHP”If you’re using PHP, you can manage your files with the AWS SDK for PHP.
Alternatively, you can use the simplified class below, which provides a basic API for working with Sirv storage.
- Download the latest version of the Sirv API class (zipped PHP file).
- Require the Sirv API class:
require_once('sirv.api.class.php');- Initialize the class instance with your Sirv S3 key, secret and bucket, from your Uploads settings.
$sirvClient = new SirvClient( // S3 bucket 'ENTER_YOUR_SIRV_S3_BUCKET_HERE', // S3 Access Key 'ENTER_YOUR_SIRV_S3_KEY_HERE', // S3 Secret Key 'ENTER_YOUR_SIRV_S3_SECRET_HERE' );- Check that the connection is working:
if ($sirvClient->testConnection()) { // Connection SUCCEEDED } else { // Connection FAILED }Now you can perform the common actions below.
Check if file/folder exists
Section titled “Check if file/folder exists”// Check if a folder exists (example/folder)$sirvClient->checkIfObjectExists('example/folder');
// Check if a file exists (example/folder/image.jpg)$sirvClient->checkIfObjectExists('example/folder','image.jpg');Get object list
Section titled “Get object list”$items = $sirvClient->getBucketContents('example/folder');Upload files to Sirv
Section titled “Upload files to Sirv”// Upload the local file local/folder/image.jpg to the folder example/folder/ on Sirv$res = $sirvClient->uploadFile( // File path on Sirv 'example/folder/image.jpg', // Local file name 'local/folder/image.jpg');Create a folder
Section titled “Create a folder”// Create the folder example/folder on Sirv$sirvClient->createFolder('example/folder');Delete a file or folder
Section titled “Delete a file or folder”All files must be deleted from a folder before a folder can be deleted.
// Delete the file example/folder/image.jpg from Sirv$sirvClient->deleteFile('example/folder/image.jpg');Download a file
Section titled “Download a file”// Download the file example/folder/image.jpg from Sirv$fileContents = $sirvClient->getFile('example/folder/image.jpg'); if (!empty($fileContents)) { file_put_contents('image.jpg', $fileContents); }Copy a file
Section titled “Copy a file”// Copy the file example/folder/image.jpg to example/folder/image_copy.jpg$sirvClient->copyFile('example/folder/image.jpg', 'example/folder/image_copy.jpg');Rename a file
Section titled “Rename a file”// Rename the file example/folder/image.jpg to example/folder/image_new.jpg$sirvClient->renameFile('example/folder/image.jpg', 'example/folder/image_new.jpg');Laravel package for Sirv S3 API
Section titled “Laravel package for Sirv S3 API”If you use Laravel, consider using this Laravel wrapper to speed up integration. This Laravel filesystem driver for Sirv files was created by a member of the Sirv community.
GitHub package and tutorial: https://github.com/lipit146/filesystem-sirv Packagist repository: https://packagist.org/packages/lipit146/filesystem-sirv
