最近使用curl对s3进行接口测试,本想写个总结文档,但没想到已有前辈写了,就直接搬过来做个记录吧,原文见:
http://blog.csdn.net/ganggexiongqi/article/details/50483389
不想使用s3cmd/s3curl等工具或者第三方库来测试RadosGW提供的s3服务。所以google了一把,已经有人做过了,详见参考链接。
贴个shell脚本做个记录。
1. 创建bucket
创建名为"mygod"的bucket
bucket="mygod" dateValue=`date -R` resource="/${bucket}/" contentType="application/octet-stream" stringToSign="PUT ${dateValue} ${resource}" s3Key="6R2MJWR863EREUDD0KTZ" s3Secret="74eHNNQa1oLBlvZfO2CC2hIU8cobSYxTgeRDtXtH" signature=`echo -en ${stringToSign} | openssl sha1 -hmac ${s3Secret} -binary | base64` curl -v -X PUT "http://${bucket}.s3.mydomain.com" -H "Host: ${bucket}.s3.mydomain.com" -H "Date: ${dateValue}" -H "Authorization: AWS ${s3Key}:${signature}"
2. 上传对象到bucket
下面的脚本就是将@file上传到@url指定的存储服务集群中的@bucket下,对象的名字叫@objname。
file=./c.zip objname="testfile" bucket=another url="s3.mydomain.com" resource="/${bucket}/${objname}" contentType="application/x-compressed-tar" dateValue=`date -R` stringToSign="PUT ${contentType} ${dateValue} ${resource}" s3Key="6R2MJWR863EREUDD0KTZ" s3Secret="74eHNNQa1oLBlvZfO2CC2hIU8cobSYxTgeRDtXtH" signature=`echo -en ${stringToSign} | openssl sha1 -hmac ${s3Secret} -binary | base64` curl -X PUT -T "${file}" -H "Host: ${bucket}.${url}" -H "Date: ${dateValue}" -H "Content-Type: ${contentType}" -H "Authorization: AWS ${s3Key}:${signature}" "http://${bucket}.${url}/${objname}"
3. 获取object
file="./myfile" objname="testfile" bucket=another url="s3.mydomain.com" resource="/${bucket}/${objname}" contentType="application/x-compressed-tar" dateValue=`date -R` stringToSign="GET ${contentType} ${dateValue} ${resource}" s3Key="6R2MJWR863EREUDD0KTZ" s3Secret="74eHNNQa1oLBlvZfO2CC2hIU8cobSYxTgeRDtXtH" signature=`echo -en ${stringToSign} | openssl sha1 -hmac ${s3Secret} -binary | base64` curl -o ${file} -X GET -H "Host: ${bucket}.${url}" -H "Date: ${dateValue}" -H "Content-Type: ${contentType}" -H "Authorization: AWS ${s3Key}:${signature}" "http://${bucket}.${url}/${objname}"
- bucket="mygod"
- dateValue=`date -R`
- resource="/${bucket}/"
- contentType="application/octet-stream"
- stringToSign="PUT ${dateValue} ${resource}"
- s3Key="6R2MJWR863EREUDD0KTZ"
- s3Secret="74eHNNQa1oLBlvZfO2CC2hIU8cobSYxTgeRDtXtH"
- signature=`echo -en ${stringToSign} | openssl sha1 -hmac ${s3Secret} -binary | base64`
- curl -v -X PUT "http://${bucket}.s3.mydomain.com"
- -H "Host: ${bucket}.s3.mydomain.com"
- -H "Date: ${dateValue}"
- -H "Authorization: AWS ${s3Key}:${signature}"