首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Gaufrette和Symfony 3.0

如何使用Gaufrette和Symfony 3.0
EN

Stack Overflow用户
提问于 2016-02-28 00:09:39
回答 2查看 4K关注 0票数 2

我在弄清楚如何在Gaufete中使用Symfony 3.0以便上传到s3存储桶时遇到了一个问题。

根据文档:https://github.com/KnpLabs/KnpGaufretteBundle

我已经设置了config.yml:

代码语言:javascript
复制
knp_gaufrette:
    adapters:
        photostorage:
            amazon_s3:
                amazon_s3_id:   amazonS3
                bucket_name:    '%save_location%'
                options:
                    directory:  'symphotest'

和services.yml:

代码语言:javascript
复制
services:
    amazonS3:
         class: Aws\S3\S3Client
        factory_class: Aws\S3\S3Client
        factory_method: 'factory'
        arguments:
            key: %amazon_s3.key%
            secret: %amazon_s3.secret%
            region: %amazon_s3.region%

因为我想使用自定义的环境变量,所以我给它传递了一个文件params.php:

代码语言:javascript
复制
  $container->setParameter('save_type','s3');
  $container->setParameter('save_location',getenv('BUCKET'));
  $container->setParameter('aws_key',getenv('S3_ACCESS'));
  $container->setParameter('aws_secret_key',getenv('S3_SECRET'));

我将其包含在config.yml顶部的位置:

代码语言:javascript
复制
imports:
    - { resource: params.php }
    - { resource: security.yml }
    - { resource: services.yml }

我创建了一个名为Images.php的实体:

代码语言:javascript
复制
<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints as Assert;

use Gaufrette\Adapter\AwsS3 as AwsS3Adapter;
use Gaufrette\Filesystem;

/**
* @ORM\Entity
* @ORM\Table(name="images")
* @ORM\HasLifecycleCallbacks
*/
class Images
{
  /**
   * @ORM\Column(type="string", length=60)
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="CUSTOM")
   * @ORM\CustomIdGenerator(class="AppBundle\Doctrine\AutoIdGenerate")
   */
  private $id;

  /**
  * @ORM\Column(type="string", length=100)
  */
  private $name;

  /**
  * @ORM\Column(type="string", length=100)
  */
  private $name_small;

  /**
  * @ORM\ManyToOne(targetEntity="AppBundle\Entity\ImageGroups", inversedBy="images")
  */
  private $users;

  /**
  * @Assert\File(maxSize="6000000")
  */
  private $file;

  private $tmp;
  private $path;

  public function getFile()
  {
    return $file;
  }

  public function setFile(UploadedFile $file = null)
  {
    $this->file=$file;

  };


  public function __construct()
  {
    //IDK what to do here
  }

  /**
    * @ORM\PrePersist()
    * @ORM\PreUpdate()
    */
   public function preUpload()
   {
       if (null !== $this->getFile())
       {
           $filename = sha1(uniqid(gethostname(), true));
           $this->name = $filename.'.'.$this->getFile()->guessExtension();
           $this->$name_small='small'.$filename.'.'.$this->getFile()->guessExtension();
       }
   }

   /**
    * @ORM\PostPersist()
    * @ORM\PostUpdate()
    */
   public function upload()
   {
       if (null === $this->getFile())
       {
           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->getFile()->move($this->getUploadRootDir(), $this->path);

       // check if we have an old image
       if (isset($this->temp))
       {
           // delete the old image
           unlink($this->getUploadRootDir().'/'.$this->temp);
           // clear the temp image path
           $this->temp = null;
       }
       $this->file = null;
   }

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


  /**
   * Get id
   *
   * @return string
   */
  public function getId()
  {
    return $this->id;
  }


  /**
   * Get name
   *
   * @return string
   */
  public function getName()
  {
      return $this->name;
  }

  /**
   * Get nameSmall
   *
   * @return string
   */
  public function getNameSmall()
  {
      return $this->name_small;
  }
}

但是我不知道如何获得S3适配器和客户端,因为在本例中:https://github.com/KnpLabs/Gaufrette/blob/master/doc/adapters/awsS3.md

在中启动s3客户端和文件系统。但是在Symfony 3.0上,我已经在config.yml上配置了它们。因此,一定有另一种方法来获得它们。

我只想要一个用法的例子。

EN

回答 2

Stack Overflow用户

发布于 2016-02-28 02:09:48

我推荐你阅读这篇文章:https://blog.fortrabbit.com/new-app-cloud-storage-s3

Is是一个快速入门指南,它讲述了为什么可以使用分散存储,并涵盖了以下主题:

  • 如何注册AWS帐户
  • 如何创建第一个S3存储桶容器-文件的命名空间
  • 如何设置适当的权限-使用其他凭据进行安全访问

或者,我对LeaguePHP适配器有很好的体验:

League\Flysystem\AwsS3v3

它提供了一个简单的api来使用amazon web服务来处理文件和内容!它可以独立兼容,也可以使用Symfony或laravel。请查看文档。您可以在源文件夹中看到这些方法。

票数 1
EN

Stack Overflow用户

发布于 2019-10-10 00:23:03

不要将服务注入实体:这是不好的做法。

改用信条事件订阅者:如Symfony docs上所述

代码语言:javascript
复制
# app/config/services.yml
services:
    # ...
    AppBundle\EventListener\SearchIndexerSubscriber:
        tags:
            - { name: doctrine.event_subscriber }

事件订阅者:

代码语言:javascript
复制
// src/AppBundle/EventListener/SearchIndexerSubscriber.php
namespace AppBundle\EventListener;

use AppBundle\Entity\Product;
use Doctrine\Common\EventSubscriber;
// for Doctrine < 2.4: use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
use Doctrine\ORM\Events;

class SearchIndexerSubscriber implements EventSubscriber
{
    public function getSubscribedEvents()
    {
        return [
            Events::postPersist,
            Events::postUpdate,
        ];
    }

    public function postUpdate(LifecycleEventArgs $args)
    {
        $this->index($args);
    }

    public function postPersist(LifecycleEventArgs $args)
    {
        $this->index($args);
    }

    public function index(LifecycleEventArgs $args)
    {
        $entity = $args->getObject();

        // perhaps you only want to act on some "Product" entity
        if ($entity instanceof Product) {
            $entityManager = $args->getObjectManager();
            // ... do something with the Product
        }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35672016

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档