git -C '/home/opc/rocketa.git' show d9de72d -- app/Http/Controllers/Admin/Traits/ImageManage.phpcommit d9de72d7d3ac56eb20c988cbaec9dbf97031adb4
Author: Satoshi Ujihara <satoshi_ujihara@fivegate.jp>
Date: Tue Oct 7 16:06:43 2025 +0900
S3からoci bucketに変更
diff --git a/app/Http/Controllers/Admin/Traits/ImageManage.php b/app/Http/Controllers/Admin/Traits/ImageManage.php
index ef406ce..78ef913 100644
--- a/app/Http/Controllers/Admin/Traits/ImageManage.php
+++ b/app/Http/Controllers/Admin/Traits/ImageManage.php
@@ -7,31 +7,51 @@ namespace App\Http\Controllers\Admin\Traits;
use Intervention\Image\Facades\Image;
use Illuminate\Support\Facades\Storage;
+use Oracle\Oci\ObjectStorage\ObjectStorageClient;
+use Oracle\Oci\Common\Auth\ConfigFileAuthProvider;
trait ImageManage
{
public function uploadImage($image, $destinationPath, $image_name, $extension, $content_type)
{
- // if (!\File::exists(public_path().$destinationPath)) {
- // \File::makeDirectory(public_path().$destinationPath, 0777, true);
- // }
- // $image->move(public_path().$destinationPath, $image_name);
- // $image_path = $destinationPath.$image_name;
+ $bucket_name = "rocket-a-mvp";
+ $auth_provider = new ConfigFileAuthProvider();
+ $c = new ObjectStorageClient($auth_provider);
+ $response = $c->getNamespace();
+ $namespace = $response->getJson();
+ // $file_handle = fopen($image, "rb");
+ $file_handle = file_get_contents($image);
+ // $img_obj = \Image::make($image)->encode($extension);
- //$extension = $image->getClientOriginalExtension();
+ // ライブラリのバグで$content_typeが反映されないので、コマンドラインから実行
+ // $objectName = $destinationPath."/".$image_name;
+ // $response = $c->putObject([
+ // 'namespaceName' => $namespace,
+ // 'bucketName' => $bucket_name,
+ // 'objectName' => $objectName,
+ // 'putObjectBody' => $file_handle,
+ // 'contentType' => $content_type,
+ // 'opcMeta' => [
+ // 'content-type' => $content_type,
+ // ]]
+ // );
+ $objectName2 = $destinationPath.'/'.$image_name;
+ $cmd = "/home/alma/bin/oci os object put -ns $namespace -bn $bucket_name --name '$objectName2' --file '$image' --content-type '$content_type' --force";
+ // echo $cmd;
+ exec($cmd);
- $img_obj = \Image::make($image)->encode($extension);
+ // $path = Storage::disk('s3')
+ // ->getDriver()
+ // ->put($destinationPath.'/'.$image_name,
+ // (string)$img_obj,
+ // [
+ // //'visibility' => 'public',
+ // 'ContentType' => $content_type,
+ // ]
+ // );
+ // $image_path = config('filesystems.disks.s3.image_domain').$destinationPath.'/'.$image_name;
+ $image_path = env('AWS_IMAGE_DOMAIN') . "/" . $destinationPath.'/'.$image_name;
- $path = Storage::disk('s3')
- ->getDriver()
- ->put($destinationPath.'/'.$image_name,
- (string)$img_obj,
- [
- //'visibility' => 'public',
- 'ContentType' => $content_type,
- ]
- );
- $image_path = config('filesystems.disks.s3.image_domain').$destinationPath.'/'.$image_name;
return $image_path;
}
@@ -40,12 +60,7 @@ trait ImageManage
{
$images = collect();
- // if (\File::exists(public_path().$image_path)) {
- // $files = \File::files(public_path().$image_path);
- // foreach($files as $file) {
- // $images->push($image_path.basename($file));
- // }
- // }
+
$files = Storage::disk('s3')->files($image_path);
foreach($files as $file) {
$images->push($file);
@@ -56,8 +71,20 @@ trait ImageManage
private function deleteImage($image_url)
{
- $image_path = $this->getAwsImagePath($image_url);
- Storage::disk('s3')->delete($image_path);
+ // $image_path = $this->getAwsImagePath($image_url);
+ // Storage::disk('s3')->delete($image_path);
+ $bucket_name = "rocket-a-mvp";
+ $auth_provider = new ConfigFileAuthProvider();
+ $c = new ObjectStorageClient($auth_provider);
+ $response = $c->getNamespace();
+ $namespace = $response->getJson();
+
+ $c->deleteObject([
+ 'namespaceName' => $namespace,
+ 'bucketName' => $bucket_name,
+ 'objectName' => $image_url
+ ]);
+
}
private function getAwsImagePath($image_url)