Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in AWS by (19.1k points)

I am trying to put a new object into a bucket with this code:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

require_once(FCPATH.'s3/aws-autoloader.php');

use Aws\S3\S3Client;

class s3{

    public $_secret_key ="********";

    public $_access_key = "********"; 

    public $_bucket = "tphotos-dev";

 function connect(){

     return  S3Client::factory(array(

    'key'    => $this->_access_key,

    'secret' => $this->_secret_key,

));

 }

 function deleteObject($prefix = false){

    if($prefix){

        $s3 = $this->connect();

        return $s3->deleteMatchingObjects($this->_bucket, $prefix);

    }

 }

 function putObject($file_name,$source_file){

    $s3 = $this->connect();

     $s3->putObject(array(

    'Bucket' => (string)$this->_bucket,

    'Key'    => $file_name,

    'SourceFile'   => $source_file,

    'ACL'         => 'public-read',

    ));

    return $s3->waitUntilObjectExists(array(

    'Bucket' => $this->_bucket,

    'Key'    => $file_name

    ));

 }  

}

?>

so once I do for example:

$s3->putObject('myfilename.jpg',get_file_content("temp/image.jpg"));

it returns error:

Fatal error: Uncaught exception 'Aws\Common\Exception\InvalidArgumentException' with message 'You must specify a non-null value for the Body or SourceFile parameters.' in /Users/ok/Projects/s3/Aws/Common/Client/UploadBodyListener.php:91 

any clue?

1 Answer

0 votes
by (44.4k points)

Fix these, and you will be good to go.

It was just to change this

'SourceFile'   => $source_file,

to

'Body'   => $source_file,

Related questions

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...