Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload file on amazon S3 with PHP SDK

I'm trying to upload a picture on my amazon S3 via their PHP SDK. So I made a little script to do so. However, my script doesn't work and my exception doesn't send me back any error message.

I'm new with AWS thank you for your help.

Here is the code :

Config.php

<?php 

return array(
'includes' => array('_aws'),
'services' => array(
  'default_settings' => array(
      'params' => array(
          'key'    => 'PUBLICKEY',
          'secret' => 'PRIVATEKEY',
          'region' => 'eu-west-1'
      )
    )
  )
);

?>

Index.php

 <?php


//Installing AWS SDK via phar
require 'aws.phar';

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

$bucket = 'infact';
$keyname = 'myImage';

// $filepath should be absolute path to a file on disk                      
$filepath = 'image.jpg';

// Instantiate the client.
$s3 = S3Client::factory('config.php');

// Upload a file.
try {

$result = $s3->putObject(array(
    'Bucket'       => $bucket,
    'Key'          => $keyname,
    'SourceFile'   => $filePath,
    'ContentType'  => 'text/plain',
    'ACL'          => 'public-read',
    'StorageClass' => 'REDUCED_REDUNDANCY'
));

 // Print the URL to the object.
    echo $result['ObjectURL'] . "\n";
} catch (S3Exception $e) {
    echo $e->getMessage() . "\n";
}

?>

EDIT : I'm now using this code but its still not working. I don't even have error or exception message.

    <?php

require 'aws.phar';

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

$bucket = 'infactr';
$keyname = 'sample';
// $filepath should be absolute path to a file on disk                      
$filepath = 'image.jpg';

// Instantiate the client.
$s3 = S3Client::factory(array(
    'key'    => 'key',
    'secret' => 'privatekey',
    'region' => 'eu-west-1'

    ));

try {
    // Upload data.
    $result = $s3->putObject(array(
        'Bucket' => $bucket,
        'Key'    => $keyname,
        'SourceFile'   => $filePath,
        'ACL'    => 'public-read',
        'ContentType' => 'image/jpeg'
    ));

    // Print the URL to the object.
    echo $result['ObjectURL'] . "\n";
} catch (S3Exception $e) {
    echo $e->getMessage() . "\n";
}

?>
like image 948
casusbelli Avatar asked Mar 16 '14 20:03

casusbelli


1 Answers

Try something like this (from the AWS docs):

<?php

require 'aws.phar';

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

$bucket = '<your bucket name>';
$keyname = 'sample';
// $filepath should be absolute path to a file on disk                      
$filepath = '/path/to/image.jpg';

// Instantiate the client.
$s3 = S3Client::factory(array(
    'key'    => 'your AWS access key',
    'secret' => 'your AWS secret access key'
));

try {
    // Upload data.
    $result = $s3->putObject(array(
        'Bucket' => $bucket,
        'Key'    => $keyname,
        'SourceFile'   => $filepath,
        'ACL'    => 'public-read'
    ));



    // Print the URL to the object.
    echo $result['ObjectURL'] . "\n";
} catch (S3Exception $e) {
    echo $e->getMessage() . "\n";
}

?>

It works fine for me as long as you have the right credentials. Keep in mind that the key name is the name of your file in S3 so if you want to have your key have the same name of your file you have to do something like: $keyname = 'image.jpg'; . Also, a jpg is generally not a plain/text file type, you can ommit that Content-type field or you can just simply specify: image/jpeg

like image 78
Rico Avatar answered Sep 23 '22 02:09

Rico