dream02008 2016-01-03 15:13
浏览 141
已采纳

使用Laravel 5.2上传到S3

Having an issue with the s3 driver for Laravel 5.2. The error i'm getting is this:

  1. Found 1 error while validating the input provided for the HeadObject operation:
  2. [Key] must be at least 1 characters long. Value provided is 0 characters long.

I'm using the league flysystem V3 as stated in Laravel docs. When I follow the stack track and start dumping out the vars the 'Key' value is always empty but i've set it all up in my config file.

Here are the top lines from my stack trace

  1. in Validator.php line 38
  2. at Validator->validate('HeadObject', object(StructureShape), array('Bucket' => 'monstervsl', 'Key' => '', '@http' => array())) in Middleware.php line 77
  3. at Middleware::Aws\{closure}(object(Command), null) in S3Client.php line 710
  4. at S3Client::Aws\S3\{closure}(object(Command), null) in S3Client.php line 729
  5. at S3Client::Aws\S3\{closure}(object(Command), null) in Middleware.php line 53
  6. at Middleware::Aws\{closure}(object(Command), null) in SSECMiddleware.php line 59
  7. at SSECMiddleware->__invoke(object(Command)) in AwsClient.php line 208

As you can see it's getting the bucket from my config but not the key it's empty.

Here is my filesystem.php file

  1. 's3' => [
  2. 'driver' => 's3',
  3. // 'key' => env('S3_KEY'),
  4. // 'secret' => env('S3_SECRET'),
  5. 'key' => '8tfnxo8abgn7voaex8rgv', // <- Not my real key
  6. 'secret' => 'aw7btx49wXNF7AGWV', // <- not my real secret
  7. 'region' => 'eu-west-1',
  8. 'bucket' => 'monstervsl',
  9. ],

Here is my controller, it's fairly straight forward, I don't think the put contents stuff is relevant but added it anyway

  1. // Write the contents to a new file on disk
  2. $view = view('iframe')->with('json', $video->toJson());
  3. $contents = $view->render();
  4. $token = '12345';
  5. $filePath = public_path() .'/iframes/' . $token . '.html';
  6. file_put_contents($filePath, $contents);
  7. Storage::disk('s3')->put('/', file_get_contents($filePath));

展开全部

  • 写回答

1条回答 默认 最新

  • dongmeijian1716 2016-01-03 23:03
    关注

    You have to give the destination file path/name where you want to save this file on S3 bucket as the first argument of put function. Currently you are trying to save the file at the root of bucket without any name. Try something like this:

    Storage::disk('s3')->put('filename.html', file_get_contents($filePath));
    

    This full path of the file is the Key in this context & that is what's missing in your original request.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部