繁体   English   中英

使用aws-php-sdk无法连接到AWS s3。 无效请求

[英]Cant connect to AWS s3, using aws-php-sdk. Invalid Request

我的错误讯息:

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";
}

尝试此操作,它肯定会工作,您代码中的问题是您尚未向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'];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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