Upload file Download file Delete file AWS S3 using php sdk Part 2

Upload file Download file Delete file AWS S3 using php sdk Part 2

using php sdk to upload download and delete file aws s3

ยท

3 min read

Hi Friend ๐Ÿ˜Ž.

In this post we are going to discuss how to use php sdk to upload download and delete file in aws s3.

To get familiar with AWS S3 basics and terminology you can visit

<< Amazon S3 get started using php sdk- Part 1

First we need to create Security Credentials to access aws s3 resources via API.

go to My Account Section -> My Security Credentials

Now click on -> Access keys (access key ID and secret access key)

Click on Create New Access Key.

This will create pair of Access Key ID and Secret Key. We will need this information to make calls via php sdk

image.png

ย 

we can download AWS SDK for PHP by going to
https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/getting-started_installation.html.

For this tutorial just download zip file for the sdk and extract to you working directory.

create file aws-s3.php and include the aws sdk using following code.

ย 

โœ… Initialize S3 client

initialize s3 client using below code

<?php

   require 'aws/aws-autoloader.php';
   use Aws\S3\S3Client;
   use Aws\S3\Exception\S3Exception;

   $s3_client =     new S3Client([
                'region' => 'us-east-2',
                'version' => 'latest',
                'credentials' => [
                    'key' => Your_Access_Key_ID,
                    'secret' => Your_Secret_Key_Access_key
                    ]
            ]);

?>

ย 

ย 

โœ… Create bucket if not exists

create a bucket which will store our file

bucket name must be unique

function used - createBucket

<?php
       if(!$s3_client->doesBucketExist($bucket_name)) {
            try{
                $s3_client->createBucket(array(
                        'Bucket' => 'test-bucket-dev-croc' // bucket name must be unique
                ));
            }
            catch (S3Exception $e) {
                $s3_client->display_aws_error($e);
            }
        }

?>

ย 

ย 

โœ… Upload file to S3

below is the code for uploading file to s3

this will upload file to the given bucket

function used - putObject

<?php
         try{
               $result = $s3_client->putObject([
               'Bucket' => 'test-bucket-dev-croc',
               'Key' => 'test_photo.jpg',
               'SourceFile' => 'C:\Users\dev manish\Documents\test_photo.jpg'
            ]);
        }
        catch (S3Exception $e) {
            $s3_client->display_aws_error($e);
        }

?>

ย 

You can see the uploaded file by going to the s3 console.

image.png

ย 

ย 

โœ… Download file from S3

below is the code for downloading a given file from s3

this will download file from the given bucket

function used - getObject

<?php
         try{
                    $result     = $s3_client->getObject(array(
                    'Bucket'     => 'test-bucket-dev-croc',
                    'Key'         => 'test_photo.jpg',
                    'SaveAs'     => 'downloaded_test_file.jpg' // you can give any name
                    ));
        }
        catch (S3Exception $e) {
                    $s3_client->display_aws_error($e);
                    return false;
        }

?>

ย 

ย 

โœ… Delete a file from S3

below is the code for deleting a file from s3

this will delete a file from the given bucket

function used - deleteObject

<?php
       try{
                $result     = $s3_client->deleteObject(array(
                'Bucket'     => 'test-bucket-dev-croc',
                'Key'         => 'test_photo.jpg'
                ));
            }
        catch (S3Exception $e) {
                $s3_client->display_aws_error($e);
        }

?>

ย 

ย 

Thank you. I hope this information can help you in you work ๐Ÿ‘.

ย