Как читать журналы cloudTrail с помощью AWS SDK в PHP

Я новичок в API AWS SDK.

У меня есть ведро s3 (my-bucket), которое мой клиент использует для загрузки файлов оттуда. У меня есть Trail (my-trail), используемый для доставки журналов в другое ведро s3 (my-bucket-logs) всякий раз, когда мой клиент загружает файлы из «my-bucket».

Я хочу читать журналы из «my-bucket-logs» и сохранять их в своей CMS для создания различных отчетов. Можно ли получить логи? Я пробовал ниже PHP-код. Я не уверен, правильный ли этот код или нет.

require 'vendor/autoload.php';
$config = [
    'version' => 'latest',
    'region'  => 'us-east-1',
    'key' => MY_AWS_KEY,
    'secret'  => MY_AWS_SECRETE
];
$s3client = new Aws\S3\S3Client($config);
$trailClient = new Aws\CloudTrail\CloudTrailClient($config);


$result = $trailClient->lookupEvents([
    'EndTime' => time(),
    'LookupAttributes' => [
        [
            'AttributeKey' => 'eventName',// 'get', // REQUIRED
            'AttributeValue' => 'ListObjects' //get', // REQUIRED
        ],
        // ...
    ],
    'MaxResults' => 1000
    //'NextToken' => '<string>',
    //'StartTime' => '01/12/2017',
]);

print_r($result);

Получение этой ошибки:

PHP Fatal error:  Uncaught exception 'Aws\CloudTrail\Exception\CloudTrailException' with message 'Error executing "LookupEvents" on "https://cloudtrail.us-east-1.amazonaws.com"; AWS HTTP error: Client error: `POST https://cloudtrail.us-east-1.amazonaws.com` resulted in a `400 Bad Request` response:
{"__type":"InvalidLookupAttributesException","Message":"You cannot perform a lookup on this attribute key: EventId|Event (truncated...)
 InvalidLookupAttributesException (client): You cannot perform a lookup on this attribute key: EventId|EventName|Username|ResourceType|ResourceName|EventSource - {"__type":"InvalidLookupAttributesException","Message":"You cannot perform a lookup on this attribute key: EventId|EventName|Username|ResourceType|ResourceName|EventSource"}'

exception 'GuzzleHttp\Exception\ClientException' with message 'Client error: `POST https://cloudtrail.us-east-1.amazonaws.com` resulted in a `400 Bad Request` response:
{"__type":"InvalidLookupAttributesException","Message":"You cannot perform a lookup on this attribu in /home/shahid/webroot/shahid/aws/sdk/vendor/aws/aws-sdk-php/src/WrappedHttpHandler.php on line 191

Можно ли получать журналы с помощью AWS SDK API в PHP?


person elegant-user    schedule 29.01.2018    source источник


Ответы (1)


Я смог прочитать журналы из корзин s3. Это был просто вопрос загрузки файлов из самой корзины s3, и я запутался, используя Aws\CloudTrail\CloudTrailClient.

<?php
    /**
     * Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
     *
     * This file is licensed under the Apache License, Version 2.0 (the "License").
     * You may not use this file except in compliance with the License. A copy of
     * the License is located at
     *
     * http://aws.amazon.com/apache2.0/
     *
     * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
     * CONDITIONS OF ANY KIND, either express or implied. See the License for the
     * specific language governing permissions and limitations under the License.
     */

    require 'vendor/autoload.php';

    use Aws\S3\S3Client;
    use Aws\Exception\AwsException;

    /**
     * Get/Download an Object from Amazon S3.
     *
     * This code expects that you have AWS credentials set up per:
     * http://docs.aws.amazon.com/aws-sdk-php/v3/guide/guide/credentials.html
     */
    $bucket = "my-bucket-logs";
    $key = "xxxxxxxxxx";
    $filePath = "my-log-files.json";

    try {
        $result = $s3Client->getObject(array(
                'Bucket' => $bucket
                ,'Key'    => $key
                ,'SaveAs' => $filePath
            ));


        } catch (S3Exception $e) {
        echo $e->getMessage() . "\n";
    }
person elegant-user    schedule 21.02.2018