upload.sh 3.61 KB
Newer Older
fuyanbin's avatar
fuyanbin committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/bin/bash

usage() {
    echo "Usage: $0 -f | --file <local file path>
                    -d | --directory <remote directory>
                    -F | --remote-file <remote file name>
                    -b | --bucket <bucket name>
                    -k | --key <ali key>
                    -D | --domain <domain>
                    -e | --endpoint <endpoint>
                    -R | --remove remove remote file
                    --noprogress
                    --help"
    exit 1
}

LOCAL_FILE=unset
ALI_KEY_FILE=""
ALI_BUCKET_NAME='ipfsbit-cdn'
ALI_END_POINT='oss-cn-beijing.aliyuncs.com'
ALI_DIRECOTRY=unset
ALI_REMOTE_FILENAME=unset
REMOVE=unset
NOPROGRESS=""
declare -A DOMAIN_MAP
declare -A BUCKET_MAP

DOMAIN_MAP["https://cdn.linkfog.cn"]="ipfsbit-cdn"
DOMAIN_MAP["https://test-cdn.linkfog.cn"]="ipfsbit-test-bucket"
DOMAIN_MAP["https://cdn2.linkfog.cn"]="agent-ipbit"
DOMAIN_MAP["https://saas-cdn.linkfog.cn"]="saas-linkfog-bucket"

BUCKET_MAP["ipfsbit-cdn"]="https://cdn.linkfog.cn"
BUCKET_MAP["ipfsbit-test-bucket"]="https://test-cdn.linkfog.cn"
BUCKET_MAP["agent-ipbit"]="https://cdn2.linkfog.cn"
BUCKET_MAP["saas-linkfog-bucket"]="https://saas-cdn.linkfog.cn"

PARSED_ARUGMENTS=$(getopt -a -n "$0" -o i:b:k:D:e:d:F:R --long help,file:,bucket:,key:,domain:,endpoint:,directory:,remote-file:,remove,noprogress -- "$@")
eval set -- "$PARSED_ARUGMENTS"
while :
do
    case "$1" in
        -f | --file) LOCAL_FILE="$2"; shift 2 ;;
        -b | --bucket) ALI_BUCKET_NAME="$2"; shift 2 ;;
        -k | --key) ALI_KEY_FILE="$2"; shift 2 ;;
        -D | --domain) ALI_DOMAIN="$2"; shift 2 ;;
        -e | --endpoint) ALI_END_POINT="$2"; shift 2 ;;
        -d | --directory) ALI_DIRECOTRY="$2"; shift 2 ;;
        -F | --remote-file) ALI_REMOTE_FILENAME="$2"; shift 2 ;;
        -R | --remove) REMOVE="true"; shift 1;;
        --noprogress) NOPROGRESS="true"; shift 1;;
        --help) usage ;;
        --) shift; break;;
        *) usage ;;
    esac
done

if [[ -n "$ALI_DOMAIN" ]]
then
    if ! echo $ALI_DOMAIN | grep -q "^https://" 
    then
        ALI_DOMAIN="https://${ALI_DOMAIN}"
    fi
    ALI_BUCKET_NAME=${DOMAIN_MAP[$ALI_DOMAIN]}
elif [[ -n "$ALI_BUCKET_NAME" ]]
then
    ALI_DOMAIN=${BUCKET_MAP[$ALI_BUCKET_NAME]}
    [[ -z "$ALI_DOMAIN" ]] && ALI_DOMAIN="https://${ALI_BUCKET_NAME}.oss-cn-beijing.aliyuncs.com"
fi

if [[ -z "$ALI_DOMAIN" ]] || [[ -z "$ALI_BUCKET_NAME" ]]
then
    >&2 echo "域名或bucket 名字错误。需要用 --domain 传递cdn域名参数 或用 --bucket 传递bucket 名字"
    >&2 echo "支持以下域名或bucket:"
    for item in ${DOMAIN_MAP[@]}
    do
        >&2 echo ${BUCKET_MAP[$item]} $item
    done
    exit 1
fi

if [[ "$ALI_KEY_FILE" != "" ]]
then
    ALI_KEY_ID="$(cat $ALI_KEY_FILE | cut -d',' -f1)"
    ALI_KEY_SECRET="$(cat $ALI_KEY_FILE | cut -d',' -f2)"
else
    ALI_KEY_ID="LTAI4FgkFhkJqvSjuv6Qo5eD"
    ALI_KEY_SECRET="eu3kMJCVg90NP3hcCUy5aDE0YpAkBM"
fi

if [[ "$ALI_REMOTE_FILENAME" != "unset" ]]
then
    ALI_REMOTE_FILENAME="$ALI_DIRECOTRY/$ALI_REMOTE_FILENAME"
else
    ALI_REMOTE_FILENAME="$ALI_DIRECOTRY/""$(basename $LOCAL_FILE)"
fi
if [[ "$REMOVE" == "unset" ]]
then
    if [[ -n "$NOPROGRESS" ]]
    then
        args="--noprogress"
    fi
    upload -k "$ALI_KEY_ID" \
        -s "$ALI_KEY_SECRET" \
        -b "$ALI_BUCKET_NAME" \
        -e "$ALI_END_POINT" \
        -r "$ALI_REMOTE_FILENAME" \
        -f "$LOCAL_FILE" \
        --domain "$ALI_DOMAIN" $args
    printf "\n\n"
    printf "%s/%s\n" $ALI_DOMAIN $ALI_REMOTE_FILENAME
else
    upload -k "$ALI_KEY_ID" \
        -s "$ALI_KEY_SECRET" \
        -b "$ALI_BUCKET_NAME" \
        -e "$ALI_END_POINT" \
        -r "$ALI_REMOTE_FILENAME" \
        -d
fi