Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法使用AWS php sdk连接到AWS s3。无效请求_Php_Amazon Web Services_Amazon S3_Aws Sdk - Fatal编程技术网

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

无法使用AWS php sdk连接到AWS s3。无效请求,php,amazon-web-services,amazon-s3,aws-sdk,Php,Amazon Web Services,Amazon S3,Aws Sdk,我的错误消息: 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 u

我的错误消息:

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'];

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

<?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错误代码:InvalidRequest,状态代码:400,AWS请求ID:xxx,AWS错误类型:client,AWS错误消息:您提供的授权机制不受支持。请使用AWS4-HMAC-SHA256。
尽管:(@vonhact)请确保您使用的是正确的
密钥
机密
。我现在就可以使用了!请查看我的编辑。在您的示例中,您还必须在构造函数中传递
签名
区域
<?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'];