2017-07-06 74 views
1

我嘗試使用下面的代碼來檢查在Amazon S3存儲存在:缺少必要的客戶端的配置選項:區域

$credentials = new Aws\Common\Credentials\Credentials($creds['access_key_id'], $creds['secret_access_key']); 
$client = Aws\S3\S3Client::factory(array('credentials' => $credentials)); 
if(! $client->doesBucketExist($creds['bucket'])) { 
    throw new Exception("Bucket (" . $creds['bucket'] . ") does not exist."); 
} 

它工作在localhost(WAMP),但是當我嘗試這個服務器上它無法正常工作。我收到以下錯誤:

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://docs.aws.amazon.com/aws-sdk-php/v3/api/index.html . If you are unable to load a specific API version, then you may need to update your copy of the SDK.

我不知道爲什麼它不工作的服務器,但相同的代碼工作在本地主機上。

+0

您可能在本地主機上的aws配置文件(主目錄中的.aws文件夾)中有默認區域設置,但不在服務器上? – RaGe

回答

2

創建s3客戶端時顯式設置區域,而不是依賴默認值。

use Aws\Credentials\Credentials; 
use Aws\S3\S3Client; 

$result = $stsClient->getSessionToken(); 

$credentials = new Credentials(
    $result['Credentials']['AccessKeyId'], 
    $result['Credentials']['SecretAccessKey'], 
    $result['Credentials']['SessionToken'] 
); 

$s3Client = new S3Client([ 
    'version'  => '2006-03-01', 
    'region'  => 'us-west-2', 
    'credentials' => $credentials 
]); 
+0

爲什麼我需要提供'Aws \ Credentials \ Credentials'而不是'Aws \ Common \ Credentials \ Credentials' – EmptyData

+0

取決於您使用的aws-sdk-php版本。 2.8 vs 3.0共同走了。見[1](https://github.com/aws/aws-sdk-php/tree/master/src/Credentials)vs [2](https://github.com/aws/aws-sdk-php/樹/ 2.8/src目錄/ AWS /通用/憑證) – RaGe