Last active
October 12, 2023 11:52
-
-
Save davidejones/e68479398d3c17d0e81fe6262f16304a to your computer and use it in GitHub Desktop.
Update s3 metadata for an object and preserve existing content meta
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
BUCKET="my-bucket" | |
KEY="/foo/bar/baz.xml" | |
FILENAME=$(basename $KEY) | |
# | |
# { | |
# “ContentType”: “text/xml; charset=utf-8", | |
# “CacheControl”: “public, must-revalidate, proxy-revalidate, max-age=0", | |
# “ContentEncoding”: “gzip” | |
# } | |
# | |
EXISTING_JSON_DATA=$(aws s3api head-object --bucket "${BUCKET}" --key "${KEY}" | jq '. | {ContentType,CacheControl,ContentEncoding}') | |
# | |
# --content-type | |
# text/xml; charset=utf-8" | |
# --cache-control | |
# public, must-revalidate, proxy-revalidate, max-age=0 | |
# --content-encoding | |
# gzip | |
# | |
IFS=$'\n' | |
PARAM_KV_DATA=($(echo $EXISTING_JSON_DATA | jq -r 'to_entries|map("--\(.key | [splits("(?=[A-Z])")] | map(select(. != "") | ascii_downcase) | join("-"))\n\(.value)")|.[]')) | |
# update meta with new data and retain existing we retrieved | |
aws s3api put-object \ | |
--bucket "${BUCKET}" \ | |
--key "${KEY}" \ | |
--content-disposition "attachment;filename=\"${FILENAME}\"" \ | |
${PARAM_KV_DATA[@]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment