-->

Symfony的2 - 最佳实践上传在Amazon S3上的图像(Symfony 2 - Best

2019-08-16 22:45发布

我有,我有一个表单file字段上传的图像。 我需要上传在Amazon S3这一形象。 建设一步一步来,我开始上传本地磁盘上的形象和它现在的工作。

上载在我的实体存在的Page ,因为它是建议测试上传成功之前保存的实体。 我结束了此块的代码

    /**
     * @ORM\Column(name="objectThumbnail", type="string", length=255, nullable=true)
     */
    protected $objectThumbnail;

    /**
     * This is not a column in the database but it's mapping a field from the form
     *
     * @Assert\File(maxSize="600000000")
     */
    public $file;

    ...

    /**
     * @ORM\PrePersist()
     * @ORM\PreUpdate()
     */
    public function preUpload()
    {
        if (null !== $this->file) {
            // generate a unique filename
            $this->objectThumbnail = $this->getGuid().'.'.$this->file->guessExtension();
        }
    }

    /**
     * @ORM\PostPersist()
     * @ORM\PostUpdate()
     */
    public function upload()
    {
        if (null === $this->file) {
            return;
        }

        // if there is an error when moving the file, an exception will
        // be automatically thrown by move(). This will properly prevent
        // the entity from being persisted to the database on error
        $this->file->move($this->getUploadRootDir(), $this->objectThumbnail);

        unset($this->file);
    }

    /**
     * @ORM\PostRemove()
     */
    public function removeUpload()
    {
        if ($file = $this->getAbsolutePath()) {
            unlink($file);
        }
    }

这是完美的,它的工作就像一个魅力。 只是Symfony的(2.1.7)的尖叫,因为公众范围的file属性,没有重大。

现在我需要的S3层整合。 对于我想我会使用GaufretteStreamWrapper

现在我在努力寻找什么是做到这一点的最好办法。 如何获得访问Filesystem中的实体服务? 是不是真的干净做这样的吗? 这感觉有点别扭给我处理在实体上S3图像的上传。

你会如何建议办呢?

干杯,

马克西姆

Answer 1:

在实体使用的服务是不是一个很好的实践。 我要推荐创建在这个例子中进行验证,持久性,并上传表单处理程序

我写符合您需求的例子

//...
//inside a creation controller action
//...
//create a new page entity
$page = new Page();

//get your page form class
$form = $this->createForm(new PageForm(), $page);

//call your form handler
$formHandler = new PageFormHandler(
        $form,
        $this->get('request'),
        $this->get('your.gaufrette.filesystem'),
        $this->get('your.page.manager')
);

//form is valid ?
if ($formHandler->process()) {         
    //flash message, redirection etc
}

处理程序类

namespace YourCompagnyBundle\Form\Handler;

use YourCompagnyBundle\Entity\Page;
use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Gaufrette\Filesystem;


class PageFormHandler
{
    protected $form;
    protected $request;
    protected $pageManager;
    protected $filesystem;

    public function __construct(Form $form, Request $request, Filesystem $filesystem, $pageManager)
    {
        $this->form    = $form;
        $this->request = $request;
        $this->filesystem = $filesystem;
        $this->pageManager = $pageManager;
    }

    public function process()
    {
        if( $this->request->getMethod() == 'POST' )
        {
            $this->form->bind($this->request);

            if( $this->form->isValid() )
            {
               $this->onSuccess($this->form->getData());               

                return true;
            }
        }

        return false;
    }

    function onSuccess(Page $page)
    {
        //has uploaded file ?
        if($page->getFile() instanceof UploadedFile){
            //do some logic with gaufrette filesystem
           //if page is already managed in database (update), delete previous file


        }
        //storage
        $this->pageManager->save($page);
    }

}

希望这有助于



Answer 2:

也许你应该检查VichUploaderBundle基本上注入File对象到您的entites与Gaufrette集成是很容易的。



文章来源: Symfony 2 - Best practice to upload an image on Amazon S3