我正在尝试使用php 5.3.3上传一个文件到s3。我正在使用Amazon PHP sdk和autoloader。问题是亚马逊网络服务的自动加载器没有正确加载类,并且在加载S3时出现异常。我们的服务器结构如下所示:
->public (www文档根目录)
->lib
->->aws
因此,我们的上传代码位于/ public /,而我们的AWS库位于/ lib /aws/,因此从public到lib的位置是/../lib/aws/。
下面是启动亚马逊上传的公共文件夹中的upload.php代码:
require $_SERVER['DOCUMENT_ROOT'].'/../lib/aws/aws-autoloader.php';
use Aws\Common\Aws;
use Aws\S3\Exception\S3Exception;
echo 'creating...';
$s3 = S3Client::factory(array(
'key' => '****',
'secret' => '******'
));它成功地工作并打印出回声'creating...‘输出,然后我们得到一个错误。
下面是异常的样子:
2014-04-12 19:46:40 UTC creating...
2014-04-12 19:46:40 UTC ( Exception Object
2014-04-12 19:46:40 UTC [message:protected] => The class S3Client could not be loaded
2014-04-12 19:46:40 UTC [string:Exception:private] =>
2014-04-12 19:46:40 UTC [code:protected] => 0
2014-04-12 19:46:40 UTC [file:protected] => /public/upload.php
2014-04-12 19:46:40 UTC [line:protected] => 26
2014-04-12 19:46:40 UTC [trace:Exception:private] => Array
2014-04-12 19:46:40 UTC (
2014-04-12 19:46:40 UTC [0] => Array
2014-04-12 19:46:40 UTC (
2014-04-12 19:46:40 UTC [function] => __autoload
2014-04-12 19:46:40 UTC [args] => Array
2014-04-12 19:46:40 UTC (
2014-04-12 19:46:40 UTC [0] => S3Client
2014-04-12 19:46:40 UTC )
2014-04-12 19:46:40 UTC
2014-04-12 19:46:40 UTC )
2014-04-12 19:46:40 UTC
2014-04-12 19:46:40 UTC [1] => Array
2014-04-12 19:46:40 UTC (
2014-04-12 19:46:40 UTC [file] => /public/upload.php
2014-04-12 19:46:40 UTC [line] => 15
2014-04-12 19:46:40 UTC [function] => spl_autoload_call
2014-04-12 19:46:40 UTC [args] => Array
2014-04-12 19:46:40 UTC (
2014-04-12 19:46:40 UTC [0] => S3Client
2014-04-12 19:46:40 UTC )
2014-04-12 19:46:40 UTC
2014-04-12 19:46:40 UTC )
2014-04-12 19:46:40 UTC
2014-04-12 19:46:40 UTC [2] => Array
2014-04-12 19:46:40 UTC (
2014-04-12 19:46:40 UTC [file] => /public/upload.php
2014-04-12 19:46:40 UTC [line] => 408
2014-04-12 19:46:40 UTC [args] => Array
2014-04-12 19:46:40 UTC (
2014-04-12 19:46:40 UTC [0] => /public/upload.php
2014-04-12 19:46:40 UTC )
2014-04-12 19:46:40 UTC
2014-04-12 19:46:40 UTC [function] => include
2014-04-12 19:46:40 UTC )
2014-04-12 19:46:40 UTC
2014-04-12 19:46:40 UTC )
2014-04-12 19:46:40 UTC
2014-04-12 19:46:40 UTC [previous:Exception:private] =>
2014-04-12 19:46:40 UTC )我们使用的是最新版本的亚马逊网络服务,网址为:https://github.com/aws/aws-sdk-php
我们使用的是PHP5.3.3和flourishlib:http://flourishlib.com/
它还有一个flourishlib autoload函数,看起来像这样:
function __autoload($class_name)
{
// Customize this to your root Flourish directory
$flourish_root = $_SERVER['DOCUMENT_ROOT'].'/../lib/flourishlib/';
$file = $flourish_root . $class_name . '.php';
if (file_exists($file)) {
include $file;
return;
}
throw new Exception('The class ' . $class_name . ' could not be loaded');
}
spl_autoload_register('__autoload');我认为发生的事情是flurishlib autoload函数试图加载amazon类,这导致了一个错误。
如何让amazon使用正确的自动加载功能?
发布于 2014-04-14 17:30:45
去掉它,反转过来。任何人都不应该继续使用__autoload()函数。这在很多年前就被弃用了,取而代之的是可堆叠的spl_autoload_register()函数。
从您发布的代码片段中可以看出,FlourishLib使用了一个名为__autoload()的函数,这样做是错误的。
发布于 2014-04-15 00:06:57
我认为这里唯一的问题是您没有正确引用S3Client类。在上面的任何地方,我都没有看到您使用了完全限定的类名。尝试添加
use Aws\S3\S3Client;看起来您也不需要另外两个use语句(至少在您提供的代码中是这样),因为您实际上并没有使用这两个类中的任何一个。您应该能够删除
use Aws\Common\Aws;
use Aws\S3\Exception\S3Exception;是的,我同意Ryan的观点,__autoload是不好的,但似乎你通过注册到spl自动加载链来正确地处理它。
https://stackoverflow.com/questions/23054668
复制相似问题