Vich软件包-Symfony 4.4-ApiPlatform-Flysystem-OVH S3对象存储-guessMimeType PHP在创建和更新时发生异常 摘要如何复制

问题描述

  • 版本:1.15.0
  • Symfony版本:4.4.13
  • PHP版本:7.4
  • API平台版本:2.5.7
  • flysystem-bundle版本:1.5.0
  • flysystem-aws-s3-v3版本:1.0.28

摘要

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

错误:

request.CRITICAL:未捕获的PHP异常Symfony \ Component \ Mime \ Exception \ InvalidArgumentException:“ 5f5b6d18c6671164117604.png”文件不存在或不可读。在/var/www/html/vendor/symfony/mime/FileinfoMimeTypeGuesser.php行50 {“ exception”:“ [object](Symfony \ Component \ Mime \ Exception \ InvalidArgumentException(code:0):” 5f5b6d18c6671164117604.png“文件不存在或不可读。请访问/var/www/html/vendor/symfony/mime/FileinfoMimeTypeGuesser.php:50)“} []

我知道文件已发送到存储库,因为可以使用GET请求将其恢复。

如何复制

flysystem.yaml

flysystem:
    storages:
        uploads.storage.ovh:
            adapter: "aws"
            options:
                client: "Aws\S3\S3Client"
                bucket: "%env(OVH_BUCKET)%"
        uploads.storage.local:
            adapter: 'local'
            options:
                directory: "%kernel.project_dir%/var/storage/uploads"

        uploads.storage.memory:
            adapter: "memory"
# switch with env
        uploads.storage:
            adapter: "lazy"
            options:
                source: "%env(APP_UPLOADS_SOURCE)%"

vich_uploader.yaml

vich_uploader:
    db_driver: orm
    storage: flysystem
    metadata:
        auto_detection: true

    mappings:
        userAvatar:
            upload_destination: uploads.storage
            namer:
                service: Vich\UploaderBundle\Naming\UniqidNamer
            delete_on_remove: true  # determine whether to delete file upon removal of entity
            delete_on_update: true  # determine wheter to delete the file upon update of entity
            inject_on_load: false

        productionCenterLogo:
            upload_destination: uploads.storage
            namer:
                service: Vich\UploaderBundle\Naming\UniqidNamer
            delete_on_remove: true
            delete_on_update: true
            inject_on_load: false

        donneurOrdreLogo:
            upload_destination: uploads.storage
            namer:
                service: Vich\UploaderBundle\Naming\UniqidNamer
            delete_on_remove: true
            delete_on_update: true
            inject_on_load: false

service.yaml

services:
  Aws\S3\S3Client:
    arguments:
      - version: 'latest'
        credentials:
          key: '%env(OVH_ACCESS_KEY_ID)%'
          secret: '%env(OVH_SECRET_ACCESS_KEY)%'
        endpoint: '%env(OVH_ENDPOINT)%'
        region: '%env(OVH_REGION)%'
        S3:
          version: '2006-03-01'
          endpoint_url: '%env(OVH_ENDPOINT)%'
          signature_version: 's3v4'
          addressing_style: 'virtual'
        S3API:
          endpoint_url: '%env(OVH_ENDPOINT)%'

一个实体的示例:

<?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;
    }
}
<?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",*     })     */
    private $originalName;

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

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

    /**
     * @ORM\Column(type="integer",nullable=true)
     *
     * @var int|null
     * @Groups({
     *     "file",*     })     */
    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;
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...