将发布的文件上传到亚马逊s3

时间:2014-03-06 12:35:32

标签: php amazon-web-services amazon-s3 laravel laravel-4

我正在尝试通过Laravel 4将文件上传到Amazon 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);


}

1 个答案:

答案 0 :(得分: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
);