AWS PHP SDK 2 (aws.phar) does not work with Restler Framework

只愿长相守 提交于 2019-12-08 00:25:44

问题


I have been testing some PHP scripts that uses the aws-sdk-php to upload files to S3 storage. These scripts seems to work nicely when they are executed directly from the browser, but fails when trying to use it through an API class in Luracast Restler 3.0

A sample script that upload some dummy file is like the following:

<?php

require_once (dirname(__FILE__) . "/../lib/aws/aws.phar");

use Aws\Common\Aws;
use Aws\S3\Enum\CannedAcl;
use Aws\S3\Exception\S3Exception;
use Aws\Common\Enum\Region;

function test(){
    // Instantiate an S3 client
    $s3 = Aws::factory(array(
        'key'    => 'key',
        'secret' => 'secret',
        'region' => Region::EU_WEST_1
    ))->get('s3');

    try {
        $s3->putObject(array(
            'Bucket' => 'my-bucket',
            'Key'    => '/test/test.txt',
            'Body'   => 'example file uploaded from php!',
            'ACL'    => CannedAcl::PUBLIC_READ
        ));
    } catch (S3Exception $e) {
        echo "There was an error uploading the file.\n";
    }
}

This script is placed in the folder /util/test.php, while the aws-php-sdk is at /lib/aws/aws.phar

To test that this test method is well written I have created another php script at /test/upload.php with the following code:

<?php

require_once (dirname(__FILE__) . '/../util/test.php');

test();

So I can enter in the browser http://mydomain.com/test/upload.php and all is working as expected and the file is uploaded in S3 storage.

However when I call the function test() from an API class with the Restler framework, I have an error that says that Aws cannot be found:

Fatal error: Class 'Aws\Common\Aws' not found in /var/app/current/util/test.php on line 12

However the code is exactly the same that perfectly works when called from upload.php. I have been wasting hours trying to figure out what is happening here, but I cannot get any conclusion. Is like the aws-php-sdk autoloader does not work well under some circumstances.

Any hints?


回答1:


This held me back quite some time,

The issue is caused by reslters autoloader which sets spl_autoload_unregiste as described here: https://github.com/Luracast/Restler/issues/72

One way to get arround the problem is to comment out the relevant lines in vendor/Luracast/Restler/AutoLoader.php

    public static function thereCanBeOnlyOne() {
    if (static::$perfectLoaders === spl_autoload_functions())
        return static::$instance;

/*
    if (false !== $loaders = spl_autoload_functions())
        if (0 < $count = count($loaders))
            for ($i = 0, static::$rogueLoaders += $loaders;
                 $i < $count && false != ($loader = $loaders[$i]);
                 $i++)
                if ($loader !== static::$perfectLoaders[0])
                    spl_autoload_unregister($loader);
    */
    return static::$instance;
}


来源:https://stackoverflow.com/questions/17605125/aws-php-sdk-2-aws-phar-does-not-work-with-restler-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!