"Will code for travel"

Search technical info
Load gzip from GCS as JSON text
Written: 2024-09-25 18:05:36 Last update: 2024-09-30 20:41:04

GCS (Google Cloud Storage) is a good free CDN to store static web resources, in one of my use case I use it to store large JSON text files that will be downloaded by client web.

Because the JSON text files' size are large, therefore I need to reduce the files' size to faster the download and reduce internet data usage for smartphone. Luckily there is a way to do this for JSON text file stored in GCS, below is the simple steps.

  1. Compress the JSON text file using 'gzip', only gzip is allowed (not tar nor zip nor other compression format), because gzip is a "standard" compression that is supported by GCS and Ajax call.
    Example command of how to compress a JSON file and create different filename output.
    gzip -9 -c obf2-en-amp_2024-09-30_22-35-03.json > obf2-en-amp_2024-09-30_22-35-03.json.gz
    1-how_to_compress_JSON_file_using_gzip
  2. Upload the gzip-compressed files to GCS (because it is rarely updated so I upload them to GCS in GCS's console drag-and-drop). 2-uploaded_file_list_onto_GCS_content-type_is_automatically_defined
  3. Change the metadata of each file, by default GCS will automatically recognize them and set the "Content-Type" as "application/gzip", so we need to change the "Content-Type" = "application/json" and "Content-Encoding" = "gzip" 3-change_the_Content-Type_to_application-json_and_add_Content-Encoding_to_gzip

That's it, if everything is configured properly then we can try to use Chrome/Firefox browser to directly open the JSON file by entering its public URL, we can see the JSON text content properly instead of gzip binary content.

4-test_in_Firefox_browser_to_download_the_gz_file_and_automatically_decompressed_to_JSON_text
Search more info