Ad

Uploading To S3 With Laravel 5.2

- 1 answer

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

Found 1 error while validating the input provided for the HeadObject operation:
[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

in Validator.php line 38
at Validator->validate('HeadObject', object(StructureShape), array('Bucket' => 'monstervsl', 'Key' => '', '@http' => array())) in Middleware.php line 77
at Middleware::Aws\{closure}(object(Command), null) in S3Client.php line 710
at S3Client::Aws\S3\{closure}(object(Command), null) in S3Client.php line 729
at S3Client::Aws\S3\{closure}(object(Command), null) in Middleware.php line 53
at Middleware::Aws\{closure}(object(Command), null) in SSECMiddleware.php line 59
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

        's3' => [
            'driver' => 's3',
//            'key'    => env('S3_KEY'),
//            'secret' => env('S3_SECRET'),
            'key' => '8tfnxo8abgn7voaex8rgv', // <- Not my real key 
            'secret' => 'aw7btx49wXNF7AGWV', //  <- not my real secret
            'region' => 'eu-west-1',
            'bucket' => 'monstervsl',
        ],

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

// Write the contents to a new file on disk
$view = view('iframe')->with('json', $video->toJson());
$contents = $view->render();
$token = '12345';
$filePath = public_path() .'/iframes/' . $token . '.html';
file_put_contents($filePath, $contents);

Storage::disk('s3')->put('/', file_get_contents($filePath));
Ad

Answer

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.

Ad
source: stackoverflow.com
Ad