通过重命名将本地文件夹中的文件复制到gcs

时间:2019-04-15 14:45:13

标签: bash google-cloud-platform

我编写了以下脚本,其目的是在gcs中指定文件夹下的给定路径中复制文件:

 #!/usr/bin/env bash

set -e
set -u

SOURCE=$1
CI_PROJECT_DIR="/home/user/project-ingestion"
DICT_PATH="${CI_PROJECT_DIR}/src/main/resources/dictionaries/$SOURCE"
DICT_BUCKETS="dict-bucket"

dictionaries="dictionaries"
DATE=`date '+%Y_%m_%d'`
for dictionary in $DICT_PATH/*.json; do
    FOLDER=${dictionary//+(*\/|.*)}
    TARGET="gs://$DICT_BUCKETS/$dictionaries/$SOURCE/$FOLDER/$FOLDER'_'$DATE.json"
    gsutil cp $dictionary  $TARGET
done

如果我的json文件在下面,

/home/user/project-ingestion/src/main/resources/dictionaries/source/factories.json

我要将其复制到

gs://dict-bucket/factories/factories_2019_05_04.json

我编写的脚本将整个字典树复制到给定的目标。知道如何解决此问题

1 个答案:

答案 0 :(得分:0)

看起来问题出在以下几行中,

# parent path of $dictionary is not truncated
FOLDER=${dictionary//+(*\/|.*)}

# TARGET will contain single quotes
TARGET="gs://$DICT_BUCKETS/$dictionaries/$SOURCE/$FOLDER/$FOLDER'_'$DATE.json"

我不确定您将如何精确地构建目标路径,但是以下操作可能会有所帮助(在循环块内部)。

filename=$(basename $dictionary)    # e.g. factories.json
file_prefix=${filename%%.*}         # e.g. factories (remove everything after first period)
TARGET="gs://fixed_path/${file_prefix}/${file_prefix}_${DATE}.json" # change this as per your need