Created
May 27, 2024 05:06
-
-
Save AmeerFaisalAdanan/5cd1638e839adad8b9c03410b84f5957 to your computer and use it in GitHub Desktop.
Minio Helpers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | |
]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I use existing AWS_ environment variable for this setup, so its up to you to declare new environment using MINIO_