15.AWS の DynamoDB、S3に保管したデータをEC2のWebサーバで表示

前へ 目次

AWS の DynamoDB に保管したデータを EC2 の Webサーバから表示するPHPプログラム例です。

普段は PHP MVC Framework を使って、Json での Rest full Web API で実装しますが、デモなのでこの辺りのサンプルで。。。

試作システム

</code>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>塩ケンピ試食者一覧</title>
<style type="text/css">

table {
border-collapse:collapse;
border-spacing:0;
background-color:#ffffff;
empty-cells:show;
}

th {
border-right:1px solid #663300;
border-bottom:1px solid #663300;
color:#ffffff;
background-color:#006400;
padding: 5px 4px;
text-align:center;
}

td {
border-right:1px solid #006400;
border-left:1px solid #006400;
border-top:1px solid #006400;
border-bottom:1px solid #006400;
padding:0.1em 0.3em;
}

.tdback {
background-color:#008000;
color:#ffffff;
}

h2 {
position: relative;
color: #ffffff;
background: #008000;
font-size: 16px;
line-height: 1;
margin: 0px 0px 5px 5px;
padding: 5px 5px 5px 10px;
border-radius: 5px 5px 5px 5px;
}

p {
margin:0 0 15px 5px;
}

a {
text-decoration:underline;
}
a:hover {
color: #ffffff;
background: #008000;
text-decoration:underline;
}

</style>

</head>
<body>

<h2>塩ケンピ試食者検索条件</h2>

<?php

ini_set( 'display_errors', 1 );
ini_set('error_reporting', E_ALL);

require_once('/usr/share/pear/AWSSDKforPHP/aws.phar');
use Aws\Common\Aws;
use Aws\DynamoDb\DynamoDbClient;
use Aws\DynamoDb\Exception\DynamoDbException;
use Aws\Common\Enum\Region;
use Guzzle\Http\EntityBody;

$aws = Aws::factory("/usr/share/pear/AWSSDKforPHP/Aws/Common/Resources/aws-config.php");

$interval = "-1 hour";
if(isset($_REQUEST['r'])) $interval = $_REQUEST['r']."";

$r = array("","","","","","","","");
switch ($interval) {
case "-1 hour":
$r[0] = " selected";
break;
case "-2 hour":
$r[1] = " selected";
break;
case "-4 hour":
$r[2] = " selected";
break;
case "-6 hour":
$r[3] = " selected";
break;
case "-12 hour":
$r[4] = " selected";
break;
case "-1 day":
$r[5] = " selected";
break;
case "-2 day":
$r[6] = " selected";
break;
}

?>

<form name="frm01" action="splist.php" method="post" >
表示範囲:<select name="r">
<option value="-1 hour"<?php echo $r[0]; ?>>1時間</option>
<option value="-2 hour"<?php echo $r[1]; ?>>2時間</option>
<option value="-4 hour"<?php echo $r[2]; ?>>4時間</option>
<option value="-6 hour"<?php echo $r[3]; ?>>6時間</option>
<option value="-12 hour"<?php echo $r[4]; ?>>12時間</option>
<option value="-1 day"<?php echo $r[5]; ?>>1日</option>
<option value="-2 day"<?php echo $r[6]; ?>>2日</option>
</select>
<input type="submit" value="Do">
</form>

<?php

// Dynamo の処理
$dynamo     = $aws->get('dynamodb');
$dynamoTbl  = "demo-distance";            // TABLE NAME

$now        = new DateTime();
$eymdhims   = $now->format('YmdHis');
$now->modify($interval);
$symdhims   = $now->format('YmdHis');
//echo $symdhims."〜".$eymdhims."<br />\n";

?>

<br /><br />
<h2>塩ケンピ試食者一覧</h2>
<?php

// Image
try{
$iterator = $dynamo->getIterator('Query', array(
'TableName'     => $dynamoTbl,
'KeyConditions' => array(
'HashKey' => array(
'AttributeValueList' => array(
array('S' => 'D001-PIC-DIST')
),
'ComparisonOperator' => 'EQ'
),
'RangeKey' => array(
'AttributeValueList' => array(
array('S' => "".$symdhims),
array('S' => "".$eymdhims)
),
'ComparisonOperator' => 'BETWEEN'
)
),
'ScanIndexForward' => false,
)
);
$count = 1;
foreach ($iterator as $item) {
if($count == 1){
echo "<table>\n";
echo "<tr><th>No.</th><th>日付</th><th>距離</th><th>画像・情報</th>";
echo "<th>HashKey</th><th>RangeKey</th><th>HashKey01</th><th>RangeKey02</th><th>RangeKey03</th></tr>";
}
echo "<tr><td>".$count."</td><td>".$item['datetime']['S']."</td><td>".$item['distance']['S']." ".$item['unit']['S']."</td>";
echo "<td><a href=\"ImageData.php?b=".$item['s3backet']['S']."&k=".$item['s3key']['S']."\" target=\"_blank\">画像</a> ";
echo "<a href=\"DataGet.php?p=".$item['HashKey01']['S']."\" target=\"_blank\">情報</a></td>\n";
echo "<td>".$item['HashKey']['S']."</td><td>".$item['RangeKey']['S']."</td>";
echo "<td>".$item['HashKey01']['S']."</td><td>".$item['RangeKey02']['S']."</td><td>".$item['RangeKey03']['S']."</td></tr>";
$count++;
}
if($count > 1) echo "</table>\n";
if($count < 2) echo "<table><tr><td>該当するデータはありませんでした。</td></tr></table>\n";
} catch (DynamoDbException $exc) {
echo "<table><tr><td>エラーが発生しました : ".$exc->getMessage()."</td></tr></table>\n";
}
?>

<br /><br />
<h2>環境情報(温湿度情報)</h2>
<?php
// Data
try{
$iterator = $dynamo->getIterator('Query', array(
'TableName'     => $dynamoTbl,
'KeyConditions' => array(
'HashKey' => array(
'AttributeValueList' => array(
array('S' => 'D001-AIR-TEMP')
),
'ComparisonOperator' => 'EQ'
),
'RangeKey' => array(
'AttributeValueList' => array(
array('S' => "".$symdhims),
array('S' => "".$eymdhims)
),
'ComparisonOperator' => 'BETWEEN'
)
),
'ScanIndexForward' => false,
)
);
$count = 1;
foreach ($iterator as $item) {
if($count == 1){
echo "<table>\n";
echo "<tr><th>No.</th><th>日付</th><th>温度</th><th>湿度</th><th>情報</th>";
echo "<th>HashKey</th><th>RangeKey</th><th>HashKey01</th><th>RangeKey02</th><th>RangeKey03</th></tr>";
}
echo "<tr><td>".$count."</td><td>".$item['datetime']['S']."</td><td>".$item['air_temp']['S']." ".$item['temp_unit']['S']."</td>";
echo "<td>".$item['air_hum']['S']." ".$item['hum_unit']['S']."</td>";
echo "<td><a href=\"DataGet.php?p=".$item['HashKey01']['S']."\" target=\"_blank\">情報</a></td>\n";
echo "<td>".$item['HashKey']['S']."</td><td>".$item['RangeKey']['S']."</td>";
echo "<td>".$item['HashKey01']['S']."</td><td>".$item['RangeKey02']['S']."</td><td>".$item['RangeKey03']['S']."</td></tr>";
$count++;
}
if($count > 1) echo "</table>\n";
if($count < 2) echo "<table><tr><td>該当するデータはありませんでした。</td></tr></table>\n";
} catch (DynamoDbException $exc) {
echo "<table><tr><td>エラーが発生しました : ".$exc->getMessage()."</td></tr></table>\n";
}
?>

<br /><br />
<h2>環境情報(Raspberry Pi 情報)</h2>
<?php
// Data
try{
$iterator = $dynamo->getIterator('Query', array(
'TableName'        => $dynamoTbl,
'ScanIndexForward' => false,
'KeyConditions'    => array(
'HashKey' => array(
'AttributeValueList' => array(
array('S' => 'D001-CPU-TEMP')
),
'ComparisonOperator' => 'EQ'
),
'RangeKey' => array(
'AttributeValueList' => array(
array('S' => "".$symdhims),
array('S' => "".$eymdhims)
),
'ComparisonOperator' => 'BETWEEN'
)
),
'ScanIndexForward' => false,
)
);
$count = 1;
foreach ($iterator as $item) {
if($count == 1){
echo "<table>\n";
echo "<tr><th>No.</th><th>日付</th><th>CPU温度</th><th>CPU利用率</th><th>メモリ使用率</th><th>情報</th>";
echo "<th>HashKey</th><th>RangeKey</th><th>HashKey01</th><th>RangeKey02</th><th>RangeKey03</th></tr>";
}
echo "<tr><td>".$count."</td><td>".$item['datetime']['S']."</td><td>".$item['cpu_temp']['S']." ".$item['cpu_unit']['S']."</td>";
echo "<td>".$item['cpu_used']['S']." ".$item['cpu_unit2']['S']."</td><td>".$item['mem_used']['S']." ".$item['mem_unit']['S']."</td>";
echo "<td><a href=\"DataGet.php?p=".$item['HashKey01']['S']."\" target=\"_blank\">情報</a></td>\n";
echo "<td>".$item['HashKey']['S']."</td><td>".$item['RangeKey']['S']."</td>";
echo "<td>".$item['HashKey01']['S']."</td><td>".$item['RangeKey02']['S']."</td><td>".$item['RangeKey03']['S']."</td></tr>";
$count++;
}
if($count > 1) echo "</table>\n";
if($count < 2) echo "<table><tr><td>該当するデータはありませんでした。</td></tr></table>\n";
} catch (DynamoDbException $exc) {
echo "<table><tr><td>エラーが発生しました : ".$exc->getMessage()."</td></tr></table>\n";
}
?>
<br /><br />
<h2>定期画像一覧</h2>
<?php

// Image
try{
$iterator = $dynamo->getIterator('Query', array(
'TableName'     => $dynamoTbl,
'KeyConditions' => array(
'HashKey' => array(
'AttributeValueList' => array(
array('S' => 'D001-PIC-PERI')
),
'ComparisonOperator' => 'EQ'
),
'RangeKey' => array(
'AttributeValueList' => array(
array('S' => "".$symdhims),
array('S' => "".$eymdhims)
),
'ComparisonOperator' => 'BETWEEN'
)
),
'ScanIndexForward' => false,
)
);
$count = 1;
foreach ($iterator as $item) {
if($count == 1){
echo "<table>\n";
echo "<tr><th>No.</th><th>日付</th><th>画像・情報</th>";
echo "<th>HashKey</th><th>RangeKey</th><th>HashKey01</th><th>RangeKey02</th><th>RangeKey03</th></tr>";
}
echo "<tr><td>".$count."</td><td>".$item['datetime']['S']."</td>";
echo "<td><a href=\"ImageData.php?b=".$item['s3backet']['S']."&k=".$item['s3key']['S']."\" target=\"_blank\">画像</a> ";
echo "<a href=\"DataGet.php?p=".$item['HashKey01']['S']."\" target=\"_blank\">情報</a></td>\n";
echo "<td>".$item['HashKey']['S']."</td><td>".$item['RangeKey']['S']."</td>";
echo "<td>".$item['HashKey01']['S']."</td><td>".$item['RangeKey02']['S']."</td><td>".$item['RangeKey03']['S']."</td></tr>";
$count++;
}
if($count > 1) echo "</table>\n";
if($count < 2) echo "<table><tr><td>該当するデータはありませんでした。</td></tr></table>\n";
} catch (DynamoDbException $exc) {
echo "<table><tr><td>エラーが発生しました : ".$exc->getMessage()."</td></tr></table>\n";
}
?>
<br /><br />
<a href="#" onClick="window.close(); return false;">閉じる</a>
</body>
</html>
<code>

AWS の S3 に保管したデータを EC2 の Webサーバから表示するPHPプログラム例です。

試作システム

</code>
<html>
<head>
<title>S3の画像</title>
<style type="text/css">

table {
border-collapse:collapse;
border-spacing:0;
background-color:#ffffff;
empty-cells:show;
}

th {
border-right:1px solid #663300;
border-bottom:1px solid #663300;
color:#ffffff;
background-color:#006400;
padding: 5px 4px;
text-align:center;
}

td {
border-right:1px solid #006400;
border-left:1px solid #006400;
border-top:1px solid #006400;
border-bottom:1px solid #006400;
padding:0.1em 0.3em;
}

.tdback {
background-color:#008000;
color:#ffffff;
}

h2 {
position: relative;
color: #ffffff;
background: #008000;
font-size: 16px;
line-height: 1;
margin: 0px 0px 5px 5px;
padding: 5px 5px 5px 10px;
border-radius: 5px 5px 5px 5px;
}

p {
margin:0 0 15px 5px;
}

a {
text-decoration:underline;
}
a:hover {
color: #ffffff;
background: #008000;
text-decoration:underline;
}

</style>

</head>

<body>
<h2>S3の保管画像</h2>
<?php

ini_set( 'display_errors', 1 );
ini_set('error_reporting', E_ALL);

$bucket = "";
if(isset($_REQUEST['b'])) $bucket = $_REQUEST['b']."";
$key = "";
if(isset($_REQUEST['k'])) $key = $_REQUEST['k']."";

?>
<table>
<tr><th>S3保管先画像ファイル => <?php echo $key; ?></th></tr>
<tr><td>
<?php
echo "<image src=\"Image.php?b=".$bucket."&k=".$key."\" />"
?>
</td></tr>
</table>
<br />
<a href="#" onClick="window.close(); return false;">閉じる</a>
</body>
</html>
<code>

 

</code>
<?php

ini_set( 'display_errors', 1 );
ini_set('error_reporting', E_ALL);

require_once('/usr/share/pear/AWSSDKforPHP/aws.phar');

use Aws\Common\Aws;
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
use Aws\Common\Enum\Region;
use Guzzle\Http\EntityBody;

$aws    = Aws::factory("/usr/share/pear/AWSSDKforPHP/Aws/Common/Resources/aws-config.php");
$s3     = $aws->get('s3');
$bucket = "";
if(isset($_REQUEST['b'])) $bucket = $_REQUEST['b']."";

$key = "";
if(isset($_REQUEST['k'])) $key = $_REQUEST['k']."";

if($bucket != "" && $key != ""){
$result = $s3->getObject(array(
'Bucket' => $bucket,
'Key' => $key
));
header("Content-Type: {$result['ContentType']}");
echo $result['Body'];

/*
$length = $result['ContentLength'];
$result['Body']->rewind();
$data = $result['Body']->read($length);
$filename = end(explode('/', $key));

header('Content-Type: application/octet-stream');
$disposition = 'Content-Disposition: attachment; filename="' . $filename . '"';
header($disposition);
$contentlength = 'Content-Length: ' . $length;
header($contentlength);
echo $data;
*/
}else{
header('HTTP/1.0 404 Not Found');
}
?>
<code>

前へ 目次

広告

About Yukihito Kataoka
@ykataoka

現在コメントは受け付けていません。

%d人のブロガーが「いいね」をつけました。