Skip to content

Instantly share code, notes, and snippets.

@DanitoGH
Forked from keithweaver/s3-upload-via-form.php
Created November 13, 2020 00:46
Show Gist options
  • Select an option

  • Save DanitoGH/d48270646cd8c3666dd4f45793b0bc6f to your computer and use it in GitHub Desktop.

Select an option

Save DanitoGH/d48270646cd8c3666dd4f45793b0bc6f to your computer and use it in GitHub Desktop.

Revisions

  1. @keithweaver keithweaver revised this gist Jul 18, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion s3-upload-via-form.php
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    <?php
    // This file demonstrates file upload to an S3 bucket. This is for using file upload via a
    // file compared to just having the link. If you are doing it via link, refer to this:
    //
    // https://gist.github.com/keithweaver/08c1ab13b0cc47d0b8528f4bc318b49a
    //
    // You must setup your bucket to have the proper permissions. To learn how to do this
    // refer to:
  2. @keithweaver keithweaver created this gist Jul 18, 2017.
    77 changes: 77 additions & 0 deletions s3-upload-via-form.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,77 @@
    <?php
    // This file demonstrates file upload to an S3 bucket. This is for using file upload via a
    // file compared to just having the link. If you are doing it via link, refer to this:
    //
    //
    // You must setup your bucket to have the proper permissions. To learn how to do this
    // refer to:
    // https://github.com/keithweaver/python-aws-s3
    // https://www.youtube.com/watch?v=v33Kl-Kx30o

    // I will be using composer to install the needed AWS packages.
    // The PHP SDK:
    // https://github.com/aws/aws-sdk-php
    // https://packagist.org/packages/aws/aws-sdk-php
    //
    // Run:$ composer require aws/aws-sdk-php
    require 'vendor/autoload.php';

    use Aws\S3\S3Client;
    use Aws\S3\Exception\S3Exception;

    // AWS Info
    $bucketName = 'SUB_YOUR_BUCKET_NAME_IN';
    $IAM_KEY = 'SUB_YOUR_IAM_KEY_IN';
    $IAM_SECRET = 'SUB_YOUR_IAM_SECRET_IN';

    // Connect to AWS
    try {
    // You may need to change the region. It will say in the URL when the bucket is open
    // and on creation.
    $s3 = S3Client::factory(
    array(
    'credentials' => array(
    'key' => $IAM_KEY,
    'secret' => $IAM_SECRET
    ),
    'version' => 'latest',
    'region' => 'us-east-2'
    )
    );
    } catch (Exception $e) {
    // We use a die, so if this fails. It stops here. Typically this is a REST call so this would
    // return a json object.
    die("Error: " . $e->getMessage());
    }


    // For this, I would generate a unqiue random string for the key name. But you can do whatever.
    $keyName = 'test_example/' . basename($_FILES["fileToUpload"]['tmp_name']);
    $pathInS3 = 'https://s3.us-east-2.amazonaws.com/' . $bucketName . '/' . $keyName;

    // Add it to S3
    try {
    // Uploaded:
    $file = $_FILES["fileToUpload"]['tmp_name'];

    $s3->putObject(
    array(
    'Bucket'=>$bucketName,
    'Key' => $keyName,
    'SourceFile' => $file,
    'StorageClass' => 'REDUCED_REDUNDANCY'
    )
    );

    } catch (S3Exception $e) {
    die('Error:' . $e->getMessage());
    } catch (Exception $e) {
    die('Error:' . $e->getMessage());
    }


    echo 'Done';

    // Now that you have it working, I recommend adding some checks on the files.
    // Example: Max size, allowed file types, etc.
    ?>