Class' Aws \ S3 \ S3Client'将应用程序构建到AWS yii2后找不到

时间:2017-09-16 16:13:17

标签: php amazon-web-services amazon-s3 yii2 yii2-advanced-app

我已将应用程序上传到AWS,该代码在HostGator和localhost中可识别,但是当我将其上传到AWS时,它会显示找不到该文件。我下载了上传的文件并检查了文件是否存在但仍然没有。有什么想法为什么这个问题一直在为我展示?

这是显示错误的页面

<?php

/**
 * S3 Upload component for Yii2 framework
 *
 * S3 is a wrapper for AWS SDK for PHP (@link https://github.com/aws/aws-sdk-php)
 * This wrapper contains minimal functionality as there is only so much I want to allow access to from the Yii public end
 *
 * @version 0.1
 *
 * @author Maxim Gordienko (3dmaxpayne.com)
 */

namespace yii\mag;

use Yii;

use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
use yii\base\Component;
use yii\base\Exception;
use yii\mag\S3\FileUpload;

class S3 extends Component
{

    private $_s3;
    public $key;    // AWS Access key
    public $secret; // AWS Secret key
    public $bucket;
     public $region;
       public $version='2006-03-01';
    public $lastError="";
  private $_folder        = '######';
    private $_treeStructure = true;
    private $_hashFilename  = true;
    private $_fsPath        = '########';
    private $_fsUrl         = '##############';
    private $_ACL           = '########';
    /**
     * Return S3 Client instance
     * @return S3Client
     */
    private function getInstance()
    {
        if ($this->_s3 === NULL){
        $this->_s3=$this->connect();}
        return $this->_s3;
    }

    /**
     * Instance the S3 object
     */
    public function connect()
    {
        if ( $this->key === NULL || $this->secret === NULL )
            throw new Exception('S3 Keys are not set.');

        $this->_s3 = S3Client::factory([
           'credentials' => ['key' => $this->key, 'secret' => $this->secret],  //'key'    => ,
            //'secret' => ,
            'version' => $this->version,
            'region'=>$this->region,
            'bucket'=>$this->bucket,
        ]);

        return  $this->_s3 ;
    }

    /**
     * Upload file to S3
     * @param string $file path to file on local server
     * @param string $fileName name of file on Amazon. It can include directories.
     * @param null $bucket bucket name. By default use bucket from config
     */
    public function upload($file, $fileName, $bucket)
    {

            $bucket = $this->bucket;


         //$aws = Yii::$app->awssdk->getAwsSdk();
     //$s3 =new S3 ;
             //$s3=$s3->createS3();

        if(!empty($fileName)){
        try {
            /*$s3->putObject(array(
                'Bucket' => $bucket,
                //'SourceFile' => $fileName,
                'Key'    => basename($file),
                'Body'   => $file,
                'SourceFile'   => $file->tempName,
             'ContentType'  => $file->type,
                'ACL'    => 'public-read',
                'StorageClass' => 'REDUCED_REDUNDANCY',

            ));*/

              $s3 = $this->getInstance();
              $upload= $s3->putObject(
            array(
             'Bucket'       => $this->bucket,
             'Key'          => $this->getUploadFolder().$this->getFilename($fileName),
             'SourceFile'   => $fileName->tempName,
             'ContentType'  => $fileName->type,
             'ACL'          => $this->_ACL,
             'StorageClass' => 'REDUCED_REDUNDANCY',
            )
        );
        //var_dump($upload);die;
        //return $upload->get(['ObjectURL']);


            //return 1;//$upload['ObjectURL'];

           /* $photo = UploadedFile::getInstance($file, $fileName); 
$uploader = new FileUpload(FileUpload::S_S3, [
    'version' => $this->version,
    'region' => $this->region,
    'credentials' => [
        'key' => $this->key,
        'secret' => $this->secret
    ],
    'bucket' => $this->bucket
]);*/
        //return 1;
        } catch (S3Exception $e) {

            return 0;
            //echo "There was an error uploading the file.\n";
        }}else{return 0;}
    }

    /**
     * Use for call another functions of S3Client
     * @param string $func
     * @param array $args
     * @return mixed
     * @throws \yii\base\Exception
     */
      final function getTreeStructureMap()
    {
        return substr(md5(microtime()), mt_rand(0, 30), 2).DIRECTORY_SEPARATOR.substr(md5(microtime()), mt_rand(0, 30), 2);

    }
    final function getUploadFolder()
    {
        return $this->_folder.'/';

    }

    final function getFilename($file){



        //$fileName  = $file->name;
        $fileName= str_replace([" ", "."], "_", $file->name).date('_Y_m_d_H_i').'.'.$file->extension;
        $pathParts = pathinfo($fileName);

        return  $fileName;

    }

    public function __call($func, $args )
    {
        $s3 = $this->getInstance();
        return call_user_func([$s3, $func], $args[0]);
    }
}

我该怎么办?

0 个答案:

没有答案