AWS S3 Fix (Code Provided) Method: Signed URLs with Caveat
I previously posted a STRONG recommendation on Amazon S3 non-public access for MagicAI storage. Some countries MAY consider AI-generated images private only because the user considers them their property. Also, if you have AWS Control Tower in your master account, there is no way to enable public access on an S3 bucket as security best practices. Otherwise, I understand this is not a requirement. However, to keep the S3 bucket private, one option is to use signed URLs: In ~/app/Http/Controllers/AIController.php, around line 724, find: $path = Storage::disk('s3')->url($aws_path); Replace (or comment out) this line with: // Generate signed URL $expiration = Carbon::now()->addMinutes(10); // Set expiration as per your need $path = Storage::disk('s3')->temporaryUrl($aws_path, $expiration); Add use Carbon\Carbon; to the top list of namespace imports, or use statements. Save. Remaining MagicAI instructions work: https://magicaidocs.liquid-themes.com/aws-s3-storage-setup/ You will now be able to create images, with signed URLs, that are visible and downloadable from a non-public S3 buckets. Works flawlessly. WARNING: The maximum time a signed URL can exist is 7 days (10,080 minutes). Even if you try to put more than 7 days, it will automatically throttle back to 7 days (10,080 minutes), due to the secure nature of a signed URL; it's considered a temporary, secure credential. Afterwards, the image will not be accessible (placeholder, but no image is visible). Alternatives: Add CloudFront to the MagicAI roadmap. It generates signed URLs for up to 1 year. Add Cognito to the MagicAI roadmap. It also generates signed URLs for up to 1 year. Request: If you use this method, we should also let the user know that the image will not be accessible after the time you choose in addMinutes. TEMPORARY front-end notification workaround: At line ~756, where it says: return response()->json(["status" => "success", "images" => $entries, "imagestorage" => $imagestorage]); Modify the return response to add an expiration notice, such as: $expiration_notice = "Note: The image will be inaccessible after 7 days and will be deleted. Download and save to your local drive today."; return response()->json(["status" => "success", "images" => $entries, "imagestorage" => $imagestorage, "expirationnotice" => $expirationnotice]); Final Thoughts: This needs to displayed on the front end, so some code changes there. lastly, once inaccessible, the 'missing image' box looks 'unprofessional' and ugly. maybe it should be deleted altogether. Hope this helps. Ron
