Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel secure Amazon s3 bucket files

I am using Amazon s3 but here i am facing two problems

1.I can't directly upload files to amazon server when i submit form.I mean i have to upload images to an upload folder on my PHP server and from there i have to retrieve and upload them to s3 server. Is there a way to upload images directly to s3 when we click on submit?

2.If i pass 'public' in s3 put object then i can access or view files, but if i make it public every one can view files. But i need to protect all files and view only to the authenticated user. Can any one suggest me how to fix this issue?

try {           
    $s3 = \Storage::disk('s3');
    $s3->put($strFileName, file_get_contents($img_path.$strFileName), 'public');
} catch (Aws\Exception\S3Exception $e) {
    echo "There was an error uploading the file.\n"+$e;
}

Before asking questions i have read many answers from stackoverflow but it didnt helped me to fix my issue. Thanks.

like image 628
Vision Coderz Avatar asked Oct 21 '16 04:10

Vision Coderz


1 Answers

For you first question, it can be directly upload images to AWS S3.

$s3 = \Storage::disk('s3')->getDriver();
$s3->put($filePath, file_get_contents($file), array('ContentDisposition' => 'attachment; filename=' . $file_name . '', 'ACL' => 'public-read'));

You are supposed to specify your file path and the file you get from the form.

like image 105
Anandhan Avatar answered Oct 17 '22 08:10

Anandhan