1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

S3 オブジェクトをプレフィックス・サフィックス両方で検索する

Posted at

s3 でプレフィックス、サフィックス両方を使ってオブジェクトを取得したいとき、
boto3のメソッドはプレフィックスのみでしか検索できなくて少し不便だったので自作の関数を用意して実装しました。

引数suffixに正規表現で検索文字を入れると、ヒットするオブジェクトが配列で返ってくる

import boto3
import re
def get_s3_objects(bucket, prefix, suffix):
    suffix += "$" # 末尾文字を付与
    s3 = boto3.client("s3")
    obj_list = s3.list_objects_v2(Bucket=bucket, Prefix=prefix)["Contents"]
    obj_list = list(filter(lambda x: re.compile(suffix).search(x["Key"]), obj_list))
    return obj_list
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?