Amazon S3 / Upload
Pessoal,
Vocês tem ideia o poque este script troca o conteúdo do arquivo em questão (que estou fazendo o upload) pelo numero de bytes?
Desenhando: tenho um txt chamado "upload.txt" com cerca de 1234 bytes. Eu subo o arquivo, o upload "envia" o arquivo mas ele imprime no conteudo do txt este numero de bytes "1234" substituindo o conteudo do txt... e isso acontece com tudo que subo, fazendo com que, claro, imagens, pdfs e demais arquivos fiquem corrompidos.
<script language="javascript" runat="server">
function GMTNow(){
return new Date().toGMTString()
}
</script>
<%
Const AWS_BUCKETNAME = "XXXX"
Const AWS_ACCESSKEY = "XXXX"
Const AWS_SECRETKEY = "XXXX"
Dim arquivo
arquivo = "upload.txt"
extensao = Split(arquivo,".")
extensaoTratada = LCase(extensao(Ubound(extensao)))
Select Case extensaoTratada
Case "bmp"
ext = "image/bmp"
Case "gif"
ext = "image/gif"
Case "jpg"
ext = "image/jpeg"
Case "png"
ext = "image/png"
Case "tif"
ext = "image/tiff"
Case "ico"
ext = "image/x-icon"
Case "rtf"
ext ="application/rtf"
Case "doc"
ext = "application/msword"
Case "xls"
ext = "application/vnd.ms-excel"
Case "csv"
ext = "application/csv"
Case "wml"
ext = "text/vnd.wap.wml"
Case "xml"
ext = "text/xml"
Case "htm"
ext = "text/HTML"
Case "html"
ext = "text/HTML"
Case "txt"
ext = "text/plain"
Case "ppt"
ext ="application/ms-powerpoint"
Case "pps"
ext ="application/ms-powerpoint"
Case "mdb"
ext = "application/vnd.ms-access"
Case "zip"
ext = "application/x-zip-compressed"
Case "pdf"
ext ="application/pdf"
End Select
LocalFile = Server.Mappath("/"&arquivo)
Dim sRemoteFilePath
sRemoteFilePath = "/DLBCASystems/NumeroDoCaso/"&arquivo '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\n"&ext&"\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", ext
.setRequestHeader "Host", AWS_BUCKETNAME & ".s3.amazonaws.com"
.setRequestHeader "x-amz-date", strNow
.send GetBytes(LocalFile) 'Get bytes of local file and send
'response.Write(LocalFile) &" - "& GetBytes(LocalFile) &" bytes <br/>"
If .status = 200 Then ' successful
Response.Write "<a href=https://dantas.s3.amazonaws.com" & sRemoteFilePath &" target=_blank>Uploaded File</a>"
Else ' an error ocurred, consider xml string of error details
Response.ContentType = "text/xml"
Response.Write .responseText
End IfEnd 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
%>
Quem poder dar uma luz :)
Discussão (2)
Carregando comentários...