How to Use Gaufrette and Symfony 3.0

假如想象 提交于 2020-01-01 06:56:41

问题


I have an issue from figuring out how to use the Symfony 3.0 with Gaufete in order to Upload into an s3 bucket.

According to the documentation: https://github.com/KnpLabs/KnpGaufretteBundle

I have set the config.yml:

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

And the 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%

And because I want to use custom enviromental variables any sort of configuration I pass it a file 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'));

Where I Include it on the top of the config.yml:

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

I have made an Entity names 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;
  }
}

But I do not know how can I get the S3 Adapter and client because in this example:https://github.com/KnpLabs/Gaufrette/blob/master/doc/adapters/awsS3.md

In initiates the s3 client and filesystem. But on Symfony 3.0 I've already configured them on config.yml. Therefore there must be another way to get them.

All I want is an example of usage.


回答1:


I recommend you read this article: https://blog.fortrabbit.com/new-app-cloud-storage-s3

Is is a quick start guide that talks about why you could use decentralized storage and covers the following topics:

  • How to sign up for an AWS account
  • How to create you first S3 bucket storage container — name space for your files
  • How to set up proper permissions — safe access with additional credentials

Alternatively, I have had good experiences with the LeaguePHP Adapter:

League\Flysystem\AwsS3v3

It provides a simple api for using the amazon web services for files and stuff! It's compatible standalone or using Symfony or laravel. Check out the documentation. You can see the methods from the source folder.




回答2:


Don't inject a service into an entity: it is bad practice.

Use Doctrine event subscribers instead: as described on Symfony docs

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

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


来源:https://stackoverflow.com/questions/35672016/how-to-use-gaufrette-and-symfony-3-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!