google cloud commands | add public permissions to google cloud storage | test command arguments | Search

This Bash script processes HTML files in a specified directory, sets their content type to "text/html; charset=utf-8", and uploads them to Google Cloud Storage.

Run example

npm run import -- "copy html to google cloud storage"

copy html to google cloud storage

output="../.output"; \
if [[ -n $1 ]]; \
    then output=$1; \
fi; \
for file in $(find "$output" -type f -name "*.html"); \
do \
    f=${file#"$output"/*} && \
    gsutil setmeta -h "Content-Type:text/html; charset=utf-8" "$2/$f" && \
    gsutil -h "Content-Type:text/html; charset=utf-8" cp "$file" "$2/${f%.*}"; \
done

What the code could have been:

```markdown
## Set Output Path
# Define default output path
DEFAULT_OUTPUT_PATH="../.output"

## Process Command Line Arguments
# Check if output path is provided as an argument
if [[ -z ${1+x} ]]; then
    OUTPUT_PATH=$DEFAULT_OUTPUT_PATH
else
    # If provided, validate and set the output path
    if [[ -e $1 ]]; then
        OUTPUT_PATH=$1
    else
        echo "Error: Output path '$1' does not exist."
        exit 1
    fi
fi

## Set Bucket and File Path
# Define default bucket and file path
DEFAULT_BUCKET="my-bucket"
DEFAULT_FILE_PATH="/"

## Get Bucket and File Path from Arguments
if [[ ${2+x} ]]; then
    BUCKET=${2%%/*}
    FILE_PATH=${2#*/}
else
    BUCKET=$DEFAULT_BUCKET
    FILE_PATH=$DEFAULT_FILE_PATH
fi

## Copy and Set Metadata for HTML Files
# Find all HTML files in the output path
for file in $(find "$OUTPUT_PATH" -type f -name "*.html"); do
    # Extract file name without path
    filename=${file##*/}
    # Use gsutil to copy and set metadata for the file
    gsutil setmeta -h "Content-Type:text/html; charset=utf-8" "$BUCKET/$FILE_PATH/$filename" && \
    gsutil -h "Content-Type:text/html; charset=utf-8" cp "$file" "$BUCKET/$FILE_PATH/${filename%.*}"
done
```

This Bash script sets the content type of HTML files in a directory to "text/html; charset=utf-8" and uploads them to Google Cloud Storage.

Here's a breakdown:

  1. output="../.output";:

  2. if [[ -n $1 ]]; then output=$1; fi;:

  3. for file in $(find "$output" -type f -name "*.html"); do ... done:

  4. f=${file#"$output"/*} && ...:

  5. gsutil setmeta -h "Content-Type:text/html; charset=utf-8" "$2/$f" && ...:

  6. gsutil -h "Content-Type:text/html; charset=utf-8" cp "$file" "$2/${f%.*}":

Let me know if you have any more questions!