如何在us-east-2中列出存储桶的AWS S3文件?

时间:2019-07-24 15:07:57

标签: amazon-web-services amazon-s3

我完全知道S3是区域不可知的,并且我们系统的其余部分都在us-east-2中无关紧要,但是...

如果我尝试不使用签名或regionm进行初始化,则会告诉我现在需要使用v4签名:

php > $s3Client = \Aws\S3\S3Client::factory(array('key' => 'ACCESS', 'secret' => 'SECRET', 'version' => '2006-03-01'));
php > $objects = $s3Client->getListObjectsIterator(array('Bucket'=>'my-bucket')); foreach ($objects as $object) { echo $object['Key'] . "\n"; };

Warning: Uncaught Aws\S3\Exception\InvalidRequestException: AWS Error Code: InvalidRequest, Status Code: 400, AWS Request ID: REQUEST, 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.0 Guzzle/3.9.2 curl/7.54.0 PHP/7.1.23 ITR
  thrown in phar:///Library/WebServer/lib/aws.phar/Aws/Common/Exception/NamespaceExceptionFactory.php on line 91

我尝试在没有区域的情况下初始化S3:

php > $s3Client = \Aws\S3\S3Client::factory(array('key' => 'ACCESS', 'secret' => 'SECRET', 'signature' => 'v4'));

Warning: Uncaught Aws\Common\Exception\InvalidArgumentException: A region must be specified when using signature version 4 in phar:///Library/WebServer/aws.phar/Aws/S3/S3Client.php:283
Stack trace:
#0 phar:///Library/WebServer/aws.phar/Aws/S3/S3Client.php(171): Aws\S3\S3Client::createSignature(Array)
#1 php shell code(1): Aws\S3\S3Client::factory(Array)
#2 {main}
  thrown in phar:///Library/WebServer/aws.phar/Aws/S3/S3Client.php on line 283

这很有意义,我猜我必须提供一个区域,即使S3不需要区域...

php > $s3Client = \Aws\S3\S3Client::factory(array('key' => 'ACCESS', 'secret' => 'SECRET', 'signature' => 'v4', 'region' => 'us-east-1'));
php > $objects = $s3Client->getListObjectsIterator(array('Bucket'=>'my-bucket', 'Region' => 'us-east-1')); foreach ($objects as $object) { echo $object['Key'] . "\n"; };

Warning: Uncaught Aws\S3\Exception\S3Exception: AWS Error Code: AuthorizationHeaderMalformed, Status Code: 400, AWS Request ID: REQUEST, AWS Error Type: client, AWS Error Message: The authorization header is malformed; the region 'us-east-1' is wrong; expecting 'us-east-2', User-Agent: aws-sdk-php2/2.6.15 Guzzle/3.9.2 curl/7.54.0 PHP/7.1.23 ITR
  thrown in phar:///Library/WebServer/aws.phar/Aws/Common/Exception/NamespaceExceptionFactory.php on line 91

哦...哦...好我想我需要使用us-east-2,因为我们其余的服务器和服务都建立在us-east-2上。

php > $s3Client = \Aws\S3\S3Client::factory(array('key' => 'ACCESS', 'secret' => 'SECRET', 'signature' => 'v4', 'region' => 'us-east-2'));

Warning: Uncaught Aws\Common\Exception\InvalidArgumentException: us-east-2 is not a valid region for Amazon Simple Storage Service in phar:///Library/WebServer/aws.phar/Aws/Common/Client/AbstractClient.php:131
Stack trace:
#0 phar:///Library/WebServer/aws.phar/Aws/Common/Client/ClientBuilder.php(394): Aws\Common\Client\AbstractClient::getEndpoint(Object(Guzzle\Service\Description\ServiceDescription), 'us-east-2', 'https')
#1 phar:///Library/WebServer/aws.phar/Aws/Common/Client/ClientBuilder.php(204): Aws\Common\Client\ClientBuilder->updateConfigFromDescription(Object(Guzzle\Common\Collection))
#2 phar:///Library/WebServer/aws.phar/Aws/S3/S3Client.php(207): Aws\Common\Client\ClientBuilder->build()
#3 php shell code(1): Aws\S3\S3Client::factory(Array)
#4 {main}
  thrown in phar:///Library/WebServer/aws.phar/Aws/Common/Client/AbstractClient.php on line 131

那你为什么要告诉我使用us-east-2 AWS?!?

我的aws.phar是2.6.15版。

1 个答案:

答案 0 :(得分:2)

这是2.x.x版本的AWS PHP SDK的已知问题。 您需要将SDK升级到2.8.x及更高版本。