简体   繁体   中英

AWS S3 PHP SDK, S3Client class not found

I am trying to set up a connection to an Amazon S3 storage using their PHP SDK v3.

I am following this guide: https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/getting-started_basic-usage.html

So I installed the SDK using Composer and created a file called ftptest.php (don't mind the name), that contains this:

<?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'
]);
?>

But when going to the page, I get a HTTP 500 error. When checking my error log, I see this:

[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

I included my autoload file as you can see. But for some reason it can't find the correct class, why?

This is currently how my structure looks like on my server:

Autoload location:

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

AWS folder location:

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

composer.json in my root:

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

You need replace Aws\S3\S3Client with S3Client :

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

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

More detail is here - Documentation

Hope help you

Also you can to create the alias:

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

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

Make sure you include the path to the SDK autoloader, I suggest you use the relative path like below.

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

Once you have done, if its still fails you must have issue on the sdk path or sdk version, that, make sure you follow below steps one by one and redo them one by one.

1. Add AWS SDK for PHP as a dependency via Composer

If Composer is already installed globally on your system, run the following in the base directory of your project to install AWS SDK for PHP as a dependency:

composer require aws/aws-sdk-php

2. Add autoloader to your php scripts

To utilize the AWS SDK for PHP in your scripts, include the autoloader in your scripts, as follows.

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

3. Make sure you have the access right keys setup in the enr.

You need AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY. set in the enr. if you do you can ignore the passing of the AWS_SECRET_ACCESS_KEY and AWS_ACCESS_KEY_ID to the code.

<?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 
    ]
]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM