Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Amazon SDK - s3 putObject and set Body

hi i'm trying putting 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 ?

like image 735
itsme Avatar asked Aug 21 '13 21:08

itsme


People also ask

Does PHP work in S3?

S3 doesn't run any sort of CGI script (PHP, Perl, Ruby, etc). Think of it as a static html and image repository. If you want to host your PHP application on AWS, consider using AWS Beanstalk.

How do I access an S3 bucket from AWS SDK?

To access Amazon Simple Storage Service, create an AWS. S3 service object. Call the listBuckets method of the Amazon S3 service object to retrieve a list of your buckets. The data parameter of the callback function has a Buckets property containing an array of maps to represent the buckets.

Does Putobject overwrite?

If an object already exists in a bucket, the new object will overwrite it because Amazon S3 stores the last write request.


1 Answers

Sorry i got the fix:

it was just to change this

'SourceFile'   => $source_file,

to

'Body'   => $source_file,
like image 177
itsme Avatar answered Oct 16 '22 23:10

itsme