2017-04-07 135 views
0

我想通過我的服務器連接到我的S3上傳文件,但每當我嘗試運行PHP時,我都會遇到下面的錯誤。我包括版本地區但這個問題仍然存在?適用於PHP的AWS開發工具包 - 致命錯誤問題

錯誤:

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"; 
} 
?> 
+0

我在錯誤的部分信息,它需要下鍵/祕密。解決了。 – irishwill200

+0

是的,這就是我指出http://stackoverflow.com/a/43275437/2667307 :) –

回答

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

希望它有幫助!

+0

你有php-libxml包嗎?如果沒有安裝,請安裝並重新啓動Apache –

+0

我意識到我有錯誤的關鍵!它現在使用上述方法上傳,但在文件內我只收到路徑。如果我指定key爲'sample.html',但爲文件路徑添加'/ srv/http/test/setup.html'。在s3裏面的sample.html我只能看到'/ srv/http/test/setup.html' – irishwill200

+0

它沒有上傳文件我輸入的位置也是因爲你說'Body'不是'SourceFile' – irishwill200

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") 
); 
相關問題