2014-03-06 131 views
1

我試圖通過Laravel 4上傳發布文件到Amazon S3

將文件上傳到亞馬遜S3用戶後提交表單,文件將被傳遞到哪裏,我需要使用Amazon PHP SDK功能並將文件上傳到Amazon S3存儲桶。

但是,如何將文件直接上傳到Amazon S3而不將文件保存到服務器上。

我當前的代碼看起來像這樣,

private function uploadVideo($vid){ 

    $file = $vid; 

    $filename = $file->getClientOriginalName(); 

    if (!class_exists('S3'))require_once('S3.php'); 
    if (!defined('awsAccessKey')) define('awsAccessKey', '123123123'); 
    if (!defined('awsSecretKey')) define('awsSecretKey', '123123123'); 
    $s3 = new S3(awsAccessKey, awsSecretKey); 
    $s3->putBucket("mybucket", S3::ACL_PUBLIC_READ); 

    $s3->putObject($vid, "mybucket",$filename , S3::ACL_PUBLIC_READ); 


} 
+0

官方的SDK?我知道V2可以將混合輸入(字符串,資源)輸入到「body」[putObject的參數]中(http://docs.aws.amazon.com/aws-sdk-php/latest/class-Aws.S3 .S3Client.html#_putObject)。我更喜歡使用[upload](http://docs.aws.amazon.com/aws-sdk-php/latest/class-Aws.S3.S3Client.html#_upload),因爲它基於文件包裝放置對象或分段上傳尺寸。 – Scuzzy

+0

它的版本是0.2.3。 –

+0

我發現這個項目在github上,我不知道它是哪個版本,https://github.com/tpyo/amazon-s3-php-class –

回答

5

抓住從http://docs.aws.amazon.com/aws-sdk-php/latest/index.html

您正在使用哪種版本的SDK此示例使用http://docs.aws.amazon.com/aws-sdk-php/latest/class-Aws.S3.S3Client.html#_upload

require('aws.phar'); 
use Aws\S3\S3Client; 
use Aws\Common\Enum\Region; 

// Instantiate the S3 client with your AWS credentials and desired AWS region 
$client = S3Client::factory(array(
    'key' => 'KEY HERE', 
    'secret' => 'SECRET HERE', 
    'region' => Region::AP_SOUTHEAST_2 // you will need to change or remove this 
)); 

$result = $client->upload(
    'BUCKET HERE', 
    'OBJECT KEY HERE', 
    'STRING OF YOUR FILE HERE', 
    'public-read' // public access ACL 
);