经典ASP亚马逊s3休闲授权
我很困惑我在这里做错什么
<script language="javascript" runat="server"> function GMTNow(){return new Date().toGMTString()} </script> <% Const AWS_BUCKETNAME = "uk-bucketname" Const AWS_ACCESSKEY = "GOES HERE" Const AWS_SECRETKEY = "SECRET" LocalFile = Server.Mappath("/test.jpg") Dim sRemoteFilePath sRemoteFilePath = "/files/test.jpg" 'Remote Path,note that AWS paths (in fact they aren't real paths) are strictly case sensitive Dim strNow strNow = GMTNow() ' GMT Date String Dim StringToSign StringToSign = Replace("PUTnnimage/jpegnnx-amz-date:" & strNow & "n/"& AWS_BUCKETNAME & sRemoteFilePath,"n",vbLf) Dim Signature Signature = BytesToBase64(HMACSHA1(AWS_SECRETKEY,StringToSign)) Dim Authorization Authorization = "AWS " & AWS_ACCESSKEY & ":" & Signature Dim AWSBucketUrl AWSBucketUrl = "http://s3.amazonaws.com/" & AWS_BUCKETNAME With Server.CreateObject("Microsoft.XMLHTTP") .open "PUT",AWSBucketUrl & sRemoteFilePath,False .setRequestHeader "Authorization",Authorization .setRequestHeader "Content-Type","image/jpeg" .setRequestHeader "Host",AWS_BUCKETNAME & ".s3.amazonaws.com" .setRequestHeader "x-amz-date",strNow .send GetBytes(LocalFile) 'Get bytes of local file and send If .status = 200 Then ' successful Response.Write "<a href="& AWSBucketUrl & sRemoteFilePath &" target=_blank>Uploaded File</a>" Else ' an error ocurred,consider xml string of error details Response.ContentType = "text/xml" Response.Write .responseText End If End With Function GetBytes(sPath) dim fs,f set fs=Server.CreateObject("Scripting.FileSystemObject") set f=fs.GetFile(sPath) GetBytes = f.Size set f=nothing set fs=nothing End Function Function BytesToBase64(varBytes) With Server.CreateObject("MSXML2.DomDocument").CreateElement("b64") .dataType = "bin.base64" .nodeTypedValue = varBytes BytesToBase64 = .Text End With End Function Function HMACSHA1(varKey,varValue) With Server.CreateObject("System.Security.Cryptography.HMACSHA1") .Key = UTF8Bytes(varKey) HMACSHA1 = .ComputeHash_2(UTF8Bytes(varValue)) End With End Function Function UTF8Bytes(varStr) With Server.CreateObject("System.Text.UTF8Encoding") UTF8Bytes = .GetBytes_4(varStr) End With End Function %> 现在得到错误. msxml3.dll error '800c0008' The download of the specified resource has failed. /s3.asp,line 39 解决方法
我想解释一下S3 Rest Api如何工作,就我所知.首先,你需要学习什么应该是字符串签署亚马逊接受.
格式: StringToSign = HTTP-Verb + "n" + Content-MD5 + "n" + Content-Type + "n" + Date + "n" + CanonicalizedAmzHeaders + CanonicalizedResource; 生成有符号的字符串: Signature = Base64( HMAC-SHA1( YourSecretAccessKeyID,UTF-8-Encoding-Of( StringToSign ) ) ); 传递授权标题: 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.但是,还有一个例外需要考虑多个头.多个标题应该组合成一个标题,并用逗号分隔. 正确
错误
最重要的是,标头必须按其字符串中的组的升序顺序进行签名.首先,按照升序排列保留标题,然后按规则标题升序. 我建议使用 <script language="javascript" runat="server"> function GMTNow(){return new Date().toGMTString()} </script> <% Const AWS_BUCKETNAME = "uk-bucketname" Const AWS_ACCESSKEY = "GOES HERE" Const AWS_SECRETKEY = "SECRET" LocalFile = Server.Mappath("/test.jpg") Dim sRemoteFilePath sRemoteFilePath = "/files/test.jpg" 'Remote Path,StringToSign)) Dim Authorization Authorization = "AWS " & AWS_ACCESSKEY & ":" & Signature Dim AWSBucketUrl AWSBucketUrl = "https://" & AWS_BUCKETNAME & ".s3.amazonaws.com" With Server.CreateObject("MSXML2.ServerXMLHTTP.6.0") .open "PUT",consider xml string of error details Response.ContentType = "text/xml" Response.Write .responseText End If End With Function GetBytes(sPath) With Server.CreateObject("Adodb.Stream") .Type = 1 ' adTypeBinary .Open .LoadFromFile sPath .Position = 0 GetBytes = .Read .Close End With End Function Function BytesToBase64(varBytes) With Server.CreateObject("MSXML2.DomDocument").CreateElement("b64") .dataType = "bin.base64" .nodeTypedValue = varBytes BytesToBase64 = .Text End With End Function Function HMACSHA1(varKey,varValue) With Server.CreateObject("System.Security.Cryptography.HMACSHA1") .Key = UTF8Bytes(varKey) HMACSHA1 = .ComputeHash_2(UTF8Bytes(varValue)) End With End Function Function UTF8Bytes(varStr) With Server.CreateObject("System.Text.UTF8Encoding") UTF8Bytes = .GetBytes_4(varStr) End With End Function %> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- ASP.NET MVC实现简单的文件上传与下载
- 如何在ASP.net c#中的页面之间传递对象而不是值?
- asp.net-mvc-3 – 在MVC应用程序中将配置设置注入Javascrip
- asp.net-mvc – 为什么Hangfire需要身份验证才能查看仪表板
- asp.net-mvc – MVC传递ID由“”分隔为动作
- asp.net-mvc – EntityFunctions.CreateDateTime问题与linq
- asp.net – ExecuteScalar抛出NullReferenceException
- asp.net – 连接字符串名称是否必须是LocalSqlServer?
- asp.net – 实体框架4 – 从模型更新数据库模式 不擦拭表数
- 我可以在Asp.Net中的另一个页面中访问一个页面的ViewState吗