이미지를 업로드 할 때 file
필드가있는 양식이 있습니다. 이 이미지를 Amazon S3에 업로드해야합니다. 이 단계를 단계별로 작성하여 로컬 디스크에 이미지를 업로드하기 시작했으며 현재 작동 중입니다.Symfony 2 - 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 레이어를 통합해야합니다. 그 때문에 나는 Gaufrette
과 StreamWrapper
을 사용할 것입니다.
이제 가장 좋은 방법은 무엇인지 찾아내는 데 어려움을 겪고 있습니다. 엔티티의 Filesystem
서비스에 대한 액세스 권한을 얻으려면 어떻게해야합니까? 이런 식으로하는 것이 정말 깨끗한가요? Entity에서 S3의 이미지 업로드를 처리하는 것이 다소 어색하다고 느낍니다.
어떻게 하시겠습니까? 당신의 기업에 서비스를 사용
건배,
막심
안녕하세요 @ maxwell2022,이 접근법을 통해이 문제를 해결하셨습니까? Gaufrette 번들 또는 Cybernox/AmazonWebServicesBundle을 사용 했습니까? 좋은 질문에 대한 Tks ... –
예, 저는 Gaufrette를 사용했습니다. 대답의 코멘트를 읽고, 자세한 내용이 있습니다. – maxwell2022