AWS S3 File Upload: S3Client Class Not Found

15,299

It sounds like the problem is that you are not including anything in your functions.php to enable autoloading the SDK classes. What you include will depend on how you installed the SDK. If you used Composer, the autoloader to use would be located in <your-project-root>/vendor/autoload.php. You can look at the SDK's Getting Started Guide for details on what to do for other installation methods (e.g., aws.phar and aws.zip).

Share:
15,299
Bing
Author by

Bing

Updated on June 04, 2022

Comments

  • Bing
    Bing almost 2 years

    I have a large functions.php file which contains all the functions I'm using in my application. At the top of it I include the use Aws\S3\S3Client; namespace. Below the only contents to the file are a large list of functions.

    I have one function called uploadFile which originally just loaded the file to the local disk and insert a record into a database. Now I want to upload the file to the AWS S3 bucket, but when I try to use the AWS code I found here: http://docs.aws.amazon.com/AmazonS3/latest/dev/UploadObjSingleOpPHP.html

    It gives me this error:

    Fatal error: Class 'Aws\S3\S3Client' not found in C:\wamp\www\project\functions.php
    

    Here is my local implementation:

    function uploadFile($file_name, $target_directory, $files = null) {
        $bucket = getS3Bucket();
        $keyname = getS3Key();
        // $filepath should be absolute path to a file on disk
        $filepath = $file_name;
    
        // Instantiate the client.
        $s3 = S3Client::factory();
    
        // Upload a file.
        $result = $s3->putObject(array(
            'Bucket'       => $bucket,
            'Key'          => $keyname,
            'SourceFile'   => $file_name,
            'ContentType'  => 'text/plain',
            'ACL'          => 'public-read',
            'StorageClass' => 'REDUCED_REDUNDANCY',
            'Metadata'     => array(
                'param1' => 'value 1',
                'param2' => 'value 2'
            )
        ));
    
        return $result;
    }
    

    It seems like I'm facing a problem of using the S3 API along with my procedural code. Is there a way to handle this without having to re-write ALL the places where I do file uploads? (In other words, I'll make any changes needed to the uploadFile function, but I'd hate to have to rewrite every implementation of that function.)