简体   繁体   English

将公共图像上传到 AWS S3 时出现问题 Laravel 9

[英]Issue uploading public images to AWS S3 Laravel 9

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: $image->storePubliclyAs()行引发错误:

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.我应该注意到我已经在.env文件中指出文件系统磁盘应该指向 S3。 The bucket's objects are also entirely public.存储桶的对象也是完全公开的。 This is quite confusing as $image->storeAs() works perfectly fine.这非常令人困惑,因为$image->storeAs()工作得很好。

Found a workaround.找到了解决方法。 Instead of using the storePubliclyAs() method, just use storeAs() and Storage::setVisibility() respectively:不要使用storePubliclyAs()方法,只需分别使用storeAs()Storage::setVisibility()

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.有点奇怪,但这是我让它工作的唯一方法。

Can you try this code below.你能试试下面的这段代码吗? I just changed how you are using $i in giving the image a name.我只是更改了您使用 $i 为图像命名的方式。

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

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM