次の例は、標準 SQL クエリを使用して S3 メタデータテーブルからさまざまなタイプの情報を取得する方法を示しています。
これらの例を使用する際は、次の点に注意してください。
-
例は、Amazon Athena で動作するように記述されています。別のクエリエンジンで動作するためには、例を変更する必要がある場合があります。
-
クエリを最適化する方法を理解していることを確認してください。
-
を、メタデータテーブルを保存する S3 テーブルバケットの名前に置き換えます。amzn-s3-demo-bucket
-
を、クエリするメタデータテーブルの名前に置き換えます。my_metadata_table
-
サポートされている列の完全なリストについては、「S3 メタデータテーブルスキーマ」を参照してください。
ファイル拡張子によるオブジェクトの検索
次のクエリは、特定のファイル拡張子 (この場合は .jpg
) を持つオブジェクトを返します。
SELECT key FROM "s3tablescatalog/amzn-s3-demo-bucket
"."aws_s3_metadata"."my_metadata_table
"
WHERE key LIKE '%.jpg'
AND record_type = 'CREATE'
オブジェクトの削除の一覧表示
次のクエリは、リクエストを行った AWS アカウント ID または AWS サービスプリンシパルを含むオブジェクト削除イベントを返します。
SELECT DISTINCT bucket, key, sequence_number, record_type, record_timestamp, requester, source_ip_address, version_id
FROM "s3tablescatalog/amzn-s3-demo-bucket
"."aws_s3_metadata"."my_metadata_table
"
WHERE record_type = 'DELETE';
オブジェクトで使用される AWS KMS 暗号化キーの一覧表示
次のクエリは、オブジェクトを暗号化する AWS Key Management Service (AWS KMS) キーの ARN を返します。
SELECT DISTINCT kms_key_arn
FROM "s3tablescatalog/amzn-s3-demo-bucket
"."aws_s3_metadata"."my_metadata_table
";
KMS キーを使用しないオブジェクトの一覧表示
次のクエリは、AWS KMS キーで暗号化されていないオブジェクトを返します。
SELECT DISTINCT kms_key_arn
FROM "s3tablescatalog/amzn-s3-demo-bucket
"."aws_s3_metadata"."my_metadata_table
"
WHERE encryption_status NOT IN ('SSE-KMS', 'DSSE-KMS')
AND record_type = 'CREATE';
Amazon Bedrock が提供するメタデータの表示
一部の AWS サービス (Amazon Bedrock など) は、Amazon S3 にオブジェクトをアップロードします。これらのサービスによって提供されるオブジェクトメタデータをクエリできます。例えば、次のクエリには、Amazon Bedrock によって汎用バケットにアップロードされたオブジェクトがあるかどうかを判断する user_metadata
列が含まれています。
SELECT DISTINCT bucket, key, sequence_number, record_type, record_timestamp, user_metadata
FROM "s3tablescatalog/amzn-s3-demo-bucket
"."aws_s3_metadata"."my_metadata_table
"
WHERE record_type = 'CREATE'
AND user_metadata['content-source'] = 'AmazonBedrock';
Amazon Bedrock がバケットにオブジェクトをアップロードした場合、user_metadata
列には、クエリ結果のオブジェクトに関連付けられた次のメタデータが表示されます。
user_metadata
{content-additional-params -> requestid="CVK8FWYRW0M9JW65", signedContentSHA384="38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b", content-model-id -> bedrock-model-arn, content-source -> AmazonBedrock}
オブジェクトの現状の把握
次のクエリは、オブジェクトの現在の状態を判断するのに役立ちます。クエリは、各オブジェクトの最新バージョンを識別し、削除されたオブジェクトを除外し、シーケンス番号に基づいて各オブジェクトの最新バージョンをマークします。結果は、bucket
、key
、および sequence_number
列で順序付けられます。
WITH records_of_interest as (
-- Start with a query that can narrow down the records of interest.
SELECT * from "s3tablescatalog/amzn-s3-demo-bucket
"."aws_s3_metadata"."my_metadata_table
"
),
version_stacks as (
SELECT *,
-- Introduce a column called 'next_sequence_number', which is the next larger
-- sequence_number for the same key version_id in sorted order.
LEAD(sequence_number, 1) over (partition by (bucket, key, coalesce(version_id, '')) order by sequence_number ASC) as next_sequence_number
from records_of_interest
),
-- Pick the 'tip' of each version stack triple: (bucket, key, version_id).
-- The tip of the version stack is the row of that triple with the largest sequencer.
-- Selecting only the tip filters out any row duplicates.
-- This isn't typical, but some events can be delivered more than once to the table
-- and include rows that might no longer exist in the bucket (since the
-- table contains rows for both extant and extinct objects).
-- In the next subquery, eliminate the rows that contain deleted objects.
current_versions as (
SELECT * from version_stacks where next_sequence_number is NULL
),
-- Eliminate the rows that are extinct from the bucket by filtering with
-- record_type. An object version has been deleted from the bucket if its tip is
-- record_type==DELETE.
existing_current_versions as (
SELECT * from current_versions where not (record_type = 'DELETE' and is_delete_marker = FALSE)
),
-- Optionally, to determine which of several object versions is the 'latest',
-- you can compare their sequence numbers. A version_id is the latest if its
-- tip's sequencer is the largest among all other tips in the same key.
with_is_latest as (
SELECT *,
-- Determine if the sequence_number of this row is the same as the largest sequencer for the key that still exists.
sequence_number = (MAX(sequence_number) over (partition by (bucket, key))) as is_latest_version
FROM existing_current_versions
)
SELECT * from with_is_latest
ORDER BY bucket, key, sequence_number;