Skip to content

Upload media with the CLI

With hakocms-cli media upload, you can upload many images and files at once. Uploaded media appears in [メディア](Media) in the admin.

  • You are logged in with hakocms-cli and have set the space (Log in with the CLI)
  • You have a role that can upload media (owner, admin, or editor)
  • There is a limit on the size of a single file (Usage limits)

Pass the file names in a list. You can also use the shell’s *.

ターミナルウィンドウ
hakocms-cli media upload photos/*.png

The CLI sends the files one at a time and shows progress after each one. At the end, it shows a list of the uploaded media.

uploaded 1/2: mountain.png
uploaded 2/2: sea.png
ID FILE NAME KIND URL
1edtz5vle5gg mountain.png image https://<hostname>.hakocms.net/media/1edtz5vle5gg/mountain.png
k323zqmuvvvs sea.png image https://<hostname>.hakocms.net/media/k323zqmuvvvs/sea.png
  • If an upload fails partway, the CLI stops there. The files sent before that remain. Send again starting from the file that failed
  • If you send the same file again, it is added as a separate media item. Before you send again, check in the admin or with media list that the file is not already uploaded

When you upload, you can add alt text and tags. Both are added to every file you pass. Pass tags as a JSON array of strings.

ターミナルウィンドウ
hakocms-cli media upload photos/*.png --alt "旅行の写真" --tags '["travel","2026"]'
ターミナルウィンドウ
hakocms-cli media update <media ID> --alt 海の写真 --tags '["sea","2026"]'

--tags replaces the current tags instead of adding to them.

ターミナルウィンドウ
hakocms-cli media list --tag 2026 --all # By tag
hakocms-cli media list --query sea # By file name, alt text, or tag
hakocms-cli media list --kind file # Only files that are not images

Put a media URL in an image field in the form {"url": "<URL>"}. Use the URL shown when you upload.

This example is for a content type photos with an image field image. For each file, it uploads the file, then creates an entry that contains that image.

ターミナルウィンドウ
for f in photos/*.png; do
url=$(hakocms-cli media upload "$f" --jq '.[0].url')
hakocms-cli entries create -t photos \
--set "title=$(basename "$f" .png)" \
--set "image={\"url\":\"$url\"}" --jq .id
done

Hako CMS fills in the width and height from the media.

{ "url": "https://<hostname>.hakocms.net/media/pdegl1xwddch/mountain.png",
"width": 1200, "height": 800 }

You cannot use media from another space. For a multiple-image field (mediaList), put an array of the same shape.

Before you delete media, check which entries use it.

ターミナルウィンドウ
hakocms-cli media usages <media ID>

The content types and IDs of the entries that use it are shown. If it is not used anywhere, [] is shown.

ターミナルウィンドウ
hakocms-cli media delete <media ID>

The CLI asks you to confirm the deletion. To delete from a script, add --force. The URL of deleted media stops working immediately. This cannot be undone.

You cannot delete media that entries use. First check the entries that use it with media usages, and remove the media from those entries.

In [メディア](Media) in the admin, check that the uploaded files are listed and that they have the alt text and tags.