gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:05:54 25 4
gpt4 key购买 nike

我有一个表单,其中有一个用于上传图像的 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 属性的公共(public)范围而尖叫,没什么大不了的。

现在我需要集成S3层。为此,我想我将使用 GaufretteStreamWrapper

现在我正在努力寻找最好的方法。如何访问实体中的 Filesystem 服务?这样做真的很干净吗?在Entity中处理S3上图片的上传,我感觉有点别扭。

您建议如何做?

干杯,

马克西姆

最佳答案

在您的实体中使用服务不是一个好习惯。在这个例子中,我建议创建一个表单处理程序来执行验证、持久化和上传

我写了一个符合你需求的例子

//...
//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);
}

}

希望对你有帮助

关于php - Symfony 2 - 在 Amazon S3 上上传图像的最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14847154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com