无法使用带有查询字符串 requestauthentication 的 curl 从 amazon s3 存储中获取文件

Can't get a file from amazon s3 storage using curl with query string requestauthentication

我正在尝试使用我在 Internet 上找到的简单 bash 脚本从我的 s3 存储下载文件。

#!/bash/sh

bucket='my_bucket_name'
file_path='path_to_my_file'
resource="/${bucket}/${file_path}"
# set url time to expire

expires=$(date +%s -d '4000 seconds')
stringtoSign="GET\n\n\n${expires}\n${resource}"
s3Key='s3Key_here'
s3Secret='s3SecretKey_here'

signature=`echo -en ${stringtoSign} | openssl sha1 -hmac ${s3Key} -binary | base64`


curl -G https://${bucket}.s3.amazonaws.com/${file_path} \
    --data AWSAccessKeyId=${s3Key} \
    --data Expires=${expires}\
    --data-urlencode Signature=${signature}

如您所见,这里没有什么特别之处。我想将变体与查询字符串请求一起使用。

但它总是给我发回“403 Forbidden”错误和附加消息 - "The request signature we calculated does not match the signature you provided. Check your key and signing method."谷歌搜索该错误消息对我也没有帮助。

我在 boto python 库的帮助下检查了凭据,

import boto

from boto.s3.key import Key


KEY_ID = 'key_id'
SECRET_KEY_ID = 'secret_key'
SOURCE_FILE_NAME = 'path_to_file'
DEST_FILE_NAME = 'file'
BUCKET_NAME = 'my_bucket_name'

boto.set_stream_logger('boto')
conn = boto.connect_s3(KEY_ID, SECRET_KEY_ID)
bucket = conn.get_bucket(BUCKET_NAME)

# Get the Key object of the given key, in the bucket
k = Key(bucket, SOURCE_FILE_NAME)

# Get the contents of the key into a file
k.get_contents_to_filename(DEST_FILE_NAME)

刚刚输入了我得到的两个密钥,存储桶名称和文件路径,它对我有用。但这不是我要找的。

当然,我读了这篇文章 docs 并试图遵循它。我的 'stringtoSing' 变量以正确的方式形成。我无法想象错误隐藏在哪里。

问题是这样的:

openssl sha1 -hmac ${s3Key} 

您不使用您的密钥签名——您使用您的秘密签名。

openssl sha1 -hmac ${s3Secret} 

我在正确的位置设置了所有变量名称,并将 openssl 更新为 2016 年 3 月 1 日的 OpenSSL 1.0.2g。现在可以使用了。