VBScript
VBScript
REST Read Binary Response into BinData Object (Amazon S3)
Demonstrates how to make a REST call that returns a binary response body. This example is valid for files that fit in memory. The example shows how to download a JPG from the Amazon S3 service.Chilkat VBScript Downloads
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)
success = 0
' This example requires the Chilkat API to have been previously unlocked.
' See Global Unlock Sample for sample code.
set rest = CreateObject("Chilkat.Rest")
' Connect to the Amazon AWS REST server.
bTls = 1
port = 443
bAutoReconnect = 1
success = rest.Connect("s3.amazonaws.com",port,bTls,bAutoReconnect)
' ----------------------------------------------------------------------------
' Important: For buckets created in regions outside us-east-1,
' there are three important changes that need to be made.
' See Working with S3 Buckets in Non-us-east-1 Regions for the details.
' ----------------------------------------------------------------------------
' Provide AWS credentials for the REST call.
set authAws = CreateObject("Chilkat.AuthAws")
authAws.AccessKey = "AWS_ACCESS_KEY"
authAws.SecretKey = "AWS_SECRET_KEY"
authAws.ServiceName = "s3"
success = rest.SetAuthAws(authAws)
' Set the bucket name via the HOST header.
' In this case, the bucket name is "chilkat100".
rest.Host = "chilkat100.s3.amazonaws.com"
' Send the request to download the JPG.
success = rest.SendReqNoBody("GET","/starfish.jpg")
If (success <> 1) Then
outFile.WriteLine(rest.LastErrorText)
WScript.Quit
End If
' Read the response header.
responseStatusCode = rest.ReadResponseHeader()
If (responseStatusCode < 0) Then
outFile.WriteLine(rest.LastErrorText)
WScript.Quit
End If
outFile.WriteLine("Response status code = " & responseStatusCode)
' We expect a 200 response status if the JPG data is coming.
' Otherwise, we'll get a string response body with an error message(or no response body).
If (responseStatusCode <> 200) Then
errResponse = rest.ReadRespBodyString()
If (rest.LastMethodSuccess <> 1) Then
outFile.WriteLine(rest.LastErrorText)
Else
outFile.WriteLine(errResponse)
End If
WScript.Quit
End If
set bdBody = CreateObject("Chilkat.BinData")
success = rest.ReadRespBd(bdBody)
If (success = 0) Then
outFile.WriteLine(rest.LastErrorText)
WScript.Quit
End If
' Save the contents of the response body to a file.
success = bdBody.WriteFile("qa_output/starfish.jpg")
If (success = 1) Then
outFile.WriteLine("Success")
Else
outFile.WriteLine("Failed to write file.")
End If
outFile.Close