使用VBScript将图像文件直接上传到S3存储桶
我有一个经典的ASP脚本要首先在网页上获取所有图像,然后将图像上传到S3存储桶中。第一部分工作正常,但是当试图将图像上传到S3时,我会收到以下错误:
属性仅接受一维字节数组。请参阅下面的代码示例:
remoteurl = "https://some-website-with-images/"
Set AWS = Server.CreateObject("InAmazon.S3")
AWS.AccessKey = AWS_ACCESS_KEY
AWS.SecretKey = AWS_SECRET
AWS.Config("Url=http://s3-ap-southeast-2.amazonaws.com")
AWS.Bucket = "bucket-name"
Set http = Server.CreateObject ("MSXML2.XMLHTTP.6.0")
http.Open "GET", remoteurl, False
http.Send
Set re = New RegExp
re.Pattern = " ]*src=[""'][^ >]*(jpg|png)[""']"
re.IgnoreCase = True
re.Global = True
re.Multiline = True
Set oMatches = re.Execute(http.responseText)
If Not oMatches Is Nothing Then
If oMatches.Count > 0 Then
For Each oMatch In oMatches
If Not oMatches(0).SubMatches Is Nothing Then
sBodyText = oMatch.value
sBodyText = replace(sBodyText,"src=""","")
sBodyText = replace(sBodyText,"""","")
''Read in image as binary
binaryImg = url_to_stream(sBodyText)
AWS.objectDataB = binaryImg
''Upload to S3
AWS.createObject(sBodyText)
End If
Next
End If
End If
function url_to_stream(imageurl)
set xml = Server.CreateObject("MSXML2.XMLHTTP.6.0")
xml.Open "GET", imageurl, false
xml.Send
if err.number = 0 then
if xml.readystate = 4 then
if xml.status = 200 then
set oStream = Server.CreateObject("Adodb.Stream")
oStream.type = adTypeBinary
oStream.Open()
oStream.Write(xml.responseBody)
url_to_stream = oStream.read
oStream.close
set oStream = nothing
end if
end if
end if
set xml = Nothing
end function
从以下行触发错误:
AWS.objectDataB = binaryImg
我在毫无问题的表单上上传图像时使用aws.objectDatab方法,但是当我尝试直接从URL中读取图像时,它不起作用。我是否在图像中读书错误?如何在图像中读取以便inamazon.s3对象正确上传它?
I have a classic asp script to firstly, get all images on a web page then upload the images to an s3 bucket. The first part works fine, but when trying to upload the images to s3 I get the following error:
Property accepts only one-dimensional byte arrays. See code example below:
remoteurl = "https://some-website-with-images/"
Set AWS = Server.CreateObject("InAmazon.S3")
AWS.AccessKey = AWS_ACCESS_KEY
AWS.SecretKey = AWS_SECRET
AWS.Config("Url=http://s3-ap-southeast-2.amazonaws.com")
AWS.Bucket = "bucket-name"
Set http = Server.CreateObject ("MSXML2.XMLHTTP.6.0")
http.Open "GET", remoteurl, False
http.Send
Set re = New RegExp
re.Pattern = " ]*src=[""'][^ >]*(jpg|png)[""']"
re.IgnoreCase = True
re.Global = True
re.Multiline = True
Set oMatches = re.Execute(http.responseText)
If Not oMatches Is Nothing Then
If oMatches.Count > 0 Then
For Each oMatch In oMatches
If Not oMatches(0).SubMatches Is Nothing Then
sBodyText = oMatch.value
sBodyText = replace(sBodyText,"src=""","")
sBodyText = replace(sBodyText,"""","")
''Read in image as binary
binaryImg = url_to_stream(sBodyText)
AWS.objectDataB = binaryImg
''Upload to S3
AWS.createObject(sBodyText)
End If
Next
End If
End If
function url_to_stream(imageurl)
set xml = Server.CreateObject("MSXML2.XMLHTTP.6.0")
xml.Open "GET", imageurl, false
xml.Send
if err.number = 0 then
if xml.readystate = 4 then
if xml.status = 200 then
set oStream = Server.CreateObject("Adodb.Stream")
oStream.type = adTypeBinary
oStream.Open()
oStream.Write(xml.responseBody)
url_to_stream = oStream.read
oStream.close
set oStream = nothing
end if
end if
end if
set xml = Nothing
end function
The error is triggered from the following line:
AWS.objectDataB = binaryImg
I use the AWS.objectDataB method when uploading images from a form without issue, but when I try to read in an image directly from a url it doesn't work. Am I reading in the image incorrectly? How can I read in an image so the InAmazon.S3 object uploads it correctly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
切换到使用
adodb.stream
是正确的方法。但是,问题在于流正在写入,但随后立即读取,这时流的位置已经在写入之后,因此没有读取任何内容。在阅读之前,您需要将流
位置设置
回到0
。Switching to using the
ADODB.Stream
is the correct approach. However, the problem is the stream is being written to but then read instantly afterwards, at which point the stream position is already after the write so nothing is read.Before reading you need to set the stream
Position
back to0
.我最终通过首先保存图像文件,然后将其上传到S3来解决此问题。似乎可以解决这个问题。
I ended up resolving this by saving the image file locally first then uploading it to s3. Seemed to do the trick.