Skip to content

Instantly share code, notes, and snippets.

@AmeerFaisalAdanan
Created May 27, 2024 05:06
Show Gist options
  • Save AmeerFaisalAdanan/5cd1638e839adad8b9c03410b84f5957 to your computer and use it in GitHub Desktop.
Save AmeerFaisalAdanan/5cd1638e839adad8b9c03410b84f5957 to your computer and use it in GitHub Desktop.
Minio Helpers
<?php
namespace App\Http\Helpers;
use Aws\S3\S3Client as S3Client;
class MinIOHandler
{
public static function createClient(){
$minio_access_key = config('filesystems.disks.s3.key');
$minio_secret_key = config('filesystems.disks.s3.secret');
$minio_endpoint = config('filesystems.disks.s3.endpoint');
$minio_region = config('filesystems.disks.s3.region');
$minio_use_path_style_endpoint = config('filesystems.disks.s3.use_path_style_endpoint');
return new S3Client([
'version' => 'latest',
'region' => $minio_region,
'endpoint' => $minio_endpoint,
'use_path_style_endpoint' => $minio_use_path_style_endpoint,
'credentials' => [
'key' => $minio_access_key,
'secret' => $minio_secret_key,
],
'http' => [
'verify' => true
],
// 'bucket' => $minio_bucket,
]);
}
public static function uploadFile($file, $object_name){
$client = self::createClient();
$result = $client->putObject([
'Bucket' => config('filesystems.disks.s3.bucket'),
'Key' => $object_name,
'Body' => fopen($file, 'r'),
'ACL' => 'public-read'
]);
return $result;
}
}
?>
public function store(Request $request)
{
$file = $request->file('file');
MinIOHandler::uploadFile($file->getPathname(), 'images/' . $file->getClientOriginalName());
// get url of the uploaded file
$url = Storage::disk('s3')->url('images/' . $file->getClientOriginalName());
return response()->json(
[
'url' => $url,
]
);
}
/**
* Display the specified resource.
*/
public function show(){
$content = Storage::disk('s3')->url('output-onlinepngtools.png');
return response()->json(
[
'content' => $content,
]);
}
@AmeerFaisalAdanan
Copy link
Author

I use existing AWS_ environment variable for this setup, so its up to you to declare new environment using MINIO_

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment