簡體   English   中英

亞馬遜PHP SDK和Fourishlib自動加載器S3Client無法加載

[英]amazon php sdk and flourishlib autoloader s3client could not be loaded

我正在嘗試使用PHP 5.3.3將文件上傳到s3。 我正在使用Amazon PHP sdk和自動加載器。 問題是AWS的自動加載器無法正確加載類,並且在加載S3時出現異常。 我們的服務器結構如下所示:

->公共(www文檔根)

->庫

->-> aws

因此,我們的上傳代碼位於/ public /中,而我們的AWS庫位於/ lib / aws /中,因此要從公共目錄獲取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' => '******'
));

它成功工作並打印回顯“正在創建...”輸出,然后出現錯誤。

這是異常的樣子:

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,也使用的是flourishlib: http//flourishlib.com/

它也具有一個flourishlib自動加載功能,如下所示:

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()函數。 許多年前不建議使用此方法,而推薦使用可堆疊的spl_autoload_register()函數。

從您發布的代碼片段中,FlourishLib通過完全使用名為__autoload()的函數來做錯 __autoload()

我認為這里唯一的問題是您沒有正確引用S3Client類。 在以上任何地方,我都沒有看到您使用完全限定的類名。 嘗試添加

use Aws\S3\S3Client;

看起來您不需要其他兩個use語句(至少在您提供的代碼中),因為您實際上並沒有使用這兩個類中的任何一個。 您應該可以刪除

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

是的,我同意瑞安說__autoload是壞的,但它似乎你是通過與SPL自動加載鏈登記正確處理它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM