【问题标题】:How to Use Gaufrette and Symfony 3.0如何使用 Gaufrette 和 Symfony 3.0
【发布时间】:2016-06-10 20:48:09
【问题描述】:

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

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

我已经设置了 config.yml:

knp_gaufrette:
    adapters:
        photostorage:
            amazon_s3:
                amazon_s3_id:   amazonS3
                bucket_name:    '%save_location%'
                options:
                    directory:  'symphotest'

还有 services.yml:

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:

  $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 顶部的位置:

imports:
    - { resource: params.php }
    - { resource: security.yml }
    - { resource: services.yml }

我已经创建了一个实体名称 Images.php:

<?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

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

我想要的只是一个用法示例。

【问题讨论】:

    标签: php amazon-s3 symfony gaufrette


    【解决方案1】:

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

    Is 是一份快速入门指南,介绍了为什么可以使用去中心化存储并涵盖以下主题:

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

    另外,我对 LeaguePHP Adapter 有很好的体验:

    League\Flysystem\AwsS3v3

    它提供了一个简单的 api 用于使用亚马逊网络服务来处理文件和东西!它可以独立兼容,也可以使用 Symfony 或 laravel。查看文档。您可以从源文件夹中查看方法。

    【讨论】:

    • 问题在于如何在一个条目中使用适配器。我找不到 CODE 示例 - 为了理解和使用它而使用它的示例。
    • 首先,通常你不能模仿任何代码示例并得到正确的结果。其次......我认为,你不明白这个问题!第三,您使用并放弃了包,这意味着它现在可能无法使用。第三,我的建议是将适配器包更改为对用户更友好的包。 packagist.org/search/?q=amazonwebservices%2Faws-sdk-for-php
    【解决方案2】:

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

    改用 Doctrine 事件订阅者:如 Symfony docs 中所述

    # app/config/services.yml
    services:
        # ...
        AppBundle\EventListener\SearchIndexerSubscriber:
            tags:
                - { name: doctrine.event_subscriber }
    

    事件订阅者:

    // 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
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-07-07
      • 2014-02-24
      • 2016-03-23
      • 1970-01-01
      • 1970-01-01
      • 2016-10-01
      • 1970-01-01
      • 2014-02-11
      • 1970-01-01
      相关资源
      最近更新 更多