我有一个经典的 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 对象正确上传?
干杯
切换到使用ADODB.Stream
是正确的方法。但是,问题是正在写入流,但随后立即读取,此时流位置已经在写入之后,因此没有读取任何内容。
在阅读之前,您需要将流设置Position
回0
.
oStream.Position = 0
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句