经典ASP亚马逊s3休闲授权

前端之家收集整理的这篇文章主要介绍了经典ASP亚马逊s3休闲授权前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我很困惑我在这里做错什么
  1. <script language="javascript" runat="server">
  2. function GMTNow(){return new Date().toGMTString()}
  3. </script>
  4. <%
  5.  
  6. Const AWS_BUCKETNAME = "uk-bucketname"
  7. Const AWS_ACCESSKEY = "GOES HERE"
  8. Const AWS_SECRETKEY = "SECRET"
  9. LocalFile = Server.Mappath("/test.jpg")
  10.  
  11. Dim sRemoteFilePath
  12. sRemoteFilePath = "/files/test.jpg" 'Remote Path,note that AWS paths (in fact they aren't real paths) are strictly case sensitive
  13.  
  14. Dim strNow
  15. strNow = GMTNow() ' GMT Date String
  16.  
  17. Dim StringToSign
  18. StringToSign = Replace("PUT\n\nimage/jpeg\n\nx-amz-date:" & strNow & "\n/"& AWS_BUCKETNAME & sRemoteFilePath,"\n",vbLf)
  19.  
  20. Dim Signature
  21. Signature = BytesToBase64(HMACSHA1(AWS_SECRETKEY,StringToSign))
  22.  
  23. Dim Authorization
  24. Authorization = "AWS " & AWS_ACCESSKEY & ":" & Signature
  25.  
  26. Dim AWSBucketUrl
  27. AWSBucketUrl = "http://s3.amazonaws.com/" & AWS_BUCKETNAME
  28.  
  29. With Server.CreateObject("Microsoft.XMLHTTP")
  30. .open "PUT",AWSBucketUrl & sRemoteFilePath,False
  31. .setRequestHeader "Authorization",Authorization
  32. .setRequestHeader "Content-Type","image/jpeg"
  33. .setRequestHeader "Host",AWS_BUCKETNAME & ".s3.amazonaws.com"
  34. .setRequestHeader "x-amz-date",strNow
  35. .send GetBytes(LocalFile) 'Get bytes of local file and send
  36. If .status = 200 Then ' successful
  37. Response.Write "<a href="& AWSBucketUrl & sRemoteFilePath &" target=_blank>Uploaded File</a>"
  38. Else ' an error ocurred,consider xml string of error details
  39. Response.ContentType = "text/xml"
  40. Response.Write .responseText
  41. End If
  42. End With
  43.  
  44. Function GetBytes(sPath)
  45. dim fs,f
  46. set fs=Server.CreateObject("Scripting.FileSystemObject")
  47. set f=fs.GetFile(sPath)
  48. GetBytes = f.Size
  49. set f=nothing
  50. set fs=nothing
  51. End Function
  52.  
  53. Function BytesToBase64(varBytes)
  54. With Server.CreateObject("MSXML2.DomDocument").CreateElement("b64")
  55. .dataType = "bin.base64"
  56. .nodeTypedValue = varBytes
  57. BytesToBase64 = .Text
  58. End With
  59. End Function
  60.  
  61. Function HMACSHA1(varKey,varValue)
  62. With Server.CreateObject("System.Security.Cryptography.HMACSHA1")
  63. .Key = UTF8Bytes(varKey)
  64. HMACSHA1 = .ComputeHash_2(UTF8Bytes(varValue))
  65. End With
  66. End Function
  67.  
  68. Function UTF8Bytes(varStr)
  69. With Server.CreateObject("System.Text.UTF8Encoding")
  70. UTF8Bytes = .GetBytes_4(varStr)
  71. End With
  72. End Function
  73. %>

现在得到错误.

  1. msxml3.dll error '800c0008'
  2.  
  3. The download of the specified resource has Failed.
  4.  
  5. /s3.asp,line 39

解决方法

我想解释一下S3 Rest Api如何工作,就我所知.首先,你需要学习什么应该是字符串签署亚马逊接受.

格式:

  1. StringToSign = HTTP-Verb + "\n" +
  2. Content-MD5 + "\n" +
  3. Content-Type + "\n" +
  4. Date + "\n" +
  5. CanonicalizedAmzHeaders +
  6. CanonicalizedResource;

生成有符号的字符串:

  1. Signature = Base64( HMAC-SHA1( YourSecretAccessKeyID,UTF-8-Encoding-Of( StringToSign ) ) );

传递授权标题

  1. Authorization = "AWS" + " " + AWSAccessKeyId + ":" + Signature;

不幸的是,你会播放字节到字节,因为没有任何SDK发布经典的asp.所以,应该通过阅读整个页面http://docs.amazonwebservices.com/AmazonS3/latest/dev/RESTAuthentication.html来了解

对于字符串进行签名,您可以在上面看到格式,API有三个本机头. Content-Type,Content-MD5和Date.这些标头必须存在于字符串中,即使您的请求没有标头名称也不是空的,只是其值.有一个例外,如果请求中已经存在x-amz-date头,则字符串中的Date标头必须为空.然后,如果请求具有规范的亚马逊标头,则应将其添加为键值对,如x-amz-headername:value.但是,还有一个例外需要考虑多个头.多个标题应该组合成一个标题,并用逗号分隔.

正确

x-amz-headername:value1,value2

错误

x-amz-headername:value1\n
x-amz-headername:value2

最重要的是,标头必须按其字符串中的组的升序顺序进行签名.首先,按照升序排列保留标题,然后按规则标题升序.

我建议使用DomDocument功能生成Base64编码的字符串.
另外,除了Windows脚本组件(.wsc文件)之外,您可以使用.Net的Interops(如System.Security.Cryptography)来更有效地利用System.Text的功能生成密钥哈希.所有这些互操作性在当今的IIS Web服务器中都可用.
所以,作为一个例子,我写下面的脚本只是发送一个文件到你指定的桶.考虑和测试
假设本地文件名为myimage.jpg,并将以相同的名称上传到存储桶的根目录.

  1. <script language="javascript" runat="server">
  2. function GMTNow(){return new Date().toGMTString()}
  3. </script>
  1. <%
  2. Const AWS_BUCKETNAME = "uk-bucketname"
  3. Const AWS_ACCESSKEY = "GOES HERE"
  4. Const AWS_SECRETKEY = "SECRET"
  5.  
  6. LocalFile = Server.Mappath("/test.jpg")
  7.  
  8. Dim sRemoteFilePath
  9. sRemoteFilePath = "/files/test.jpg" 'Remote Path,StringToSign))
  10.  
  11. Dim Authorization
  12. Authorization = "AWS " & AWS_ACCESSKEY & ":" & Signature
  13.  
  14. Dim AWSBucketUrl
  15. AWSBucketUrl = "https://" & AWS_BUCKETNAME & ".s3.amazonaws.com"
  16.  
  17. With Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")
  18. .open "PUT",consider xml string of error details
  19. Response.ContentType = "text/xml"
  20. Response.Write .responseText
  21. End If
  22. End With
  23.  
  24. Function GetBytes(sPath)
  25. With Server.CreateObject("Adodb.Stream")
  26. .Type = 1 ' adTypeBinary
  27. .Open
  28. .LoadFromFile sPath
  29. .Position = 0
  30. GetBytes = .Read
  31. .Close
  32. End With
  33. End Function
  34.  
  35. Function BytesToBase64(varBytes)
  36. With Server.CreateObject("MSXML2.DomDocument").CreateElement("b64")
  37. .dataType = "bin.base64"
  38. .nodeTypedValue = varBytes
  39. BytesToBase64 = .Text
  40. End With
  41. End Function
  42.  
  43. Function HMACSHA1(varKey,varValue)
  44. With Server.CreateObject("System.Security.Cryptography.HMACSHA1")
  45. .Key = UTF8Bytes(varKey)
  46. HMACSHA1 = .ComputeHash_2(UTF8Bytes(varValue))
  47. End With
  48. End Function
  49.  
  50. Function UTF8Bytes(varStr)
  51. With Server.CreateObject("System.Text.UTF8Encoding")
  52. UTF8Bytes = .GetBytes_4(varStr)
  53. End With
  54. End Function
  55. %>

猜你在找的asp.Net相关文章