2013-08-21 65 views
12

hi i đang cố gắng đưa một đối tượng mới vào một xô với mã này:PHP Amazon SDK - s3 putObject và thiết lập Body

<?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 
    )); 

} 

} 

>

nên một lần tôi làm ví dụ:

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

nó sẽ trả về lỗi:

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 

bất kỳ đầu mối?

+0

Trong trường hợp của tôi đó là vì tôi đã có một giới hạn về ** upload_max_filesize * * và ** post_max_size ** vì $ source_file không đến (là null), máy chủ xác thực $ source_file> upload_max_filesize hoặc post_max_size. –

Trả lời

27

Xin lỗi tôi đã nhận sửa chữa:

nó chỉ là để thay đổi điều này

'SourceFile' => $source_file, 

để

'Body' => $source_file, 
+0

Làm việc tốt, Cảm ơn – Elby

+0

Xin chào Bạn có thể giúp tôi được không. Khi cố gắng sử dụng cùng một hàm tôi nhận được phản hồi 403. – ARUN

+0

Cảm ơn @sbaaaang, nhưng tại sao? Tài liệu nói http://docs.aws.amazon.com/aws-sdk-php/v2/guide/service-s3.html#uploading-a-file Phiên bản sdk 2 –