【问题标题】:Amazon S3 how to validate Access Key Id and Secret Access Key? PHP SDK v3Amazon S3 如何验证访问密钥 ID 和秘密访问密钥? PHP SDK v3
【发布时间】:2015-11-18 09:46:22
【问题描述】:

我试图在 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";
    }

如果我提供了错误的密钥,它只会返回 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/guide/

【问题讨论】:

    标签: php amazon-web-services amazon-s3


    【解决方案1】:

    这是 AWS 命令​​行界面 (CLI) 的技巧。 可以使用下一个命令来验证它们:

    aws iam get-account-authorization-details
    

    即使用户没有执行 GetAccountAuthorizationDetails 操作的权限,如果 AWS 访问密钥正确,您也会得到如下输出:

    An error occurred (AccessDenied) when calling the 
    GetAccountAuthorizationDetails operation: 
    User: arn:aws:iam::012345678901:user/username is not authorized to perform:
    iam:GetAccountAuthorizationDetails
    

    这意味着 AWS 不仅具有经过验证的凭证,而且还返回给您与这些凭证关联的账户 ID 和用户名。

    如果 Access Key ID 错误,你会得到下一个回复: 调用 GetAccountAuthorizatio 时发生错误(InvalidClientTokenId) nDetails操作:请求中包含的安全令牌无效。

    或者,对于 S3 等其他一些服务,如果您尝试执行“aws s3 ls”,它可能会显示 (InvalidAccessKeyId)。

    如果 Secret Access Key 错误,那么你会得到下一个回复: 调用 GetAccountAuthorizati 时发生错误 (SignatureDoesNotMatch) onDetails 操作:我们计算的请求签名与签名不匹配 你提供的性质。检查您的 AWS 秘密访问密钥和签名方法。咨询 有关详细信息,请参阅服务文档。

    【讨论】:

    • 这假定用户具有 IAM 权限,但并非总是如此。替代方案可能是STS.getCallerIdentity() - 请参阅我对另一个问题的回答 (stackoverflow.com/a/53766801/383673)。
    【解决方案2】:

    我去亚马逊论坛寻求专业人士的帮助,我得到了一个答案,there's no service to verify access key id and secret access key,你必须使用一些会抛出异常的服务(我不想这样做,但它是你必须做什么),然后捕获异常并处理它。我使用 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());
        }
    }
    

    这样我可以知道访问密钥 ID 和秘密访问密钥是否有效,以及我的存储桶是否存在。如果您愿意,您可以跳过对存储桶的检查,如果没有发生异常则返回 true,如果您只想检查访问密钥 id 和秘密访问密钥,则返回 false 表示异常,但可能会发生其他一些异常。

    希望这可以帮助遇到与我相同的问题的人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-10
      • 1970-01-01
      • 1970-01-01
      • 2017-03-23
      • 2017-10-02
      • 2022-08-09
      • 2020-03-24
      相关资源
      最近更新 更多