Android Question Amazon AWS Signature Problem

Discussion in 'Android Questions' started by garciag, May 8, 2015.

  1. garciag

    garciag New Member Licensed User

    I'm trying to upload a file to Amazon S3 and haven't been able to get it to work. I've tried several different things such as an aws library on this forum that didn't work for me. (BTW - I would be willing to pay money for someone to wrap the official AWS SDK ).

    Essentially I have a server where i request and receive a pre-signed url to s3. All I have to do is do a PUT on the file to s3 with the url and it should work. In fact, If I paste that url into version of a simple upload routine it works fine. If I try it with B4A i get a Forbidden-403 response. I'm curious if anyone else here has had ANY success uploading files to S3 as I use this functionality all of the time.

    Here's a copy of the code to upload based on a pre-signed url I received and then a copy of the b4a code with the same url. version
    Sub PostToS3()
    'use a presigned url
            Dim filePath As String = "C:\temp\Agreement.pdf"
    Dim url As String = "*** PUT REAL URL HERE ***"
    Dim request As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest)

                request.Method = 
                Using fileStream 
    As FileStream = File.OpenRead(filePath)
                    Using requestStream 
    As Stream = request.GetRequestStream()
    Dim bufferSize As Integer = 1024
    Dim buffer(bufferSize - 1As Byte
    Dim byteCount As Integer = 0
                        byteCount = fileStream.Read(buffer, 
    0, bufferSize)
    Do While byteCount > 0
    0, byteCount)
                            byteCount = fileStream.Read(buffer, 
    0, bufferSize)
    End Using
    End Using
    Dim result As String

                Using response 
    As WebResponse = request.GetResponse()
                    Using reader 
    As New StreamReader(response.GetResponseStream())
                        result = reader.ReadToEnd()
    End Using
    End Using


    Catch ex As Exception
    End Try
    End Sub
    Here's a portion of my b4a code with the same url
    Sub Process_Globals
    Dim hClient As HttpClient
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
    End If
    End Sub

    Sub DoTest()
    Dim req As HttpRequest
    Dim MyArray() As Byte
            MyArray = ReadFile(
    "*** same url here ***", MyArray)   
    "application/pdf")    'Ive done it with and without this
    End Sub
    I looked at the packets going through and the file is PUT to s3 and the headers seem to be alright but i get the Forbidden response. The code in .net works fine with the url so I can only assume it's something in my b4a code or how I'm doing it. I don't know what else to do. I've tried using the httputils2 library also and no success. Please help! Thx.
  2. Erel

    Erel Administrator Staff Member Licensed User

    Try to add a user agent header. Some servers reject requests without this header.
  3. DonManfred

    DonManfred Expert Licensed User

    Wrong forum? this seems to be an B4J issue, not B4A
  4. garciag

    garciag New Member Licensed User

    Thanks but the user agent header didn't fix the problem and this isn't the wrong forum since I'm trying to do this from B4A. Has anyone had any success sending a file to S3?
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice