PHP 7.4+ SDK¶
In this section, we will show you how to use Tebi with Amazon’s PHP SDK with composer as our package manager.
Installation¶
Create your PHP project as usual. Make sure to install the required package via composer.
# installing composer, skip if you got it already installed!
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '55ce33d7678c5a611085589f1f3ddf8b3c52d662cd01d4ba75c0ee0459970c2200a51f492d557530c71c15d8dba01eae') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"
php7 composer.phar require "aws/aws-sdk-php"
Using the SDK¶
Include the composer-standard “vendor autoload” via their code-stub, and declare the use of the AWS SDK.
1<?php
2
3require __DIR__ . '/vendor/autoload.php';
4use Aws\S3\S3Client;
5use Aws\Exception\AwsException;
Create an instance of the S3 Client. Make sure to replace YOUR_KEY and YOUR_SECRET.
1<?php
2/* initialize connection*/
3$s3Client = new Aws\S3\S3Client([
4 "credentials" => [
5 "key" => "YOUR_KEY",
6 "secret" => "YOUR_SECRET"
7 ],
8 "endpoint" => "https://s3.tebi.io",
9 "region" => "de",
10 "version" => "2006-03-01"
11]);
This is all that needs to be completed. With the $s3Client in our hands, we can start using regular S3 commands.
For example, listing buckets:
1<?php
2echo "Listing available buckets: "."\n";
3$buckets = $s3Client->listBuckets();
4foreach($buckets["Buckets"] as $b) {
5 echo $b["Name"] . "\n";
6}
7echo "---"."\n";
Uploading files:
1<?php
2echo "Uploading test file..."."\n";
3$result = $s3Client->putObject([
4 'Bucket' => "YOUR_BUCKET",
5 'Key' => "filename_in_bucket",
6 'SourceFile' => "/path/to/your/local/file",
7]);
8echo "Upload result: ".$result["@metadata"]["statusCode"]."\n"."---"."\n";
Or listing bucket contents:
1<?php
2echo "Content of bucket:"."\n"."---"."\n";
3$objs = $s3Client->listObjects(['Bucket' => "YOUR_BUCKET"]);
4foreach($objs["Contents"] as $o) {
5 echo $o["Key"]."\n";
6}
For more available functions, check the intellisense of your favorite IDE, or go to official documentation by Amazon.