我正在尝试创建一个bash脚本以将文件上传到我的s3存储桶。我很难生成正确的签名。
我收到以下错误消息:
The request signature we calculated does not match the signature you provided. Check your key and signing method.
这是我的剧本:
感谢您的帮助!
#!/usr/bin/env bash
#upload to S3 bucket
sourceFilePath="$1"
#file path at S3
folderPathAtS3="packages";
#S3 bucket region
region="eu-central-1"
#S3 bucket name
bucket="my-bucket-name";
#S3 HTTP Resource URL for your file
resource="/${bucket}/${folderPathAtS3}";
#set content type
contentType="gzip";
#get date as RFC 7231 format
dateValue="$(date +'%a, %d %b %Y %H:%M:%S %z')"
acl="x-amz-acl:private"
#String to generate signature
stringToSign="PUT\n\n${contentType}\n${dateValue}\n${acl}\n${resource}";
#S3 key
s3Key="my-key";
#S3 secret
s3Secret="my-secret-code";
#Generate signature, Amazon re-calculates the signature and compares if it matches the one that was contained in your request. That way the secret access key never needs to be transmitted over the network.
signature=$(echo -en "${stringToSign}" | openssl sha1 -hmac ${s3Secret} -binary | base64);
#Curl to make PUT request.
curl -L -X PUT -T "${sourceFilePath}" \
-H "Host: ${bucket}.${region}.amazonaws.com" \
-H "Date: ${dateValue}" \
-H "Content-Type: ${contentType}" \
-H "$acl" \
-H "Authorization: AWS ${s3Key}:${signature}" \
https://s3.amazonaws.com/${bucket}/${folderPathAtS3}
答案 0 :(得分:0)
您的签名看起来不错,但您的请求有误,因此不匹配。
scanline_node['projection_mode'].setValue("uv")
TypeError: expecting a string or number, unicode found
scanline_node['projection_mode'].setValue("uv".encode('utf-8'))
不正确。
正确的值为-H "Host: ${bucket}.${region}.amazonaws.com" \
。您正在忽略主机名中的${bucket}.s3 ${region}.amazonaws.com
...但是即使正确,这仍然无效j,因为您的URL s3.
也包含存储桶,这意味着您的存储桶名称被隐式添加到开头对象键,因为它出现了两次。
另外,https://s3.amazonaws.com/${bucket}/...
是us-east-1。要连接到正确的区域,您的URL必须是以下变体之一:
https://s3.amazonaws.com
使用这些格式中的一种,并消除https://${region}.s3.amazonaws.com/${bucket}/${folderPathAtS3}
https://${bucket}.${region}.s3.amazonaws.com/${folderPathAtS3}
https://${bucket}.s3.amazonaws.com/${folderPathAtS3}
,因为这样会多余。
这3个URL格式中的最后一个只有在存储桶使用了几分钟或几小时后才能开始工作。 S3自动创建这些文件,但需要一些时间。