Home > OS >  Issue uploading public images to AWS S3 Laravel 9
Issue uploading public images to AWS S3 Laravel 9

Time:01-28

I have searched widely for this problem but to no avail. Here's the code that's giving me the issue.

 if ($request->hasFile("images")) {
    foreach ($request->file("images") as $i => $image) {
        $path = $image->storePubliclyAs($product->id, "image_$i.{$image->extension()}");

    }
}

The line with $image->storePubliclyAs() is raising the error:

InvalidArgumentException: Found 1 error while validating the input provided for the GetObject operation:
[Key] expected string length to be >= 1, but found string length of 0 in file /var/www/html/vendor/aws/aws-sdk-php/src/Api/Validator.php on line 65

I should note that I've already indicated in the .env file that the filesystem disk should be pointed to S3. The bucket's objects are also entirely public. This is quite confusing as $image->storeAs() works perfectly fine.

CodePudding user response:

Found a workaround. Instead of using the storePubliclyAs() method, just use storeAs() and Storage::setVisibility() respectively:

if ($request->hasFile("images")) {
    foreach ($request->file("images") as $i => $image) {
        $path = $image->storeAs($product->id, "image_$i.{$image->extension()}");
        Storage::setVisibility($path, "public");
    }
}

Kind of weird but it's been the only way I could get it to work.

CodePudding user response:

Can you try this code below. I just changed how you are using $i in giving the image a name.

 if ($request->hasFile("images")) {
    foreach ($request->file("images") as $i => $image) {
        $path = $image->storePubliclyAs($product->id, "image_".$i.{$image->extension()}");

    }
}
  • Related