実現したいこと
cURLコマンドを実行し、OCI Object Storageにファイルをアップロードしたい。
■実行コマンド
所々「*」でマスクしています。
curl "" "--data-binary @test.txt" -X PUT -sS https://objectstorage.ap-tokyo-1.oraclecloud.com/n/*******/b/bucket-********/o/test1.txt "-H date: Sat, 21 Nov 2020 01:06:31 GMT -H x-content-sha256: 47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU= -H content-type: application/json -H content-length: 0" -H "Authorization: Signature version="1",keyId="ocid1.tenancy.oc1..aaaaaaaa******/ocid1.user.oc1..aaaaaaaa******/:::::::::::::::",algorithm="rsa-sha256",headers="(request-target) date host x-content-sha256 content-type content-length",signature="K8HuQ5*******TyrshdR3LmWUw==""
発生している問題・エラーメッセージ
curl: option --data-binary @test.txt: is unknown curl: try 'curl --help' or 'curl --manual' for more information
該当のソースコード
Bash
1 2 3# Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. 4# This software is dual-licensed to you under the Universal Permissive License (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or 5# Apache License 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose either license. 6# 7# Version: 1.0.3 8# Usage: 9# oci-curl <host> <method> [file-to-send-as-body] <request-target> [extra-curl-args] 10# 11# ex: 12# oci-curl iaas.us-ashburn-1.oraclecloud.com get "/20160918/instances?compartmentId=some-compartment-ocid" 13# oci-curl iaas.us-ashburn-1.oraclecloud.com post ./request.json "/20160918/vcns" 14 15# If your key file has a passphrase, please set the env variable OCI_CURL_PRIVATE_KEY_PASSPHRASE with the 16# passphrase before running the script 17 18function oci-curl { 19 # TODO: update these values to your own 20 local tenancyId="ocid1.tenancy.oc1..<unique_id>"; 21 local authUserId="ocid1.user.oc1..<unique_id>"; 22 local keyFingerprint="<your_fingerprint>"; 23 local privateKeyPath="/your/path_here/.oci/oci_api_key.pem"; 24 25 local alg=rsa-sha256 26 local sigVersion="1" 27 local now="$(LC_ALL=C \date -u "+%a, %d %h %Y %H:%M:%S GMT")" 28 local host=$1 29 local method=$2 30 local extra_args 31 local keyId="$tenancyId/$authUserId/$keyFingerprint" 32 33 case $method in 34 35 "get" | "GET") 36 local target=$3 37 extra_args=("${@: 4}") 38 local curl_method="GET"; 39 local request_method="get"; 40 ;; 41 42 "delete" | "DELETE") 43 local target=$3 44 extra_args=("${@: 4}") 45 local curl_method="DELETE"; 46 local request_method="delete"; 47 ;; 48 49 "head" | "HEAD") 50 local target=$3 51 extra_args=("--head" "${@: 4}") 52 local curl_method="HEAD"; 53 local request_method="head"; 54 ;; 55 56 "post" | "POST") 57 local body=$3 58 local target=$4 59 extra_args=("${@: 5}") 60 local curl_method="POST"; 61 local request_method="post"; 62 local content_sha256="$(openssl dgst -binary -sha256 < $body | openssl enc -e -base64)"; 63 local content_type="application/json"; 64 local content_length="$(wc -c < $body | xargs)"; 65 ;; 66 67 "put" | "PUT") 68 local body=$3 69 local target=$4 70 extra_args=("${@: 5}") 71 local curl_method="PUT" 72 local request_method="put" 73 local content_sha256="$(openssl dgst -binary -sha256 < $body | openssl enc -e -base64)"; 74 local content_type="application/json"; 75 local content_length="$(wc -c < $body | xargs)"; 76 ;; 77 78 *) echo "invalid method"; return;; 79esac 80 81# This line will url encode all special characters in the request target except "/", "?", "=", and "&", since those characters are used 82# in the request target to indicate path and query string structure. If you need to encode any of "/", "?", "=", or "&", such as when 83# used as part of a path value or query string key or value, you will need to do that yourself in the request target you pass in. 84 85local escaped_target="$(echo $( rawurlencode "$target" ))" 86local request_target="(request-target): $request_method $escaped_target" 87local date_header="date: $now" 88local host_header="host: $host" 89local content_sha256_header="x-content-sha256: $content_sha256" 90local content_type_header="content-type: $content_type" 91local content_length_header="content-length: $content_length" 92local signing_string="$request_target\n$date_header\n$host_header" 93local headers="(request-target) date host" 94local curl_header_args 95curl_header_args=(-H "$date_header") 96local body_arg 97body_arg=() 98 99if [ "$curl_method" = "PUT" -o "$curl_method" = "POST" ]; then 100 signing_string="$signing_string\n$content_sha256_header\n$content_type_header\n$content_length_header" 101 headers=$headers" x-content-sha256 content-type content-length" 102 curl_header_args=("${curl_header_args[@]}" -H "$content_sha256_header" -H "$content_type_header" -H "$content_length_header") 103 body_arg=(--data-binary @${body}) 104fi 105 106if [[ -z "${OCI_CURL_PRIVATE_KEY_PASSPHRASE}" ]]; then 107 local sig=$(printf '%b' "$signing_string" | \ 108 openssl dgst -sha256 -sign $privateKeyPath | \ 109 openssl enc -e -base64 | tr -d '\n') 110else 111 local sig=$(printf '%b' "$signing_string" | \ 112 openssl dgst -sha256 -sign $privateKeyPath -passin env:OCI_CURL_PRIVATE_KEY_PASSPHRASE | \ 113 openssl enc -e -base64 | tr -d '\n') 114fi 115 116curl "${extra_args[@]}" "${body_arg[@]}" -X $curl_method -sS https://${host}${escaped_target} "${curl_header_args[@]}" \ 117 -H "Authorization: Signature version=\"$sigVersion\",keyId=\"$keyId\",algorithm=\"$alg\",headers=\"${headers}\",signature=\"$sig\"" 118} 119# url encode all special characters except "/", "?", "=", and "&" 120function rawurlencode { 121 local string="${1}" 122 local strlen=${#string} 123 local encoded="" 124 local pos c o 125 126 for (( pos=0 ; pos<strlen ; pos++ )); do 127 c=${string:$pos:1} 128 case "$c" in 129 [-_.~a-zA-Z0-9] | "/" | "?" | "=" | "&" ) o="${c}" ;; 130 * ) printf -v o '%%%02x' "'$c" 131 esac 132 encoded+="${o}" 133 done 134 135 echo "${encoded}" 136} 137
試したこと
上記Bashプログラム自体は正常に動作し、ファイルアップロードが行えます。
実現したい事はcURLコマンドでの実行ですので、プログラムからcURLコマンドを割り出したところ、実行コマンドと判明したので実行したのですが、エラーとなります。
cURLコマンドの割り出し方は、下記のように cat <<EOD~EOD で割り出しました。
cat <<EOD
curl "${extra_args[@]}" "${body_arg[@]}" -X $curl_method -sS https://${host}${escaped_target} "${curl_header_args[@]}"
-H "Authorization: Signature version="$sigVersion",keyId="$keyId",algorithm="$alg",headers="${headers}",signature="$sig""
EOD
また、アップロードするtext.txtファイルも実在します。
[root@django oci-bash]# ls
oci_api_key.pem oci_api_key_public.pem oci-curl test.txt
[root@django oci-bash]#
補足情報(FW/ツールのバージョンなど)
・cURLのバージョン
[root@django oci-bash]# curl -V
curl 7.29.0 (x86_64-redhat-linux-gnu) libcurl/7.29.0 NSS/3.44 zlib/1.2.7 libidn/1.28 libssh2/1.8.0
・cURLの実行環境
Oracle Linux 7
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/11/21 10:34
2020/11/21 10:46
2020/11/21 11:17
2020/12/06 15:50