【问题标题】:How to work with aws-sdk-php in zf2 project?如何在 zf2 项目中使用 aws-sdk-php?
【发布时间】:2015-04-17 16:10:06
【问题描述】:

我实际上知道 ZF2 有一个名为 aws-sdk-php-zf2 的 aws-sdk-php 模块,但我有一部分使用简单的 sdk,我想在我的 zf2 控制器中使用它而无需2个SDK;一个用于简单的 PHP,另一个用于 ZF2 脚本。有什么办法让它工作吗?

这是我在一个简单的 PHP 脚本中使用 aws-sdk 的工作方式:

require 'vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
// Instantiate an S3 client
$client = S3Client::factory(array(
    'credentials' => array(
        'key'    => 'key',
        'secret' => 'secret_key',
    )
));
$bucket = 'bucket_name';
$keyname = 'project_name/file.ext';

$result = $client->deleteObject(array(
    'Bucket' => $bucket,
    'Key'    => $keyname
)); 
print_r($result);

我怎样才能做到这一点?

【问题讨论】:

  • 您需要将其作为外部库加载到您的 zf2 项目中,请参阅post
  • 谢谢,它也有帮助。

标签: php amazon-web-services amazon-s3 zend-framework2 aws-sdk


【解决方案1】:

一旦通过 Composer 安装:

1) 将其放入public/init_autoloader.php 文件中以设置整个应用程序可用的库,这是我的:

// Composer autoloading
if (file_exists('vendor/autoload.php')) {
    $loader = include 'vendor/autoload.php';
}

$zf2Path = false;

if (is_dir('vendor/ZF2/library')) {
    $zf2Path = 'vendor/ZF2/library';
} elseif (getenv('ZF2_PATH')) { //Support for ZF2_PATH environment variable or git submodule
    $zf2Path = getenv('ZF2_PATH');
} elseif (get_cfg_var('zf2_path')) { //Support for zf2_path directive value
    $zf2Path = get_cfg_var('zf2_path');
}

if ($zf2Path) {
    if (isset($loader)) {
        $loader->add('Zend', $zf2Path);
    } else {
        include $zf2Path . '/Zend/Loader/AutoloaderFactory.php';
        Zend\Loader\AutoloaderFactory::factory(array(
            'Zend\Loader\StandardAutoloader' => array(
                'autoregister_zf' => true
            )
        ));
    }
}

2) 根据需要在控制器中使用它,在我的情况下,以下是控制器内的私有函数:

use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception as S3Exception;
...
private function s3UploadFile($id, $invalidation=false, $file = null, $content = null){
   $response = '';
   //check if the file already exists in S3, if not then build it
   try {
       $s3Client = S3Client::factory(array(
                   'key' => $this->config['aws']['key'],
                   'secret' => $this->config['aws']['secret'],
                   'region' => $this->config['aws']['region']
       ));

       if (!$s3Client->doesObjectExist('clients','/' . $id . '/' . $file))
           $s3Client->putObject(array(
               'Bucket' => 'clients',
               'Key' => '/' . $clientId . '/' . $file,
               'Body' => $content,
               'ACL' => 'public-read'
           ));
   } catch (S3Exception $e) {
       $response = 'error';
   }
   return $response;
}
...

希望对你有所帮助。

【讨论】:

  • 感谢您的回答,但您没有提到将新图书馆放在哪里?除了 init_autoloader.php 在 public 之外,它和 public 在同一个地方......请你能澄清一下......
  • 使用 Composer,库应该在 /myproyect/vendor/aws 下。我假设您通过位于“/myproyect/composer.json”中的“composer.json”文件使用 Composer。
  • 但我看到的是你没有更改文件 init_autoloader.php 中的任何内容......我有相同的内容,我也看不到你在哪里声明了 aws 库(我认为命名空间...)
  • 查看文件的开头,我们正在加载 vendor/autoload.php,它是所有通过 Composer 安装的库的加载器。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多