Vich Bundle-Symfony 4.4-apipplatform-Flysystem-ovhs3对象存储区-guessMimeType创建和更新时PHP异常 维希版本:1.15.0 Symfony版本:4.4.13 PHP版本:7.4 API平台版本:2.5.7 flysystem捆绑包版本:1.5.0 flysystem-aws-s3-v3版本:1.0.28 总结

Vich Bundle-Symfony 4.4-apipplatform-Flysystem-ovhs3对象存储区-guessMimeType创建和更新时PHP异常 维希版本:1.15.0 Symfony版本:4.4.13 PHP版本:7.4 API平台版本:2.5.7 flysystem捆绑包版本:1.5.0 flysystem-aws-s3-v3版本:1.0.28 总结,php,symfony4,ovh,vichuploaderbundle,flysystem,Php,Symfony4,Ovh,Vichuploaderbundle,Flysystem,在远程存储库中创建或更新包含文件的对象时,会发生错误,因为当文件不在服务器上时,该对象试图读取文件的MimeType及其路径。使用本地存储时,我没有收到任何错误 错误: request.CRITICAL:uncought PHP异常Symfony\Component\Mime\Exception\InvalidArgumentException:“5F5B6D18C6671161617604.png”文件不存在或不可读。“at/var/www/html/vendor/Symfony/Mime/f

在远程存储库中创建或更新包含文件的对象时,会发生错误,因为当文件不在服务器上时,该对象试图读取文件的MimeType及其路径。使用本地存储时,我没有收到任何错误

错误:

request.CRITICAL:uncought PHP异常Symfony\Component\Mime\Exception\InvalidArgumentException:“5F5B6D18C6671161617604.png”文件不存在或不可读。“at/var/www/html/vendor/Symfony/Mime/fileinfomimetypeeguseer.PHP行50{“Exception”:“[object](Symfony\Component\Mime\Exception\InvalidArgumentException(代码:0):The”5f5b6d18c6671164117604.png“文件不存在或不可读。at/var/www/html/vendor/symfony/mime/fileinfomimetypegueser.php:50)”}[]

我知道该文件已发送到存储库,因为它可以通过GET请求进行恢复

如何繁殖 flysystem.yaml

flysystem:
存储:
uploads.storage.ovh:
适配器:“aws”
选项:
客户端:“Aws\S3\S3客户端”
桶:“%env(OVH_桶)%”
uploads.storage.local:
适配器:“本地”
选项:
目录:“%kernel.project\u dir%/var/storage/uploads”
上载.storage.memory:
适配器:“内存”
#与env切换
上载.存储:
适配器:“懒惰”
选项:
源:“%env(应用程序上传源)%”
vich_上传器。yaml

vich_上传器:
db_驱动程序:orm
存储:flysystem
元数据:
自动检测:真
映射:
用户头像:
上传目的地:uploads.storage
姓名:
服务:Vich\UploaderBundle\Naming\UniqidNamer
删除时删除:true#确定删除实体后是否删除文件
更新时删除:true#在实体更新时确定是否删除文件
加载时注入:false
ProductionCenter徽标:
上传目的地:uploads.storage
姓名:
服务:Vich\UploaderBundle\Naming\UniqidNamer
删除上的删除:真
在更新时删除:true
加载时注入:false
Donneurdrerelogo:
上传目的地:uploads.storage
姓名:
服务:Vich\UploaderBundle\Naming\UniqidNamer
删除上的删除:真
在更新时删除:true
加载时注入:false
服务。yaml

服务:
Aws\S3\S3客户端:
论据:
-版本:'最新'
资格证书:
密钥:'%env(OVH\u访问密钥\u ID)%'
机密:'%env(OVH\u secret\u ACCESS\u KEY)%'
端点:“%env(OVH_端点)%”
地区:'%env(OVH_地区)%'
S3:
版本:'2006-03-01'
端点url:“%env(OVH\u端点)%”
签名版本:“s3v4”
寻址方式:“虚拟”
S3API:
端点url:“%env(OVH\u端点)%”
一个实体的示例:

<?php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Mapping\Annotation as Vich;

/**
 * @ORM\Entity(repositoryClass="App\Repository\UserAvatarRepository")
 * @ApiResource()
 * @Vich\Uploadable
 * @UniqueEntity(fields={"user"}, message="unique.userAvatar.user")
 */
class UserAvatar extends FileUpload
{
    /**
     * @Assert\File(
     *     maxSize = "1980k",
     *     mimeTypes = {
     *      "image/jpeg",
     *      "image/png",
     *      "image/svg+xml",
     *      }
     * )
     * @Assert\NotNull()
     *
     * @var File|null
     * @Groups({
     *     "file",
     *     })
     * @Vich\UploadableField(
     *     mapping="userAvatar",
     *     fileNameProperty="filePath",
     *     size="imageSize",
     *     originalName="originalName",
     *     mimeType="mimeType",
     *     dimensions="dimensions",
     * )
     */
    public $imageFile;

    /**
     * @ORM\OneToOne(targetEntity=User::class, inversedBy="avatar")
     * @ORM\JoinColumn(nullable=false, unique=true)
     */
    private User $user;

    public function getUser(): User
    {
        return $this->user;
    }

    public function setUser(User $user): self
    {
        $this->user = $user;

        return $this;
    }

    /**
     * If manually uploading a file (i.e. not using Symfony Form) ensure an instance
     * of 'UploadedFile' is injected into this setter to trigger the update. If this
     * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
     * must be able to accept an instance of 'File' as the bundle will inject one here
     * during Doctrine hydration.
     *
     * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile|null $imageFile
     */
    public function setImageFile($imageFile = null): void
    {
        $this->imageFile = $imageFile;
        if (null !== $imageFile) {
            // It is required that at least one field changes if you are using doctrine
            // otherwise the event listeners won't be called and the file is lost
            $this->setUpdatedAt(new \DateTime());
        }
    }

    public function getImageFile(): ?File
    {
        return $this->imageFile;
    }
}

我发现了问题。将非恶性化组指定给对象并排除imageFile属性。
<?php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Entity\Traits\UpdatedAtTrait;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * @ORM\Entity(repositoryClass="App\Repository\FileUploadRepository")
 * @ApiResource()
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="type", type="string")
 * @ORM\DiscriminatorMap({
 *     "userAvatar" = "App\Entity\UserAvatar",
 *     "productionCenterLogo" = "App\Entity\ProductionCenterLogo",
 *     "donneurOrdreLogo" = "App\Entity\DonneurOrdreLogo",
 * })
 */
abstract class FileUpload
{
    use UpdatedAtTrait;
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     * @ApiProperty(identifier=true)
     */
    private int $id;

    /**
     * @ORM\Column(type="string")
     *
     * @var string|null
     * @Groups({
     *     "file",
     *     "user_read",
     *     "production_center_read",
     *     "donneur_ordre_read",
     *     })     */
    private $filePath;

    /**
     * @ORM\Column(type="string")
     *
     * @var string|null
     * @Groups({
     *     "file",
     *     "user_read",
     *     "production_center_read",
     *     "donneur_ordre_read",
     *     })     */
    private $originalName;

    /**
     * @ORM\Column(type="string")
     *
     * @var string|null
     * @Groups({
     *     "file",
     *     "user_read",
     *     "production_center_read",
     *     "donneur_ordre_read",
     *     })     */
    private $mimeType;

    /**
     * @ORM\Column(type="json", nullable=true)
     *
     * @var array|null
     * @Groups({
     *     "file",
     *     "user_read",
     *     "production_center_read",
     *     "donneur_ordre_read",
     *     })     */
    private $dimensions;

    /**
     * @ORM\Column(type="integer", nullable=true)
     *
     * @var int|null
     * @Groups({
     *     "file",
     *     "user_read",
     *     "production_center_read",
     *     "donneur_ordre_read",
     *     })     */
    private $imageSize;

    public function setFilePath(?string $filePath): self
    {
        $this->filePath = $filePath;

        return $this;
    }

    public function getFilePath(): ?string
    {
        return $this->filePath;
    }

    public function setImageSize(?int $imageSize): self
    {
        $this->imageSize = $imageSize;

        return $this;
    }

    public function getImageSize(): ?int
    {
        return $this->imageSize;
    }

    public function getId(): int
    {
        return $this->id;
    }

    public function setId(int $id): self
    {
        $this->id = $id;

        return $this;
    }

    public function getOriginalName(): ?string
    {
        return $this->originalName;
    }

    public function setOriginalName(?string $originalName): self
    {
        $this->originalName = $originalName;

        return $this;
    }

    public function getMimeType(): ?string
    {
        return $this->mimeType;
    }

    public function setMimeType(?string $mimeType): self
    {
        $this->mimeType = $mimeType;

        return $this;
    }

    public function getDimensions(): ?array
    {
        return $this->dimensions;
    }

    public function setDimensions(?array $dimensions): self
    {
        $this->dimensions = $dimensions;

        return $this;
    }
}