使用curl进行s3服务操作

时间:2023-03-08 16:02:08
使用curl进行s3服务操作

最近使用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\n\n\n${dateValue}\n${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\n\n${contentType}\n${dateValue}\n${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\n\n${contentType}\n${dateValue}\n${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}"

  

  1. bucket="mygod"
  2. dateValue=`date -R`
  3. resource="/${bucket}/"
  4. contentType="application/octet-stream"
  5. stringToSign="PUT\n\n\n${dateValue}\n${resource}"
  6. s3Key="6R2MJWR863EREUDD0KTZ"
  7. s3Secret="74eHNNQa1oLBlvZfO2CC2hIU8cobSYxTgeRDtXtH"
  8. signature=`echo -en ${stringToSign} | openssl sha1 -hmac ${s3Secret} -binary | base64`
  9. curl -v -X PUT "http://${bucket}.s3.mydomain.com" \
  10. -H "Host: ${bucket}.s3.mydomain.com" \
  11. -H "Date: ${dateValue}"\
  12. -H "Authorization: AWS ${s3Key}:${signature}"