2

s3 バケットにアップロードするために Symfony 3.0 を Gaufete で使用する方法を理解するのに問題があります。

ドキュメントによると: 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;
  }
}

しかし、この例ではhttps://github.com/KnpLabs/Gaufrette/blob/master/doc/adapters/awsS3.mdであるため、S3 アダプターとクライアントを取得する方法がわかりません。

s3 クライアントとファイルシステムを開始します。しかし、Symfony 3.0 では、config.yml で既に構成済みです。したがって、それらを取得する別の方法が必要です。

私が欲しいのは使用例です。

4

2 に答える 2

1

この記事を読むことをお勧めします: https://blog.fortrabbit.com/new-app-cloud-storage-s3

Is は、分散型ストレージを使用できる理由を説明するクイック スタート ガイドであり、次のトピックをカバーしています。

  • AWS アカウントにサインアップする方法
  • 最初の S3 バケット ストレージ コンテナの作成方法 — ファイルの名前空間
  • 適切な権限を設定する方法 - 追加の認証情報による安全なアクセス

または、LeaguePHP Adapter で良い経験をしました。

リーグ\フライシステム\AwsS3v3

ファイルなどに amazon web サービスを使用するためのシンプルな api を提供します! スタンドアロンでも、Symfony または laravel を使用しても互換性があります。ドキュメントを確認してください。ソースフォルダーからメソッドを確認できます。

于 2016-02-27T18:09:48.023 に答える