2015-02-23 15 views
0

我想這個PHP框架https://github.com/panique/mini與Amazon S3集成:■與服務構建器配置文件http://docs.aws.amazon.com/aws-sdk-php/guide/latest/credentials.html#using-a-configuration-file-with-the-service-builder我得到了一個未定義的變量時,我使用AWS的配置文件與服務構建

最好的是,如果我能將它與現有的config.php https://github.com/panique/mini/blob/master/application/config/config.php集成,但我不知道該如何處理這條線。

$aws = Aws::factory('/path/to/custom/config.php'); 

既然我已經在代碼中包括

的另一部分的config.php這是我已經試過,但不知道爲什麼它不工作

創建一個新的文件aws- config.php文件夾配置並將其包含在我的項目中。 aws-config.php具有以下代碼(使用我的正確密鑰)。

return array(
    // Bootstrap the configuration file with AWS specific features 
    'includes' => array('_aws'), 
    'services' => array(
     // All AWS clients extend from 'default_settings'. Here we are 
     // overriding 'default_settings' with our default credentials and 
     // providing a default region setting. 
     'default_settings' => array(
      'params' => array(
       array(
        'credentials' => array(
         'key' => 'YOUR_AWS_ACCESS_KEY_ID', 
         'secret' => 'YOUR_AWS_SECRET_ACCESS_KEY', 
        ) 
       ), 
       'region' => 'us-west-1' 
      ) 
     ) 
    ) 
); 

我要訪問我的控制器看起來像這樣我的憑據:https://github.com/panique/mini/blob/master/application/controller/songs.php

我實現它像這樣從文檔

<?php 
use Aws\S3\S3Client; 
use Aws\Common\Aws; 

// Create the AWS service builder, providing the path to the config file 
$aws = Aws::factory(APP . 'config/aws-config.php'); 
$client = $aws->get('s3'); 

class Album extends Controller 
{ 
    public function index() 
    { 
      foreach ($images as &$image) { 
       $image->imageThumbnailUrl = $client->getObjectUrl($resizedBucket, 'resized-'.$image->image_name, '+10 minutes'); 

      } 
... 
... 

我得到錯誤信息

Notice: Undefined variable: client in Fatal error: Call to a member function getObjectUrl() on a non-object in

我在我的循環中使用$ client和getObjectUrl。

我的代碼工作正常,如果我使用「傳遞憑據到客戶端工廠方法」http://docs.aws.amazon.com/aws-sdk-php/guide/latest/credentials.html#passing-credentials-into-a-client-factory-method在我的控制器的索引方法。

回答

0

這裏的問題與AWS SDK或panique/mini框架無關。您不能在類定義之外聲明變量,並希望能夠在類定義中使用它們。這就是PHP中變量範圍的工作原理。您需要以某種方式將s3Client對象傳遞到Controller中,或者將其實例化到控制器中。

您可以從字面上將這些行移動到您的索引方法,它應該工作。

// Create the AWS service builder, providing the path to the config file 
$aws = Aws::factory(APP . 'config/aws-config.php'); 
$client = $aws->get('s3'); 
相關問題