فهرست منبع

产品图片上传到S3

bianjunhui 15 ساعت پیش
والد
کامیت
fc45ff1b34
1فایلهای تغییر یافته به همراه22 افزوده شده و 2 حذف شده
  1. 22 2
      packages/Webkul/Product/src/Repositories/ProductMediaRepository.php

+ 22 - 2
packages/Webkul/Product/src/Repositories/ProductMediaRepository.php

@@ -2,6 +2,7 @@
 
 
 namespace Webkul\Product\Repositories;
 namespace Webkul\Product\Repositories;
 
 
+use Aws\S3\S3Client;
 use Exception;
 use Exception;
 use Illuminate\Http\UploadedFile;
 use Illuminate\Http\UploadedFile;
 use Illuminate\Support\Facades\Storage;
 use Illuminate\Support\Facades\Storage;
@@ -33,7 +34,7 @@ class ProductMediaRepository extends Repository
      */
      */
     public function getProductDirectory($product): string
     public function getProductDirectory($product): string
     {
     {
-        return 'product/'.$product->id;
+        return 'uploads/'.date('Ym/d');
     }
     }
 
 
     /**
     /**
@@ -60,8 +61,27 @@ class ProductMediaRepository extends Repository
                         $image = $manager->make($file)->encode('webp');
                         $image = $manager->make($file)->encode('webp');
 
 
                         $path = $this->getProductDirectory($product).'/'.Str::random(40).'.webp';
                         $path = $this->getProductDirectory($product).'/'.Str::random(40).'.webp';
+                        //Storage::put($path, $image);
+                        // 方式一:使用原生 AWS SDK 绕过 Laravel Storage
+                        $s3Client = new S3Client([
+                            'version' => 'latest',
+                            'region'  => env('AWS_DEFAULT_REGION'),
+                            'credentials' => [
+                                'key'    => env('AWS_ACCESS_KEY_ID'),
+                                'secret' => env('AWS_SECRET_ACCESS_KEY'),
+                            ],
+                            'http'    => [
+                                'verify' => false,  // 临时禁用 SSL 验证用于测试
+                            ],
+                        ]);
+
+                        $result = $s3Client->putObject([
+                            'Bucket' => env('AWS_BUCKET'),
+                            'Key'    => $path,
+                            'Body'   => (string) $image,
+                            'ContentType' => 'image/webp',
+                        ]);
 
 
-                        Storage::put($path, $image);
                     } else {
                     } else {
                         $path = $file->store($this->getProductDirectory($product));
                         $path = $file->store($this->getProductDirectory($product));
                     }
                     }