Created
July 14, 2022 15:51
-
-
Save iitenkida7/1622ee9bd06dd7b06461f5d329931cd4 to your computer and use it in GitHub Desktop.
awsS3Select 使って あるjsonファイルを取得する例
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Libraries; | |
use Aws\Credentials\Credentials; | |
use Aws\S3\S3Client; | |
class AwsS3Select | |
{ | |
protected S3Client $client; | |
protected string $bucket; | |
protected string $s3Key; | |
public function __construct() | |
{ | |
$this->bucket = config('filesystems.disks.s3.bucket'); | |
$params = [ | |
'version' => 'latest', | |
'region' => 'ap-northeast-1', | |
'credentials' => new Credentials( | |
config('filesystems.disks.s3.key'), | |
config('filesystems.disks.s3.secret') | |
), | |
]; | |
$this->client = new S3Client($params); | |
} | |
public function setVideoSlug(string $videoSlug): self | |
{ | |
$this->s3Key = 'dev/' . $videoSlug . '.json.gz'; | |
return $this; | |
} | |
public function getVideo(): array | |
{ | |
$query = 'SELECT | |
s.author.id as author_id, | |
s.message_type, | |
s.money.currency as money_currency, | |
s.money.amount as money_amount, | |
s."timestamp" / 1000 as posted_at, | |
s.time_in_seconds | |
FROM s3object s'; | |
return $this->getS3Select($query); | |
} | |
public function getSuperChatSum(string $currency = 'JPY'): int | |
{ | |
$query = "SELECT | |
sum(s.money.amount) as total_amount | |
FROM s3object s | |
WHERE s.money is not null | |
AND s.money.currency = '{$currency}'"; | |
$total_amount = $this->getS3Select($query)[0]['total_amount']; | |
return $total_amount ?? 0; | |
} | |
private function getS3Select(string $query) | |
{ | |
ini_set('memory_limit', '512M'); | |
$result = $this->client->selectObjectContent([ | |
'Bucket' => $this->bucket, | |
'Key' => $this->s3Key, | |
'ExpressionType' => 'SQL', | |
'Expression' => $query, | |
'InputSerialization' => [ | |
'JSON' => [ | |
'Type' => 'Lines', | |
], | |
'CompressionType' => 'GZIP', | |
], | |
'OutputSerialization' => [ | |
'JSON' => [ | |
'RecordDelimiter' => '\n', | |
], | |
], | |
]); | |
$payload = ''; | |
foreach ($result['Payload'] as $event) { | |
if (isset($event['Records'])) { | |
$payload = $payload . ((string) $event['Records']['Payload']); | |
} | |
} | |
$jsonItems = explode("\n", $payload); | |
$items = []; | |
foreach ($jsonItems as $item) { | |
if ($item) { | |
$items[] = json_decode($item, true); | |
} | |
} | |
return $items; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment