skip to Main Content

I have an s3 route and I have to split it in bucket, prefix and key using JQ.

"s3://bucket/folder1/folder2/folder3/key.txt" ->

{
  bucket: bucket,
  prefix: folder1/folder2/folder3,
  key: key.txt
}

2

Answers


  1. Quick / manual solution:

    If you split on both // and / you can use $foo[0] to get the bucket, use $foo[-1] for the key (last index) and join the rest (1:-1) to get the prefix.

    split("//")[1] | split("/") | {
        bucket: .[0],
        prefix: .[-1],
        key: .[1:-1] | join("/")
    }
    

    Demo

    Login or Signup to reply.
  2. Using regex capturing should do the trick while addressing any concerns regarding the use of split; e.g. with your input

    capture("^[^:]*://(?<bucket>[^/]+)/(?<prefix>.*)/(?<key>[^/]+)$")
    

    produces valid JSON:

    {
      "bucket": "bucket",
      "prefix": "folder1/folder2/folder3",
      "key": "key.txt"
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search