使用VBScript将图像文件直接上传到S3存储桶

发布于 2025-02-07 09:42:49 字数 1980 浏览 1 评论 0原文

我有一个经典的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静谧幽蓝 2025-02-14 09:42:49

切换到使用adodb.stream是正确的方法。但是,问题在于流正在写入,但随后立即读取,这时流的位置已经在写入之后,因此没有读取任何内容。

在阅读之前,您需要将流位置设置回到0

oStream.Position = 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 to 0.

oStream.Position = 0
半透明的墙 2025-02-14 09:42:49

我最终通过首先保存图像文件,然后将其上传到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

I ended up resolving this by saving the image file locally first then uploading it to s3. Seemed to do the trick.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文