我有一個經典的 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 物件正確上傳?
干杯
uj5u.com熱心網友回復:
我最終通過首先在本地保存影像檔案然后將其上傳到 s3 來解決這個問題。似乎成功了。
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)
''Get file name
aryPath = split(imageurl, "/")
varFileName = aryPath(UBound(aryPath))
varFilePath = varAppPath & "uploads\aws\" & varFileName
''Save file locally
oStream.SaveToFile varFilePath, adSaveCreateOverwrite
''Read saved file
oStream.LoadFromFile varFilePath
url_to_stream = oStream.Read
oStream.Close
set oStream = Nothing
end if
end if
end if
set xml = Nothing
end function
uj5u.com熱心網友回復:
切換到使用ADODB.Stream是正確的方法。但是,問題是正在寫入流,但隨后立即讀取,此時流位置已經在寫入之后,因此沒有讀取任何內容。
在閱讀之前,您需要將流設定Position回0.
oStream.Position = 0
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/491051.html
上一篇:如何將此圖上的線延伸到圖的邊緣?
下一篇:如何判斷condition_variable.wait_for被虛假喚醒或cv_status::timeout阻止的方式是什么?
