通过curl向nacos上传配置文件

参考-->nacos配置迁移

Linux下:
先创建一个文件夹,这个文件夹的名称就是你在nacos上的group的名称(一般都是DEFAULT_GROUP),然后把你所要上传的文件放入这个文件夹,在使用zip的命令压缩这个文件,再通过

curl --location --request POST 'http://ip:8848/nacos/v1/cs/configs?import=true&namespace=' \
--form 'policy=OVERWRITE' \
--form 'file=@"zip文件的绝对路径"'

ip为你的nacos的ip
如果发送后返回
请检查你的文件内容是否存在内容为空的文件

通过shell脚本上传nacos配置

#!/bin/bash

if [ $# != 1 ]; then
echo "./a.sh nacosIp"
exit -1
fi

nacosIp=$1
echo "set nacosIp=$nacosIp"
erroe=0
for i in $(ls /mnt/cloudcard/conf/)
do
   text=`cat /mnt/cloudcard/conf/$i`
   echo "cat text=$text"
   result=`curl -X POST "http://$nacosIp:8848/nacos/v1/cs/configs" -d "dataId=$i&group=DEFAULT_GROUP&content=$text&type=yaml"`
   if [ "$result"x == "true"x ]; then

     echo "$result"

   else

    echo "$result"
    let error++

  fi
done

如果你配置文件中存在'&'他只会把他前面的内容上传上去,目前还未找到解决办法(感谢园友的帮助,给了我解决方法,将上面的result改为下面的即可解决)

result=`curl -X POST "http://nacos ip:8848/nacos/v1/cs/configs" \
               -d "group=分组名&type=yaml&tenant=名称空间" \
               -d "dataId=${i}" \
               --data-urlencode "content=${text}"`
posted @ 2022-08-04 09:43  freedomAndWind  阅读(2158)  评论(2编辑  收藏  举报