Failure uploading Image on AmazonS3 with PHP SDK

0 votes

Trying to upload a picture on my Amazon S3 through their PHP SDK. Made a script for it, my script is not even working neither it's sending me back an error

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";
}
?>


Afer this, I used this code, I hope it will work, but not even working neither it's reverting any exceptional error


<?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";
}
?>
May 4, 2018 in AWS by Flying geek
• 3,160 points
559 views
Hi everyone. I have a trouble with it
What exactly is the problem? Can you give more details about it.
hello all. thanks for approved.
Hmm. Its me again. I am serwesen from Chinese.
Hey @Soongaind, Is there any clarification we can help you with.
@Soongaind What is the problem you are facing can you give a brief about it?
I have a question for you. How can i do it everyday? hm? / Migedonis
@Migegaind are you saying that you want to post questions daily?

1 answer to this question.

0 votes

Try this, I took it out from Amazon Documents, hope it helps.

<?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";
}

?>




After this, name your file sample.php and run it via the command line:

php sample.php



Make sure that the key name is the name of your file in S3 hence if you want to have your key to have the same name of your file, try this way: $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

answered May 4, 2018 by Cloud gunner
• 4,280 points

Related Questions In AWS

0 votes
1 answer

Web Hosting on Amazon AWS (PHP + MySQL)

You can simply refer the following link, ...READ MORE

answered Aug 7, 2018 in AWS by Priyaj
• 57,300 points
118 views
0 votes
1 answer

How to switch between multiple accounts with AWS SDK

You can define multiple profiles in ~/.aws/credentials ...READ MORE

answered Aug 24, 2018 in AWS by Priyaj
• 57,300 points
67 views
+1 vote
1 answer

How to authenticate session in application running on Beanstalk with boto3

The recommended way of managing credentials used ...READ MORE

answered Aug 28, 2018 in AWS by Priyaj
• 57,300 points
149 views
0 votes
1 answer

Deploy Docker Containers from Docker Cloud

To solve this problem, I followed advice ...READ MORE

answered Sep 3, 2018 in AWS by Priyaj
• 57,300 points
265 views
+13 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 26, 2018 in DevOps & Agile by DragonLord999
• 8,380 points
254 views
+1 vote
2 answers

Starting with an AWS Instance with API and AUTHPARAMS

The API is usually much easier to ...READ MORE

answered Apr 17, 2018 in AWS by Cloud gunner
• 4,280 points
533 views