创建一个包装 AWS SDK 的 Class
Creating A Class That wraps AWS SDK
我正在使用 AWS PHP SDK。通常,我会在我的程序中做这样的事情:
require '/aws_sdk/aws-autoloader.php';
use Aws\Rds\RdsClient;
use Aws\Ec2\Ec2Client;
use Aws\Exception\AwsException;
try {
$ec2Client = new Aws\Ec2\Ec2Client([
'version' => 'latest',
'region' => 'us-west-1,
'profile' => 'default'
]);
} catch (Ec2Exception $ex) {
pretty_echo($ex->getMessage());
}
我想做的是编写一个使用 Aws\Rds\RdsClient、Aws\Ec2\Ec2Client 和 Aws\Exception\AwsException.
的 class
以下是我的命名空间 (esd_aws_sdk.php):
<?php
namespace Esd\AwsSdk;
require PHP_BASE.'/aws_sdk/aws-autoloader.php';
use Aws\Rds\RdsClient;
use Aws\Ec2\Ec2Client;
use Aws\Exception\AwsException;
class Clients
{
public $region;
public $profile;
public $Ec2Client = FALSE;
public $RdsClient = FALSE;
function __construct($region, $profile) {
$this->region = $region;
$this->profile = $profile;
$this->createAwsClients();
}
private function createAwsClients()
{
{
try {
$this->Ec2Client = new Aws\Ec2\Ec2Client([
'version' => 'latest',
'region' => $this->region,
'profile' => $this->profile
]);
} catch (Ec2Exception $ex) {
echo($ex->getMessage());
}
}
try {
$this->RdsClient = new Aws\Rds\RdsClient([
'version' => 'latest',
'region' => $this->region,
'profile' => $this->profile
]);
} catch (RdsException $ex) {
echo($ex->getMessage());
}
}
}
和测试程序:test.php
<?php
SetPaths();
include_once 'aws_sdk.php';
use Esd\AwsSdk;
$AwsClients = new Esd\AwsSdk\Clients('us-east-1', 'airtable');
function SetPaths()
{
$ds = DIRECTORY_SEPARATOR;
$dir = explode($ds, __DIR__);
$dir_last_key = count($dir) - 1;
unset($dir[$dir_last_key]);
$app_root = implode($ds, $dir);
define('APP_CODE',$app_root.$ds.'code'.$ds);
define('APP_INCLUDE',$app_root.$ds.'include'.$ds);
define('APP_DATA',$app_root.$ds.'data'.$ds);
define('APP_LOG',$app_root.$ds.'log'.$ds);
unset($dir[$dir_last_key - ($depth + 1)]);
define('SECTION_COMMON',implode($ds, $dir).$ds.'common'.$ds);
unset($dir[$dir_last_key - ($depth +2)]);
define('PHP_BASE',implode($ds, $dir).$ds);
}
但是当我 运行 test.php 时,我得到以下信息:
Fatal error: Uncaught Error: Class 'Esd\AwsSdk\Aws\Ec2\Ec2Client' not found in /Users/esd-mac/php/aws/aws/code/aws_sdk.php:26
Stack trace: #0 /Users/esd-mac/php/aws/aws/code/aws_sdk.php(19): Esd\AwsSdk\Clients->createAwsClients() #1 /Users/esd-mac/php/aws/aws/code/test.php(7): Esd\AwsSdk\Clients->__construct('us-east-1', 'airtable') #2 {main} thrown in /Users/esd-mac/php/aws/aws/code/aws_sdk.php on line 26
有什么想法吗?
粗糙。回到基础,PHP 解析器似乎不认为它可以根据您的布局找到 class 文件。我认为我们需要查看更多信息才能从中得到好的答案,但我会尝试一些故障排除步骤:
- 您使用的 class 成员名称与您的 Class 名称相同($this->Ec2Client = new Aws\Ec2\Ec2Client),这使您的调试工作更加困难。即使只是在你建造的时候,我也会改成
$this->computeClient = Ec2Client
- 很难判断您的 Use 语句是否正常工作,尤其是当您在
new
调用中调用整个命名空间时。我会将 Use 语句更改为 Use Aws\Ec2\Ec2Client as Ec2Client;
之类的内容,这样您就可以说 = new Ec2Client
。到那时,您的错误看起来会稍微有意义一些,因为它们会告诉您 Ec2Client 是否有意义(并验证 Use 语句是否具有正确的路径)
- 如果我不得不猜测,PHP 认为您正在尝试访问
Esd/AwsSdk
命名空间内的 Aws\Ec2\Ec2Client
。尝试添加一个前导斜杠以通知 PHP 您正在从根 (\Aws\Ec2\Ec2Client
) 加载
我正在使用 AWS PHP SDK。通常,我会在我的程序中做这样的事情:
require '/aws_sdk/aws-autoloader.php';
use Aws\Rds\RdsClient;
use Aws\Ec2\Ec2Client;
use Aws\Exception\AwsException;
try {
$ec2Client = new Aws\Ec2\Ec2Client([
'version' => 'latest',
'region' => 'us-west-1,
'profile' => 'default'
]);
} catch (Ec2Exception $ex) {
pretty_echo($ex->getMessage());
}
我想做的是编写一个使用 Aws\Rds\RdsClient、Aws\Ec2\Ec2Client 和 Aws\Exception\AwsException.
的 class以下是我的命名空间 (esd_aws_sdk.php):
<?php
namespace Esd\AwsSdk;
require PHP_BASE.'/aws_sdk/aws-autoloader.php';
use Aws\Rds\RdsClient;
use Aws\Ec2\Ec2Client;
use Aws\Exception\AwsException;
class Clients
{
public $region;
public $profile;
public $Ec2Client = FALSE;
public $RdsClient = FALSE;
function __construct($region, $profile) {
$this->region = $region;
$this->profile = $profile;
$this->createAwsClients();
}
private function createAwsClients()
{
{
try {
$this->Ec2Client = new Aws\Ec2\Ec2Client([
'version' => 'latest',
'region' => $this->region,
'profile' => $this->profile
]);
} catch (Ec2Exception $ex) {
echo($ex->getMessage());
}
}
try {
$this->RdsClient = new Aws\Rds\RdsClient([
'version' => 'latest',
'region' => $this->region,
'profile' => $this->profile
]);
} catch (RdsException $ex) {
echo($ex->getMessage());
}
}
}
和测试程序:test.php
<?php
SetPaths();
include_once 'aws_sdk.php';
use Esd\AwsSdk;
$AwsClients = new Esd\AwsSdk\Clients('us-east-1', 'airtable');
function SetPaths()
{
$ds = DIRECTORY_SEPARATOR;
$dir = explode($ds, __DIR__);
$dir_last_key = count($dir) - 1;
unset($dir[$dir_last_key]);
$app_root = implode($ds, $dir);
define('APP_CODE',$app_root.$ds.'code'.$ds);
define('APP_INCLUDE',$app_root.$ds.'include'.$ds);
define('APP_DATA',$app_root.$ds.'data'.$ds);
define('APP_LOG',$app_root.$ds.'log'.$ds);
unset($dir[$dir_last_key - ($depth + 1)]);
define('SECTION_COMMON',implode($ds, $dir).$ds.'common'.$ds);
unset($dir[$dir_last_key - ($depth +2)]);
define('PHP_BASE',implode($ds, $dir).$ds);
}
但是当我 运行 test.php 时,我得到以下信息:
Fatal error: Uncaught Error: Class 'Esd\AwsSdk\Aws\Ec2\Ec2Client' not found in /Users/esd-mac/php/aws/aws/code/aws_sdk.php:26
Stack trace: #0 /Users/esd-mac/php/aws/aws/code/aws_sdk.php(19): Esd\AwsSdk\Clients->createAwsClients() #1 /Users/esd-mac/php/aws/aws/code/test.php(7): Esd\AwsSdk\Clients->__construct('us-east-1', 'airtable') #2 {main} thrown in /Users/esd-mac/php/aws/aws/code/aws_sdk.php on line 26
有什么想法吗?
粗糙。回到基础,PHP 解析器似乎不认为它可以根据您的布局找到 class 文件。我认为我们需要查看更多信息才能从中得到好的答案,但我会尝试一些故障排除步骤:
- 您使用的 class 成员名称与您的 Class 名称相同($this->Ec2Client = new Aws\Ec2\Ec2Client),这使您的调试工作更加困难。即使只是在你建造的时候,我也会改成
$this->computeClient = Ec2Client
- 很难判断您的 Use 语句是否正常工作,尤其是当您在
new
调用中调用整个命名空间时。我会将 Use 语句更改为Use Aws\Ec2\Ec2Client as Ec2Client;
之类的内容,这样您就可以说= new Ec2Client
。到那时,您的错误看起来会稍微有意义一些,因为它们会告诉您 Ec2Client 是否有意义(并验证 Use 语句是否具有正确的路径) - 如果我不得不猜测,PHP 认为您正在尝试访问
Esd/AwsSdk
命名空间内的Aws\Ec2\Ec2Client
。尝试添加一个前导斜杠以通知 PHP 您正在从根 (\Aws\Ec2\Ec2Client
) 加载