我已经写了shell脚本上传数据到s3使用CURL请求,但我得到错误,我已经尝试了这么多的方法,但没有工作了我。
上传文件到S3
curl -X PUT
-T“${FILE_PATH}”
-H“主机:${S3_BUCKET}.s3.${S3_REGION}.amazonaws.com”
-H“Content-Type:application/octet-stream”
-H“X-Amz-Content-SHA 256:无信号有效载荷”
-H“X-Amz-Date:${REQUEST_DATE}”
-H“授权:AWS4-HMAC-SHA256凭据=${AWS_ACCESS_KEY}/${REQUEST_SHORT_DATE}/${S3_REGION}/s3/aws4_request,SignedHeaders =host;content-type;x-amz-content-sha256;x-amz-date,Signature=${SIGNATURE}”
“https://${S3_BUCKET}.s3.${S3_REGION}.amazonaws.com”
#!/bin/bash
# AWS Credentials
AWS_ACCESS_KEY="xxxxxxxxxxxx"
AWS_SECRET_KEY="yyyyyyyyyyyyyyyy"
# S3 Bucket details
S3_BUCKET="xxxxxx"
S3_REGION="ap-south-1"
FILE_PATH="/home/user/data.sh"
# Set the date and time for the request
REQUEST_DATE=$(date -u "+%Y%m%dT%H%M%SZ")
REQUEST_SHORT_DATE=$(echo $REQUEST_DATE | cut -c 1-8)
# Generate the signature
SIGNING_KEY=$(printf "${REQUEST_SHORT_DATE}\n${S3_REGION}\ns3\naws4_request\n${AWS_SECRET_KEY}" | \
openssl sha256 -hmac "${REQUEST_SHORT_DATE}" -binary | \
xxd -p -c 256)
CANONICAL_REQUEST=$(printf "PUT\n/${S3_OBJECT_KEY}\n\nhost:${S3_BUCKET}.s3.${S3_REGION}.amazonaws.com\n\nhost\ncontent-type:application/octet-stream\n\ndate:${REQUEST_DATE}\n\nx-amz-content-sha256:UNSIGNED-PAYLOAD\nx-amz-date:${REQUEST_DATE}\n\n/${S3_OBJECT_KEY}")
CANONICAL_REQUEST_HASH=$(printf "${CANONICAL_REQUEST}" | openssl sha256 | awk '{print $2}')
STRING_TO_SIGN=$(printf "AWS4-HMAC-SHA256\n${REQUEST_DATE}\n${REQUEST_SHORT_DATE}/${S3_REGION}/s3/aws4_request\n${CANONICAL_REQUEST_HASH}")
SIGNATURE=$(printf "${STRING_TO_SIGN}" | openssl sha256 -mac HMAC -macopt "hexkey:${SIGNING_KEY}" -binary | xxd -p -c 256)
# Upload the file to S3
curl -X PUT \
-T "${FILE_PATH}" \
-H "Host: ${S3_BUCKET}.s3.${S3_REGION}.amazonaws.com" \
-H "Content-Type: application/octet-stream" \
-H "X-Amz-Content-SHA256: UNSIGNED-PAYLOAD" \
-H "X-Amz-Date: ${REQUEST_DATE}" \
-H "Authorization: AWS4-HMAC-SHA256 Credential=${AWS_ACCESS_KEY}/${REQUEST_SHORT_DATE}/${S3_REGION}/s3/aws4_request,SignedHeaders=host;content-type;x-amz-content-sha256;x-amz-date,Signature=${SIGNATURE}" \
"https://${S3_BUCKET}.s3.${S3_REGION}.amazonaws.com"`
字符串
错误#
SignatureDoesNotMatch
我们计算的请求签名与您提供的签名不匹配。请检查您的密钥和签名方法。AKIAZP 5IDSQC 7 FNOOQCFAWS 4-HMAC-SHA 256 20230301 T060846 Z 20230301/ap-south-1/s3/aws 4_请求3ae 12 e94 bc 2d 6de 162 fc 7a 12 aa 4d 4 baf 25974 b6 f3920 a7433 cc 0 d5 f834 bac 4d 23 d8 a12 be 533618007718 a22 e41 fce 6dd 10 d565834553 fbf 1b 9 c79 ac 2cd 920 d 6d 41 57 53 34 2d 48 4d 41 43 2d 53 48 41 32 35 36 0a 32 30 32 33 30 31 54 30 36 30 38 34 36 5a 0a 32 30 32 33 30 33 30 31 2f 61 70 2d 73 6f 75 74 68 2d 31 2f 73 33 2f 61 77 73 34 5f 72 65 71 75 65 73 74 0a 33 61 65 31 32 65 39 34 62 63 32 64 36 64 65 31 36 32 66 63 37 61 31 32 61 61 34 64 34 62 61 66 32 35 39 37 34 62 36 66 33 39 32 30 61 37 34 33 63 63 30 64 35 66 38 33 34 62 61 63 34 64 32PUT /data.sh
content-type:application/octet-stream主机:vedic.s3.ap-south-1.amazonaws.com x-amz-content-sha256:UNSIGNED-PAYLOAD x-amz-date:20230301T060846Z
content-type;主机; x-amz-content-sha256; x-amz-日期未签名-有效载荷50 55 54 0a 2f 64 61 74 61 2e 73 68 0a 63 6f 6e 74 65 6e 74 2d 74 79 70 65 3a 61 70 6c 69 63 61 74 69 6f 6e 2f 63 74 65 74 2d 73 74 72 65 61 6d 0a 68 6f 73 74 3a 76 65 64 69 63 2e 73 33 2e 61 70 2d 73 6f 75 74 68 2d 31 2e 61 6d 61 7a 6f 6e 61 77 73 2e 63 6f 6d 0a 78 2d 61 6d 7a 2d 63 6f 6e 74 65 6e 74 2d 73 68 61 32 35 36 3a 55 4e 53 49 47 4e 45 44 2d 50 41 59 4c 4f 41 44 0a 78 2d 61 6d 7a 2d 64 61 74 65 3a 32 30 32 33 30 33 30 31 54 30 36 30 38 34 36 5a 0a 0a 63 6f 6e 74 65 6e 74 2d 74 79 70 65 3b 68 6f 73 74 3b 78 2d 61 6d 7a 2d 63 6f 6e 74 65 6e 74 2d 73 68 61 32 35 36 3b 78 2d 61 6d 7a 2d 64 61 74 65 0a 55 4e 53 49 47 4e 45 44 2d 50 41 59 4c 4f 41 442FVFF45HZTKJR1W5axa+Me22CvWnXJ4j/9rAcKNjndTQGeWJIZGo3F+FpATI+R9olEsnLLxut4RzT5kZeJVJX90WvrQ=
1条答案
按热度按时间6g8kf2rb1#
这个bash脚本对我很有效:https://gist.github.com/vszakats/2917d28a951844ab80b1
以下是链接的当前(2023-11-14)内容,以防有一天它消失。
字符串
(图像来源:Viktor Szakats)