簡體   English   中英

AWS S3 PHP SDK、S3Client class 未找到

[英]AWS S3 PHP SDK, S3Client class not found

我正在嘗試使用他們的 PHP SDK v3 建立與 Amazon S3 存儲的連接。

我正在關注本指南: https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/getting-started_basic-usage.html

所以我使用 Composer 安裝了 SDK 並創建了一個名為 ftptest.php 的文件(不要介意名稱),其中包含以下內容:

<?PHP
require '/home/printzel/public_html/new/vendor/autoload.php';

use Aws\S3\S3Client;

use Aws\Exception\AwsException;

//Create a S3Client
$s3 = new Aws\S3\S3Client([
    'version' => 'latest',
    'region' => 'nl-ams1'
]);
?>

但是當轉到頁面時,我收到 HTTP 500 錯誤。 檢查我的錯誤日志時,我看到:

[01-Apr-2021 14:01:27 UTC] PHP Fatal error:  Uncaught Error: Class 'Aws\S3\S3Client' not found in /home/printzel/public_html/new/ftptest.php:9
Stack trace:
#0 {main}
  thrown in /home/printzel/public_html/new/ftptest.php on line 9

如您所見,我包含了我的自動加載文件。 但由於某種原因,它找不到正確的 class,為什么?

這是目前我的結構在我的服務器上的樣子:

自動加載位置:

/home/printzel/public_html/new/vendor/autoload.php

AWS 文件夾位置:

/home/printzel/public_html/new/vendor\aws\aws-sdk-php\src

composer.json 在我的根目錄中:

{
    "require": {
        "aws/aws-sdk-php": "^3.176"
    }
}

您需要將Aws\S3\S3Client替換為S3Client

use Aws\S3\S3Client;
use Aws\Exception\AwsException;

//Create a S3Client
$s3 = new S3Client([
    'version' => 'latest',
    'region' => 'nl-ams1'
]);

更多細節在這里 -文檔

希望能幫到你

您也可以創建別名:

use Aws\S3\S3Client as AwsClient;
use Aws\Exception\AwsException;

//Create a S3Client
$s3 = new AwsClient([
    'version' => 'latest',
    'region' => 'nl-ams1'
]);

確保包含 SDK 自動加載器的路徑,我建議您使用如下所示的相對路徑。

// require the amazon sdk from your composer vendor dir
require __DIR__.'/vendor/autoload.php';

完成后,如果仍然失敗,則必須在 sdk 路徑或 sdk 版本上遇到問題,請確保您按照以下步驟一一執行並一一重做。

1. 通過 Composer 為 PHP 添加 AWS SDK 作為依賴項

如果 Composer 已全局安裝在您的系統上,請在項目的基本目錄中運行以下命令以安裝 AWS SDK for PHP 作為依賴項:

composer require aws/aws-sdk-php

2. 將自動加載器添加到您的 php 腳本

要在腳本中為 PHP 使用 AWS SDK,請在腳本中包含自動加載器,如下所示。

<?php
   require '/path/to/vendor/autoload.php';
?>

3. 確保您在 enr 中設置了訪問權限密鑰。

您需要 AWS_ACCESS_KEY_ID 和 AWS_SECRET_ACCESS_KEY。 在 enr 中設置。 如果您這樣做,您可以忽略將 AWS_SECRET_ACCESS_KEY 和 AWS_ACCESS_KEY_ID 傳遞給代碼。

<?php

use Aws\S3\S3Client;

define('AWS_KEY', 'place access key here');
define('AWS_SECRET_KEY', 'place secret key here');

// require the amazon sdk from your composer vendor dir
require __DIR__.'/vendor/autoload.php';

// Instantiate the S3 class and point it at the desired host
$client = new S3Client([
    'region' => '',
    'version' => '2021-03-01',
    'credentials' => [
        'key' => AWS_ACCESS_KEY_ID ,
        'secret' => AWS_SECRET_ACCESS_KEY 
    ]
]);

暫無
暫無

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

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