簡體   English   中英

Amazon S3:未捕獲的錯誤:未找到 Class 'S3Client'

[英]Amazon S3: Uncaught Error: Class 'S3Client' not found

我正在嘗試將.zip文件上傳到 Amazon S3 服務,但我不斷收到錯誤消息:

未捕獲的錯誤:在 /class/database 中找不到 Class 'S3Client'。class.php:411

這是代碼的基礎。 我有嘗試調試的exit() class_exists function 指定 S3Client 存在並回顯“Class.!!” 到頁面。
我在亞馬遜自動加載文件中添加了一條回顯語句,它回顯了它,因此它正在查找自動加載文件。

為什么PHP在嘗試調用時找不到class?

我的亞馬遜 Class

    require_once( $path["full"].'/assets/vendor/amazon-s3/aws-autoloader.php' );
    use Aws\S3\S3Client;
    class amazons3 {
        // For future use to build some functions to help.
    }

我的自動加載

     //Misc Functions Above    
      include('/path/to/amazon-s3.class.php')
    //Misc Functions Below

內部備份數據庫 Function

     include('/path/to/my/my-autoload.class.php');
     if(class_exists('Aws\S3\S3Client')) {
        echo "CLASS!!!";
        /* Line 411 */ $s3 = new S3Client([
            'version' => 'latest',
            'region'  => 'us-west-2'
        ]);
                        
    }
    else {
        echo "No Class";
    }
    exit(0);
                            
    try {
        $s3->putObject([
            'Bucket' => $data["domain"],
            'Key'    => 'my-object',
            'Body'   => fopen($zipName, 'r'),
            'ACL'    => 'public-read',
        ]);
    } catch (Aws\S3\Exception\S3Exception $e) {
        echo "There was an error uploading the file.\n";
    }

更新我沒有使用作曲家; 我下載了 package 並將其上傳到我的服務器。

我仍然沒有運氣讓它識別 S3Client class。 I added an echo to the top of the S3Client.php right above the class that is in the amazon package to see if it is included and it is and echoed out the string on my page, so from everything I know, all Amazon class files被包含在內。

更新 2我將包含文件添加到 Amazon 自動加載器的包含中。

在您的class_exists調用中,您指定完全限定的 class 名稱(及其命名空間):

     if(class_exists('Aws\S3\S3Client')) {
        echo "CLASS!!!";
    }

但是當您嘗試實例化它時,您只使用 class 名稱:

        /* Line 411 */ $s3 = new S3Client([
            'version' => 'latest',
            'region'  => 'us-west-2'
        ]);            

請注意,錯誤消息不包括命名空間 Aws Aws\S3 ,因為 PHP 不知道S3Client實際上應該表示 Aws Aws\S3\S3Client

如果您使用其完全限定名稱實例化 class 它將起作用:

        /* Line 411 */ $s3 = new Aws\S3\S3Client([
            'version' => 'latest',
            'region'  => 'us-west-2'
        ]);            

要將名稱S3Client擴展到Aws\S3\S3Client ,您的代碼需要 a) 在Aws\S3命名空間內; 或 b) 在文件頂部有適當的use聲明。

要記住的重要一點是namespaceuse語句只影響當前文件,它們不能通過require / include加載。

所以這也將起作用:

/* top of file */
use Aws\S3\S3Client;
// ...
        /* Line 411 */ $s3 = new S3Client([
            'version' => 'latest',
            'region'  => 'us-west-2'
        ]);            

您用於 class 的名稱在每個文件中都可能不同, use... as ,因此您也可以這樣做:

/* top of file */
use Aws\S3\S3Client as AmazonS3;
use Something\Else as S3Client;
// ...
        /* Line 411 */ $s3 = new AmazonS3([
            'version' => 'latest',
            'region'  => 'us-west-2'
        ]);            

To see what PHP resolves a bare class name to in a particular file, you can use the magic ::class suffix, which just expands the name according to current namespace and use rules (even if there isn't actually a class with that name ):

if ( class_exists(S3Client::class) ) {
    echo 'Class exists: ' . S3Client::class;
}
else {
    echo 'Class does not exist: ' . S3Client::class;
}

當您想要一個字符串中的完全限定的 class 名稱時,這幾乎總是您想要做的。

暫無
暫無

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

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