AWS SDK for PHP - 致命错误问题

时间:2017-04-07 10:04:07

标签: php amazon-web-services amazon-s3 aws-sdk

我正在尝试连接到我的S3以通过我的服务器上传文件,但每当我尝试运行PHP时,我都会遇到以下错误。我包含版本区域,但问题仍然存在?

错误:

Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Missing required client configuration options: region: (string) A "region" configuration value is required for the "s3" service (e.g., "us-west-2"). A list of available public regions and endpoints can be found at http://docs.aws.amazon.com/general/latest/gr/rande.html. version: (string) A "version" configuration value is required. Specifying a version constraint ensures that your code will not be affected by a breaking change made to the service. For example, when using Amazon S3, you can lock your API version to "2006-03-01". Your build of the SDK has the following version(s) of "s3": * "2006-03-01" You may provide "latest" to the "version" configuration value to utilize the most recent available API version that your client's API provider can find. Note: Using 'latest' in a production application is not recommended. A list of available API versions can be found on each client's API documentation page: http:/ in /srv/http/auploader/include/Aws/ClientResolver.php on line 364 

我的代码:

<?PHP
require '/srv/http/test/include/aws-autoloader.php';

use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;

$bucket = 'testbucket';
$keyname = 'sample';
// $filepath should be absolute path to a file on disk                      
$filepath = '/srv/http/testfile/setup.html';

// Instantiate the client.
$s3 = S3Client::factory(array(
    'key'    => 'blank',
    'secret' => 'blank'
));

try {
    // Upload data.
    $result = $s3->putObject(array(
        'Bucket' => $bucket,
        'Key'    => $keyname,
        'SourceFile'   => $filepath,
        'ACL'    => 'public-read',
        'Region'  => 'eu-west-1',
        'Version' => '2006-03-01'
    ));



    // Print the URL to the object.
    echo $result['ObjectURL'] . "\n";
} catch (S3Exception $e) {
    echo $e->getMessage() . "\n";
}
?>

2 个答案:

答案 0 :(得分:2)

您必须创建S3的对象。你放的钥匙是错位的,请按照以下方式进行操作。

$s3 = S3Client::factory([
                'version'     => 'latest',
                'region'      => 'eu-west-1',
                'credentials' => [
                    'key'    => "your s3 bucket key",
                    'secret' => "your s3 bucket secret key",
                ]
            ]);

通过使用s3对象,您可以实现类似这样的putObject方法。

$result = $s3->putObject(array(
            'Bucket'     => "yourbucket name",
            'Key'        => $keyName,
            'SourceFile' => $filepath,
            'ACL'        => 'public-read', //for making the public url
            'Version'    => '2006-03-01'
));
        ));

希望它有所帮助!

答案 1 :(得分:0)

SES AWS SDK v3使用

/*
* 1. version as `2010-12-01`
* 2. version as Eg. `us-east-1`.
*/
ini_set("display_errors", 1);
Aws\Ses\SesClient::factory(array(
        'credentials' => array(
            'key' => "someKey",
            'secret' => "someSecret",
        ),
        "region" => "us-east-1",
        "version" => "2010-12-01")
);