这是从AWS S3 Bucket事件接收消息的代码。
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);
这是var_dump的输出。 json_decode作为第二个参数传递为true,因此返回一个数组。
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"
}
}
}
}
我的问题是这个:
什么是处理此数组的最佳方法,以便我可以轻松访问其中的元素。我希望能够举例说明 [“键”] => 字符串(16)“ presignedurl.php”
,并且通常很容易访问其他数组元素,而无需遍历整个数组。这可能吗 ?任何建议。例子会很棒。在此先感谢您的帮助-我已经搜索了示例和教程,但找不到任何有用的东西。
答案 0 :(得分:0)
我认为下面的代码应该解析并返回所需的键值。
$decode_msg = json_decode($msg["Body"],true);
$object_key = $decode_msg['Records'][0]['s3']['object']['key'];