Learn how to use the AWS SDK for PHP with Filebase.
What is AWS SDK - PHP?
AWS SDKs (software development kits) help simplify coding and application development by supporting and providing code objects for use with S3-compatible services. There are a variety of different AWS SDKs, each for a different coding language. This guide covers AWS SDK - PHP. Read below to learn how to use the AWS SDK for PHP with Filebase.
Prerequisites:
Setting Your Filebase Credentials as Environment Variables
SET FILEBASE_SECRET_ACCESS_KEY=[Filebase-Secret-Key]
Alternatively, you may use the credentials stored in your .aws/credentials file and reference them using 'profile' => 'filebase' in place of the 'credentials' array, or you may hard code your credentials. Hard coding is not recommended for production environments.
List All Buckets
The following code example lists all buckets within a user account.
<?phprequire'vendor/autoload.php';useAws\S3\S3Client; useAws\Exception\AwsException;$s3Client =newS3Client(['endpoint'=>'https://s3.filebase.com','region'=>'us-east-1','version'=>'latest','use_path_style_endpoint'=>true,'credentials'=> ['key'=> $FILEBASE_ACCESS_KEY_ID,'secret'=> $FILEBASE_SECRET_ACCESS_KEY, ],]);//Listing all S3 Bucket$buckets = $s3Client->listBuckets();foreach ($buckets['Buckets'] as $bucket) {echo $bucket['Name'] ."\n";}
Upload an Object
The following code example uploads an object to the specified bucket. Replace the following values in the code to match your configuration:
The following code example generates a pre-signed URL for an object in a Filebase bucket. For more information on pre-signed URLs, please see here. This workflow can be used for both public and private buckets. Replace the following values in the code to match your configuration: