0

This is the code that receives a message from an AWS S3 Bucket event.

include_once('../aws/aws-autoloader.php');
include_once('AWSMsgQueueUserActivity.php');
include_once('AWSMsgQueueS3.php');


use Aws\Sqs\SqsClient;
use Aws\Exception\AwsException;
use Aws\Credentials\Credentials;

//$receive = new AWSMsgQueueUserActivity();
$receive = new AWSMsgQueueS3();

do
{
    //sleep(1);
    $result = $receive->getMessage();
   // print_r('\n');

    if (count($result->get('Messages')) > 0) {

        $msg = $result->get('Messages')[0];
        $decode_msg = json_decode($msg["Body"],true);
        var_dump($decode_msg);

        $receive->deleteMessage($result->get('Messages')[0]['ReceiptHandle']);
    }
 else{
     //echo "No Message In Queue\n";
 }

} while (true);

This is the output of the var_dump. The json_decode was passed true as its second parameter so, got back an array.

array(1) {
  ["Records"]=>
  array(1) {
    [0]=>
    array(9) {
      ["eventVersion"]=>
      string(3) "2.1"
      ["eventSource"]=>
      string(6) "aws:s3"
      ["awsRegion"]=>
      string(9) "us-east-1"
      ["eventTime"]=>
      string(24) "2019-06-30T18:26:49.428Z"
      ["eventName"]=>
      string(17) "ObjectCreated:Put"
      ["userIdentity"]=>
      array(1) {
        ["principalId"]=>
        string(13) "ANRN8QDWGH20Q"
      }
      ["requestParameters"]=>
      array(1) {
        ["sourceIPAddress"]=>
        string(13) "69.14.165.122"
      }
      ["responseElements"]=>
      array(2) {
        ["x-amz-request-id"]=>
        string(16) "21D1A6ADF98A39B0"
        ["x-amz-id-2"]=>
        string(76) "9G957u6trQA+Q6VjxwfZy6Q+0OKppD8vjwiPr8ESDXqnUXm4uaX76gsKF9rRsWEqjphcdOjjxTc="
      }
      ["s3"]=>
      array(4) {
        ["s3SchemaVersion"]=>
        string(3) "1.0"
        ["configurationId"]=>
        string(24) "shiftproviderbucketevent"
        ["bucket"]=>
        array(3) {
          ["name"]=>
          string(13) "shiftprovider"
          ["ownerIdentity"]=>
          array(1) {
            ["principalId"]=>
            string(13) "ANRN8QDWGH20Q"
          }
          ["arn"]=>
          string(26) "arn:aws:s3:::shiftprovider"
        }
        ["object"]=>
        array(4) {
          ["key"]=>
          string(16) "presignedurl.php"
          ["size"]=>
          int(774)
          ["eTag"]=>
          string(32) "21308bd245239e3a72bdd1c78c087f1a"
          ["sequencer"]=>
          string(18) "005D18FEE953EB0DD0"
        }
      }
    }
  }

My question is this:

What is the best approach to handling this array so I can easily access elements inside. I want to be able to get just as an example the value of ["key"]=> string(16) "presignedurl.php"

and generally easily access other array elements without iterating through the whole array. Is this possible ? Any recommendations. Examples would be great. Thanks in advance in for help - I've searched for examples and tutorials and can't find anything that helps.

  • 2
    Possible duplicate of [Parsing JSON with PHP](https://stackoverflow.com/questions/6964403/parsing-json-with-php) – ehymel Jun 30 '19 at 19:07

1 Answers1

0

I think the below code should parse and return the desired key value.

$decode_msg = json_decode($msg["Body"],true);
$object_key = $decode_msg['Records'][0]['s3']['object']['key'];
Mohit Kale
  • 91
  • 6