0

我正在尝试使用 php 5.3.3 将文件上传到 s3。我正在使用 Amazon PHP sdk 并使用自动加载器。问题是 AWS 的自动加载器没有正确加载类,我们在加载 S3 时遇到异常。我们的服务器结构如下所示:

->public(www 文档根目录)

->库

->->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' => '******'
));

它成功地工作并打印了 echo '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 )

我们正在使用最新版本的 AWS: https ://github.com/aws/aws-sdk-php

我们正在使用 PHP 5.3.3 和繁荣库:http: //flourishlib.com/

这也有一个像这样的繁荣库自动加载功能:

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自动加载功能正在尝试加载亚马逊类并导致错误。

如何让亚马逊使用正确的自动加载功能?

4

2 回答 2

0

打那个,反转。没有人应该仍然使用该__autoload()功能。这在多年前被弃用,取而代之的是可堆叠spl_autoload_register()功能。

从您发布的代码片段中,FlourishLib通过调用一个函数来做错事__autoload()

于 2014-04-14T09:30:45.377 回答
0

我认为这里唯一的问题是您没有S3Client正确引用该类。在上面的任何地方,我都没有看到您使用完全限定的类名。尝试添加

use Aws\S3\S3Client;

看起来您也不需要其他两个use语句(至少在您提供的代码中),因为您实际上并没有使用这两个类中的任何一个。你应该可以删除

use Aws\Common\Aws;
use Aws\S3\Exception\S3Exception;

是的,我同意 Ryan 这__autoload很糟糕,但看起来你通过注册 spl 自动加载链来正确处理它。

于 2014-04-14T16:06:56.667 回答