Cloudinary の Named Transformation の派生アセット数を調べるには

Cloudinary にて、特定の Named Transformation に紐づくすべての派生アセット(derived assets)を確認する方法をご紹介します。
2024.03.04

今回のブログでは、特定の Named Transformation を使う派生アセット(すでに生成された変換画像/動画)の件数や対象を調べる方法をご紹介します。

Cloudinary において、派生アセットのうち特定の変換に絞り込んで確認するには、以前のブログの「特定変換を使う派生画像」でやっているように API の transformation メソッドを使用します。

res = cloudinary.api.transformation("t_cm_wm_colors", max_results=500)
dvs = res['derived']

# [{'bytes': 68971,
#   'format': 'jpg',
#   'id': '7b11e699fbb0a291d052646969bbe001',
#   'public_id': 'ito-test/travel/img4',
#   'resource_type': 'image',
#   'secure_url': 'https://res.cloudinary.com/CLOUD-NAME/image/upload/t_cm_wm_colors/v1626788147/ito-test/travel/img4.jpg',
#   'type': 'upload',
#   'url': 'http://res.cloudinary.com/CLOUD-NAME/image/upload/t_cm_wm_colors/v1626788147/ito-test/travel/img4.jpg'}]

ただし、このメソッドはパラメータで指定した変換そのものしか検索してくれません。つまり、この例では「t_cm_wm_colors」のみで、これを含む変換(t_cm_wm_colors,q_auto,f_auto など)の結果は返してくれません。

やり方の流れ

特定の変換を使うすべての派生アセットを調べるには、もう少し工夫する必要があります。

  1. 環境内でこれまでに使われたすべての変換パラメータを取得する(例「f_auto,q_auto」「t_cm_wm_colors」「t_cm_wm_colors,q_auto,f_auto」等々)
  2. 1のうち、特定の変換およびそれを含むものだけを抽出する(例「t_cm_wm_colors」「t_cm_wm_colors,q_auto,f_auto」等)
  3. 抽出した変換パラメータそれぞれに対して、派生アセットを取得(前述の transformation メソッドを使う)し、リストにまとめる

スクリプト例

以下、 Python での実行例です。

※ Cloudinary ライブラリやクレデンシャル等の基本の Python 操作はこのブログをご参考ください。

# 環境内で使われたすべての変換パラメータを取得する
res = cloudinary.api.transformations(named=False, max_results=500)
params = [t['name'] for t in res['transformations']]
while res.get("next_cursor"):
    res = cloudinary.api.transformations(max_results=500, next_cursor=res['next_cursor'])
    params += [t['name'] for t in res['transformations']]

# 対象の Named Transformation を含む変換パラメータだけを抽出する
named_transformation = "t_cm_wm_colors"
filtered_params = [param for param in params if re.search(r'(,|/|^)' + re.escape(trn_name) + r'($|,|/)', param)]
filtered_params.append(trn_name)

# 抽出した変換パラメータに対して、それぞれ派生アセットを取得し、リストにまとめる
dvs_list = {}
for trn_param in filtered_params:
    # 指定した変換(そのもの)の派生アセットを取得
    res = cloudinary.api.transformation(trn_param, max_results=500)
    dvs = res['derived']
    while res.get("next_cursor"):
        res = cloudinary.api.transformation(trn_name, max_results=500, next_cursor=res['next_cursor'])
        dvs += res['derived']
    # リストに追加
    dvs_list[trn_param] = dvs

解説

named=False のオプションで Named Transformation ではないもののみに制限します。(例:「t_cm_wm_colors」は該当せず「t_cm_wm_colors,q_auto,f_auto」は結果に該当)

max_results / next_cursor は Cloudinary の API でよく使われるパラメータです。max_results で出力結果の最大件数を指定し、該当結果がそれを超える場合にレスポンスの next_cursor にこの値が返されるので、次回実行時にパラメータでこれを指定することで続きの結果を取得することができます。

res = cloudinary.api.transformations(named=False, max_results=500)
params = [t['name'] for t in res['transformations']]
while res.get("next_cursor"):
    res = cloudinary.api.transformations(max_results=500, next_cursor=res['next_cursor'])
    params += [t['name'] for t in res['transformations']]

※ 異なる変換を使う大規模環境では大量の件数となるため、ループ数が増えると Admin API のレートリミットに達してエラーとなる可能性があります。

  • 取得した全変換パラメータのうち、対象 Named Transformation を含むものだけを抽出

対象とする Named Transformation を含む変換パラメータのみを抽出しますが、 例えば「t_cm_wm_colors_hoge」など部分一致してしまう別の変換が存在する場合にそれが該当しないよう、正規表現で絞り込みます。

named_transformation = "t_cm_wm_colors"
filtered_params = [param for param in params if re.search(r'(,|/|^)' + re.escape(trn_name) + r'($|,|/)', param)]
filtered_params.append(trn_name)  ## 対象 Named Transformation そのものも追加

今回取得した値は次のようなものが入っています。

>>> print(filtered_params)
['w_200,t_cm_wm_colors', 't_cm_wm_colors,w_200', 't_cm_wm_colors']
  • 抽出した各変換パラメータに紐づく派生アセットを取得し、リスト化

紐づく派生アセットを取得する transformation メソッドを、前項で抽出した変換パラメータにそれぞれ実行します。

dvs_list = {}
for trn_param in filtered_params:
    # 特定の変換(そのもの)の派生アセットを取得
    res = cloudinary.api.transformation(trn_param, max_results=500)
    dvs = res['derived']
    while res.get("next_cursor"):
        res = cloudinary.api.transformation(trn_name, max_results=500, next_cursor=res['next_cursor'])
        dvs += res['derived']
    dvs_list[trn_param] = dvs

これで、「t_cm_wm_colors」単体だけでなく、「t_cm_wm_colors」を使ったすべての変換の派生アセットが取得できました。

今回の例では、3つのパラメータに計5件の派生アセットがあることが分かります。

>>> len(dvs_list)
3
>>> sum(len(dvs) for dvs in dvs_list.values())
5
>>> pprint(dvs_list)
{'t_cm_wm_colors': [{'bytes': 68971,
                     'format': 'jpg',
                     'id': '7b11e699fbb0a291d052646969bbe001',
                     'public_id': 'ito-test/travel/img4',
                     'resource_type': 'image',
                     'secure_url': 'https://res.cloudinary.com/demo/image/upload/t_cm_wm_colors/v1626788147/ito-test/travel/img4.jpg',
                     'type': 'upload',
                     'url': 'http://res.cloudinary.com/demo/image/upload/t_cm_wm_colors/v1626788147/ito-test/travel/img4.jpg'},
                    {'bytes': 372403,
                     'format': 'jpg',
                     'id': '77420cd56903954bc3525db0fe8424f3',
                     'public_id': 'ito-test/bikee',
                     'resource_type': 'image',
                     'secure_url': 'https://res.cloudinary.com/demo/image/upload/t_cm_wm_colors/v1622648607/ito-test/bikee.jpg',
                     'type': 'upload',
                     'url': 'http://res.cloudinary.com/demo/image/upload/t_cm_wm_colors/v1622648607/ito-test/bikee.jpg'},
                    {...(中略)...}],
 't_cm_wm_colors,w_200': [{'bytes': 5662,
                           'format': 'jpg',
                           'id': '03c23d8ca7511d5e3c60487cdc700173',
                           'public_id': 'ito-test/travel/img4',
                           'resource_type': 'image',
                           'secure_url': 'https://res.cloudinary.com/demo/image/upload/t_cm_wm_colors,w_200/v1626788147/ito-test/travel/img4.jpg',
                           'type': 'upload',
                           'url': 'http://res.cloudinary.com/demo/image/upload/t_cm_wm_colors,w_200/v1626788147/ito-test/travel/img4.jpg'}],
 'w_200,t_cm_wm_colors': [{...中略...}]}

Named Transformation の定義を更新する場合、すべての紐づく派生アセットが1000件以上かどうかで挙動が変わるので、件数を調べたい場合に今回のスクリプトが使えます。

▼ Named Transformation を更新するには
https://support.cloudinary.com/hc/en-us/articles/202521272-How-can-I-update-a-named-transformation

その他、検証環境や何か不具合があった場合に紐づくアセットを調べたい場合にも、こちらを参考に応用してもらえれば幸いです。


クラスメソッドはCloudinaryのパートナーとして導入のお手伝いをさせていただいています。お気軽にこちらからお問い合わせください。こちらから無料でもサインアップいただけます。