Przeglądaj źródła

评论图片上传S3

bianjunhui 14 godzin temu
rodzic
commit
bc5c00e02f

+ 26 - 10
packages/Longyi/ImageUpload/src/Services/ImageUploadService.php

@@ -7,7 +7,8 @@ use Illuminate\Support\Facades\Storage;
 use Illuminate\Support\Str;
 use Intervention\Image\ImageManager;
 use Exception;
-
+use Aws\S3\S3Client;
+use Aws\Exception\AwsException;
 class ImageUploadService
 {
     /**
@@ -28,7 +29,7 @@ class ImageUploadService
 
         // Generate directory path
         $uploadDirectory = $directory ?? config('image_upload.upload_directory', 'images/uploads');
-        $path = $uploadDirectory . '/' . date('Y/m/d');
+        $path = $uploadDirectory . '/' . date('Ym/d');
 
         // Process and store the image
         if (Str::contains($file->getMimeType(), 'image')) {
@@ -101,15 +102,32 @@ class ImageUploadService
      */
     protected function processAndStoreImage(UploadedFile $file, string $path, string $disk): array
     {
-        try {
+
             $manager = new ImageManager();
             $image = $manager->make($file)->encode('webp');
 
             $fileName = Str::random(40) . '.webp';
             $fullPath = $path . '/' . $fileName;
 
-            Storage::disk($disk)->put($fullPath, $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'    => $fullPath,
+                'Body'   => (string) $image,
+                'ContentType' => 'image/webp',
+            ]);
             return [
                 'success' => true,
                 'path' => $fullPath,
@@ -119,9 +137,7 @@ class ImageUploadService
                 'size' => $file->getSize(),
                 'disk' => $disk,
             ];
-        } catch (Exception $e) {
-            throw new Exception(__('Failed to process image: :error', ['error' => $e->getMessage()]));
-        }
+
     }
 
     /**
@@ -164,7 +180,7 @@ class ImageUploadService
     public function delete(string $path, ?string $disk = null): bool
     {
         $disk = $disk ?? config('image_upload.default_disk', 's3');
-        
+
         return Storage::disk($disk)->delete($path);
     }
 
@@ -178,7 +194,7 @@ class ImageUploadService
     public function getUrl(string $path, ?string $disk = null): string
     {
         $disk = $disk ?? config('image_upload.default_disk', 's3');
-        
+
         return Storage::disk($disk)->url($path);
     }
 }

+ 1 - 1
packages/Webkul/BagistoApi/src/State/ProductReviewProcessor.php

@@ -277,7 +277,7 @@ class ProductReviewProcessor implements ProcessorInterface
     private function saveImagesToS3(array $images, int $reviewId): array
     {
         $attachmentUrls = [];
-        $directory = "review/{$reviewId}";
+        $directory = "uploads";
 
         foreach ($images as $index => $imageData) {
             try {