2017-04-27 137 views
0

我的錯誤信息:使用aws-php-sdk無法連接到AWS s3。無效請求

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,並且已經設置UPP我的配置和憑據下~/.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"; 
} 

回答

0

試試這個,它肯定會工作,在你的代碼的問題是你沒有提供的憑證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']; 
+0

謝謝!但我仍然收到'AWS Error Code:InvalidRequest,Status Code:400,AWS Request ID:xxx,AWS錯誤類型:客戶端,AWS錯誤消息:您提供的授權機制不受支持。請使用AWS4-HMAC-SHA256.'雖然:( – vonhact

+0

@vonhact確保您使用的是正確的'KEY'和'SECRET'。 –

+0

我現在的工作!請看看我的編輯。在你的榜樣,你也必須在構造函數中傳遞'signature'和'region'。 – vonhact