Lau*_*182 6 php amazon-s3 amazon-web-services
我正试图在registerStreamWrapper上捕获错误的密钥异常,而它只是没有发生.
我的问题是在尝试验证对象是否存在时产生的,如果有人打电话帮助我(最好的方法),会很棒,但这是另一个问题.回到问题所在.
我正在使用此代码来检查对象是否存在使用registerStreamWrapper:
try{
$s3Client = new \Aws\S3\S3Client($sharedConfig);
$s3Client->registerStreamWrapper();
$file = 's3://'."mybucket".'/'."testpath/testpic.jpg";
if(file_exists($file)){
echo "true";
}else{
echo "false";
}
} catch (S3Exception $e) {
// Catch an S3 specific exception.
echo $e->getMessage();
} catch (AwsException $e) {
// This catches the more generic AwsException. You can grab information
// from the exception using methods of the exception object.
echo $e->getAwsRequestId() . "\n";
echo $e->getAwsErrorType() . "\n";
echo $e->getAwsErrorCode() . "\n";
}
Run Code Online (Sandbox Code Playgroud)
如果我提供了错误的密钥,它只返回false.无论如何都要验证访问密钥ID和秘密访问密钥?这会让我的生活变得如此轻松.
在这里找不到任何东西:http://docs.aws.amazon.com/aws-sdk-php/v3/api/api-s3-2006-03-01.html或这里:http://docs.aws .amazon.com/AWS-SDK-PHP/V3 /引导/
小智 6
以下是AWS命令行界面(CLI)的技巧.可以使用next命令验证它们:
aws iam get-account-authorization-details
Run Code Online (Sandbox Code Playgroud)
即使用户没有授权执行GetAccountAuthorizationDetails操作,如果AWS Access Key正确,那么您将获得如下输出:
An error occurred (AccessDenied) when calling the
GetAccountAuthorizationDetails operation:
User: arn:aws:iam::012345678901:user/username is not authorized to perform:
iam:GetAccountAuthorizationDetails
Run Code Online (Sandbox Code Playgroud)
这意味着AWS不仅具有经过验证的凭据,还会返回与您的凭据链接到的帐户ID和用户名.
如果访问密钥ID错误,则您将收到下一个回复:调用GetAccountAuthoriztio nDetails操作时发生错误(InvalidClientTokenId):请求中包含的安全令牌无效.
或者对于某些其他服务(如S3),如果您尝试执行'aws s3 ls',它可能会说(InvalidAccessKeyId).
如果秘密访问密钥错误,那么您将得到下一个回复:调用GetAccountAuthorizati onDetails操作时发生错误(SignatureDoesNotMatch):我们计算的请求签名与您提供的签名不匹配.检查您的AWS Secret Access Key和签名方法.有关详细信息,请参阅服务文档
我去亚马逊论坛的专业人士那里寻求帮助,我得到了一个答案,没有服务来验证访问密钥ID和秘密访问密钥,你必须使用一些确实抛出异常的服务(我不想这样做,但这是你必须做的),然后捕获异常并处理它。我使用listsBuckets执行了以下函数,并且由于我使用服务只是为了检查我的访问密钥ID和秘密是否正确,所以我添加了检查我要使用的存储桶是否存在:
function checkaccess($bucket){
global $sharedConfig;
try{
$s3Client = new \Aws\S3\S3Client($sharedConfig);
//Get buckets list
$buckets = $s3Client->listBuckets([]);
//Go through every bucket
foreach ($buckets['Buckets'] as $key=>$obj){
//Check if the bucket I'm going to use exists
if ($buckets['Buckets'][$key]['Name'] === $bucket){
//If exists, return true, everything is fine
return true;
}
}
//Bucket doesn't exists but access key id and secret are correct.
return false;
} catch (S3Exception $e) {
//Exception ocurred,
//"SignatureDoesNotMatch" for bad secret access key
//"InvalidAccessKeyId" for invalid access key id
return ($e->getAwsErrorCode());
} catch (AwsException $e) {
//More generic exceptions
error_log($e->getAwsRequestId());
error_log($e->getAwsErrorType());
error_log($e->getAwsErrorCode());
return($e->getAwsErrorType());
}
}
Run Code Online (Sandbox Code Playgroud)
这样我就可以知道访问密钥 ID 和秘密访问密钥是否有效,以及我的存储桶是否存在。如果您愿意,可以跳过对存储桶的检查,如果没有发生异常,则返回 true;如果您只想检查访问密钥 id 和秘密访问密钥,则返回 false,表示异常,但可能会发生其他一些异常。
希望这可以帮助遇到与我相同问题的人。
| 归档时间: |
|
| 查看次数: |
4620 次 |
| 最近记录: |