【问题标题】:Cant connect to AWS s3, using aws-php-sdk. Invalid Request无法使用 aws-php-sdk 连接到 AWS s3。无效的请求
【发布时间】:2017-04-28 15:45:28
【问题描述】:

我的错误信息:

Uncaught Aws\S3\Exception\InvalidRequestException: 
AWS Error Code: InvalidRequest, 
Status Code: 400, AWS Request ID: xxx, 
AWS Error Type: client, 
AWS Error Message: The authorization mechanism you have provided is not supported. 
Please use AWS4-HMAC-SHA256., User-Agent: aws-sdk-php2/2.7.27 Guzzle/3.9.3 curl/7.47.0 PHP/7.0.15-0ubuntu0.16.04.4

我的代码:

<?php

use Aws\S3\S3Client;

require 'vendor/autoload.php';

$config = array(
        'key' => 'xxx',
        'secret' => 'xxx',
        'bucket' => 'myBucket'
);

$filepath = '/var/www/html/aws3/test.txt';

//s3
// Instantiate the client.
$s3 = S3Client::factory();

// Upload a file.
$result = $s3->putObject(array(
'Bucket'       => $config['bucket'],
'Key'          => $config['key'],
'SourceFile'   => $filepath,
'Endpoint' => 's3-eu-central-1.amazonaws.com',
'Signature'=> 'v4',
'Region' => 'eu-central-1',
//'ContentType'  => 'text/plain',
//'ACL'          => 'public-read',
//'StorageClass' => 'REDUCED_REDUNDANCY',   
//'Metadata'     => array(    
//    'param1' => 'value 1',
//    'param2' => 'value 2'
)
);

echo $result['ObjectURL'];

无法弄清楚为什么我无法通过。我的通话中有正确的参数。我的代码大部分是从他们的aws-sdk-php 示例页面复制而来的。所以那里不应该有太大的错。

我正在使用 aws cli,并在 ~/.aws/ 下设置了我的配置和凭据文件

编辑:

让它工作!这是我现在的代码:

<?php

// Get dependencies
use Aws\S3\S3Client;
require 'vendor/autoload.php';


// File to upload
$filepath = '/var/www/html/aws3/test.txt';

// instantiate the Client
$s3Client = S3Client::factory(array(
        'credentials' => array(
            'key' => 'AKIAJVKBYTTADILGRTVQ',
            'secret' => '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'
        ),
        'region'            => 'eu-central-1',
        'version'           => 'latest',
        'signature_version' => 'v4'
    ));

// Upload a file.
$result = $s3Client->putObject(array(
        'Bucket' => "myBucket",//some filebucket name
        'Key' => "some_file_name.txt",//name of the object with which it is created on s3
        'SourceFile' => $filepath,
    )
);

// Echo results
if ($result){
    echo $result['ObjectURL'];
} else{
    echo "fail";
}

【问题讨论】:

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


    【解决方案1】:

    试试这个,它肯定会起作用,你的代码中的问题是你没有向factory函数提供凭据。

    <?php
    
    ini_set('display_errors', 1);
    
    use Aws\S3\S3Client;
    
    require 'vendor/autoload.php';
    
    
    //here we are creating client object
    $s3Client = S3Client::factory(array(
                'credentials' => array(
                    'key' => 'YOUR_AWS_ACCESS_KEY_ID',
                    'secret' => 'YOUR_AWS_SECRET_ACCESS_KEY',
                )
            ));
    
    // Upload a file.    
    $filepath = '/var/www/html/aws3/test.txt';
    
    $result = $s3Client->putObject(array(
        'Bucket' => "myBucket",//some filebucket name
        'Key' => "some_file_name.txt",//name of the object with which it is created on s3
        'SourceFile' => $filepath,
        'Endpoint' => 's3-eu-central-1.amazonaws.com',
        'Signature' => 'v4',
        'Region' => 'eu-central-1',
            )
    );
    
    echo $result['ObjectURL'];
    

    【讨论】:

    • 谢谢!但我仍然得到AWS Error Code: InvalidRequest, Status Code: 400, AWS Request ID: xxx, AWS Error Type: client, AWS Error Message: The authorization mechanism you have provided is not supported. Please use AWS4-HMAC-SHA256. :(
    • @vonhact 确保您使用正确的KEYSECRET
    • 我现在开始工作了!请看我的编辑。在您的示例中,您还必须在构造函数中传递 signatureregion
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-26
    • 1970-01-01
    相关资源
    最近更新 更多