uploading posted file to amazon s3

馋奶兔 提交于 2019-12-23 02:45:12

问题


I'm trying to upload a file to Amazon S3 via Laravel 4.

After user submit a form, the file will be passed to a function where I need to use Amazon PHP SDK and upload the file to Amazon S3 bucket.

But how do I upload the file straight away to Amazon S3 without saving the file onto server.

My current code looks like this,

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:


Grab the official SDK from http://docs.aws.amazon.com/aws-sdk-php/latest/index.html

This example uses 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
);


来源:https://stackoverflow.com/questions/22224724/uploading-posted-file-to-amazon-s3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!