7

I've spent the last few hours following tutorials for implementing file uploads to Amazon S3 using php. I uploaded the most recent version of Donovan Schönknecht's S3 class to my server (as S3.php) and I am trying to use the following code to test upload capability. I know this code will work because I've seen numerous examples in action.

<?php

require('S3.php');

$s3 = new S3('KEY', 'SECRET KEY');

//insert into s3
$new_name = time() . '.txt';

S3::putObject(
'upload-me.txt',
'bucketName',
$new_name,
S3::ACL_PUBLIC_READ,
array(),
array(),
S3::STORAGE_CLASS_RRS

);

?>

I get an error 500 server error when I attempt to load this page. Additionally, every other reputable tutorial of this nature has given me the same error 500.

I verified that my key and secret key are valid by connecting to S3 with Cyberduck.

Does anyone have a clue as to what I could be doing incorrectly?

Thanks,

Sean

1
  • 2
    500 error is probably an error in your php code. Turn on display_errors or check your logs to see the error message. Commented Apr 27, 2013 at 20:29

9 Answers 9

12

As it turns out, I was missing the cURL extension for PHP and this was causing an issue as the S3 class I was using required the use of cURL. All is working now.

Sign up to request clarification or add additional context in comments.

Comments

9

You should also consider using the official AWS SDK for PHP. Examples for using S3 with the SDK can be found in their S3 user guide.

4 Comments

The official AWS SDK for PHP only works with composer and there is no ZIP file download anymore. I can not use composer on a shared hosting platform for some of my websites.
There is a zip and phar included in every release that includes the entire closure dependencies such that Composer is not required.
The SDK's README and user guide both show where to download the zip. Is there some particular reason you though that the SDK only worked with Composer?
That is great news! I was under the impression that after Ver 1.6, Ver 2.0 would require composer. I'll try later the week
9

You can download most recent version of Amazon PHP SDK by running following composer command

composer require aws/aws-sdk-php

Further configuration to upload file on Amazon s3 are following

// Include the SDK using the Composer autoloader
require 'vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;

// Set Amazon s3 credentials
$client = S3Client::factory(
  array(
    'key'    => "your-key",
    'secret' => "your secret key"
  )
);

try {
  $client->putObject(array(
    'Bucket'=>'your-bucket-name',
    'Key' =>  'your-filepath-in-bucket',
    'SourceFile' => 'source-filename-with-path',
    'StorageClass' => 'REDUCED_REDUNDANCY'
  ));

} catch (S3Exception $e) {
  // Catch an S3 specific exception.
  echo $e->getMessage();
}

Get step by step details from here Amazon S3 File Upload Using PHP

Comments

3

Following example worked for me:

<?php
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;

$client = S3Client::factory([
    'version' => 'latest',
    'region'  => 'us-west-1',
    'credentials' => [
        'key'    => "<scret-key>",
        'secret' => "<my-secret>"
    ]
 ]);

try {
    $client->putObject([
        'Bucket'     =>'<my-bucket-name>',
        'Key'        => '<file-name>',
        'SourceFile' => '<file-path-on-server>',    // like /var/www/vhosts/mysite/file.csv
        'ACL'        => 'public-read',
    ]);
} catch (S3Exception $e) {
    // Catch an S3 specific exception.
    echo $e->getMessage();
}

Getting security credentials:

Getting region code

Comments

1

Use this one to Upload Images using a form and it's working Fine for me you may try using it with your code

$name = $_FILES['photo']['name'];
$size = $_FILES['photo']['size'];
$tmp = $_FILES['photo']['tmp_name'];

//////Upload Process


    // Bucket Name
    $bucket = 'bucket-name';
    require_once('S3.php');

    //AWS access info
    $awsAccessKey = 'awsAccessKey';
    $awsSecretKey = 'awsSecretKey';

    //instantiate the class
    $s3 = new S3($awsAccessKey, $awsSecretKey);

    $s3->putBucket($bucket, S3::ACL_PUBLIC_READ);


    //Rename image name. 
    $actual_image_name = time();

        //Upload to S3
    if($s3->putObjectFile($tmp, $bucket , $actual_image_name, S3::ACL_PUBLIC_READ) )
    {
        $image='http://'.$bucket.'.s3.amazonaws.com/'.$actual_image_name;
    }else{
        echo 'error uploading to S3 Amazon';
    }

Comments

1

I never found a updated Script with Amazons latest sdk. i have made it by myself. it woks as a php commandline interpreter script. give it a try :

https://github.com/arizawan/aiss3clientphp

Comments

0

I'm not familiar with S3 API, but i used it as the storage with https://github.com/KnpLabs/Gaufrette. Gaufrette is a library that provides pretty nice abstraction layer above S3 and other file services/systems.

Comments

0

Here is sample code to upload images on Amazon S3.

// Bucket Name
$bucket="BucketName";
if (!class_exists('S3'))require_once('S3.php');

    //AWS access info
    if (!defined('awsAccessKey')) define('awsAccessKey', 'ACCESS_KEY');
    if (!defined('awsSecretKey')) define('awsSecretKey', 'ACCESS_Secret_KEY');

    $s3 = new S3(awsAccessKey, awsSecretKey);
    $s3->putBucket($bucket, S3::ACL_PUBLIC_READ);
    if($s3->putObjectFile($tmp, $bucket , $image_name_actual,S3::ACL_PUBLIC_READ) )
    {
        $message = "S3 Upload Successful.";
        $s3file='http://'.$bucket.'.s3.amazonaws.com/'.$actual_image_name;
        echo "<img src='$s3file'/>";
        echo 'S3 File URL:'.$s3file;
    }
    else{
        $message = "S3 Upload Fail.";
    }
}

Comments

0

Below is the best solution. Its using multipart upload.Make Sure to install Aws SDK for PHP before using

require 'vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\Exception\AwsException;
use Aws\S3\MultipartUploader;
use Aws\Exception\MultipartUploadException;

try {
  $s3Client = new S3Client([
    'version'     => 'latest',
    'region'      => 'us-east-1',
    'credentials' => [
        'key'    => 'AKIAUMIZJR5U5IO7M3',
        'secret' => 'BmcA3vFso1bc/9GVK7nHJtFk0tQL6Vi5OoMySO',
    ],
  ]);
 
  // Use multipart upload
  $source = 'https://b8q9h6y2.stackpathcdn.com/wp-content/uploads/2016/08/banner-for-website-4.png';
  $uploader = new MultipartUploader($s3Client, $source, [
      'bucket' => 'videofilessandeep',
      'key' => 'my-file.png',
      'ACL' => 'public-read',
  ]);
  
  try {
      $result = $uploader->upload();
      echo "Upload complete: {$result['ObjectURL']}\n";
  } catch (MultipartUploadException $e) {
      echo $e->getMessage() . "\n";
  }
 } catch (Exception $e) {
    print_r($e);
}
  

1 Comment

are those your real credentials? better not to share them publicly!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.