簡體   English   中英

無法使用帶有查詢字符串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}

如您所見,這里沒有什么特別的。 我想在查詢字符串請求中使用variant。

但是它總是向我發送回“ 403 Forbidden”錯誤,並顯示其他消息-“我們計算出的請求簽名與您提供的簽名不匹配。請檢查您的密鑰和簽名方法。” 谷歌搜索該錯誤消息也沒有幫助我。

我在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)

剛剛輸入了我得到的兩個秘密密鑰,存儲桶名稱和文件路徑,它對我有用。 但這不是我想要的。

當然,我閱讀了這份文檔並嘗試遵循它。 我的“ stringtoSing”變量以正確的方式形成。 我簡直無法想象錯誤隱藏在哪里。

問題是這樣的:

openssl sha1 -hmac ${s3Key} 

您不使用密鑰簽名,而是使用秘密簽名。

openssl sha1 -hmac ${s3Secret} 

我在正確的位置設置了所有變量名稱,並將openssl更新為OpenSSL 1.0.2g(2016年3月1日)。現在可以使用了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM