<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("PUT\n\nimage/jpeg\n\nx-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
解决方法
格式:
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.但是,还有一个例外需要考虑多个头.多个标题应该组合成一个标题,并用逗号分隔.
正确
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,并将以相同的名称上传到存储桶的根目录.
<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 %>