Using Amazon S3 with php SDK
On signing up with Amazon Web Services, you get a Free Tier Usage for EC2, S3, for one year. Here in this post, I am going to help you understand about Amazon S3 and use it with PHP. First of all, you need to have an amazon account once you have signed up with AWS, you now can use the various AWS services. Here in this post, I am going to help you with using Amazon S3 using PHP. You can download the PHP SDK from https://github.com/aws/aws-sdk-php.
Make AWS connection
List all buckets
Display owner id
Listing items in a bucket
Downloading a file from AWS S3 ( for our case me.jpg from S3 is downloaded as myphoto.jpg )
Uploading file with public read access
Deleting a file, (in our example we are deleting mansi.jpg)
Deleting a bucket
Before deleting a bucket, delete items in bucket
Make AWS connection
use Aws\S3\S3Client;
$client = S3Client::factory(array( 'key' => 'key_value', 'secret' => 'secret_value' ));
List all buckets
$buckets = $client->listBuckets();
Display owner id
echo $buckets->getPath('Owner/ID');
Listing items in a bucket
$iterator = $client->getIterator('ListObjects', array( 'Bucket' => 'name_of_bucket' )); foreach ($iterator as $object) { echo $object['Key'] . "\n"; }
Downloading a file from AWS S3 ( for our case me.jpg from S3 is downloaded as myphoto.jpg )
$result = $client->getObject(array( 'Bucket' => 'name_of_bucket', 'Key' => 'me.jpg', 'SaveAs' => './myphoto.jpg' ));
Uploading file with public read access
$client->putObject(array( 'Bucket' => 'name_of_bucket', 'Key' => 'file_name_to_be_saved_as.ext', 'SourceFile' => './file_to_be_uploaded.ext', 'ACL' => 'public-read' ));
Deleting a file, (in our example we are deleting mansi.jpg)
$client->deleteObject(array( 'Bucket'=>'name_of_bucket', 'Key'=>'mansi.jpg' ));
Deleting a bucket
$client->deleteBucket(array('Bucket' => 'name_of_bucket');
Before deleting a bucket, delete items in bucket
$clear = new ClearBucket($client, $bucket); $clear->clear();
Comments
Post a Comment