Uploading a Model to Glue

Uploading a Model to Glue

david_farina_fluor_com
Contributor Contributor
975 Views
1 Reply
Message 1 of 2

Uploading a Model to Glue

david_farina_fluor_com
Contributor
Contributor

How do I upload a model to glue that is big enough to have to be chunked?  Here is my code:

 

I get the following response

 

Status Code: BadRequest
ERROR;400;The BIM 360 Glue Platform was unable to parse your multipart post.

 

 

Private Function SendUpload(ByVal authToken As String, ByVal projectId As String, FileName As String, chunk As Byte(), chunk_number As Integer, chunk_total As Integer) As String


Dim timeStamp As String = Utils.GetUNIXEpochTimestamp.ToString
Dim signature As String = Utils.ComputeMD5Hash((apiKey _
+ (apiSecret + timeStamp)))

 

Dim client = New RestClient
client.BaseUrl = baseApiUrl

 

Dim request = New RestRequest
request.Resource = "model/v1/upload"
request.Method = Method.POST

 

request.AddHeader("Content-Type", "multipart/form-data")

 

request.AddParameter("format", "json")
request.AddParameter("company_id", companyId)
request.AddParameter("api_key", apiKey)
request.AddParameter("timestamp", timeStamp)
request.AddParameter("sig", signature)
request.AddParameter("auth_token", authToken)
request.AddParameter("project_id", projectId)

request.AddParameter("chunk", chunk)

request.AddParameter("chunk_number", chunk_number)
request.AddParameter("chunk_total", chunk_total)

 

Dim response As IRestResponse = client.Execute(request)

' Save response. This is to see the response for our learning.
Me.m_lastResponse = response
If Not (response.StatusCode = HttpStatusCode.OK) Then
Return Nothing
End If

 

Return "OK"


End Function

0 Likes
Accepted solutions (1)
976 Views
1 Reply
Reply (1)
Message 2 of 2

david_farina_fluor_com
Contributor
Contributor
Accepted solution

I figured it out 🙂  It was 2 things

 

First

 

I had to change

request.AddParameter("chunk", chunk)

to

request.AddFile("chunk", chunk, FileName)

 

Second

Even Though the documentation says files greater than 10485760 need to be chunked, when I used this size it would not work.

So I made the chunks 1 MB and now it works.

 

The working sample is below:

 

'First a Function to break apart the file if needed:

 

Public Function Upload(ByVal authToken As String, ByVal projectId As String, fullfilename As String) As Integer

Dim fi As New FileInfo(fullfilename)
Dim ChunkSize = 1024 * 1024

Dim Chunks As New List(Of Byte())
' Open the file to start reading bytes.

Try
Using InputStream As New IO.FileStream(fi.FullName, IO.FileMode.Open)
Using BinaryReader As New IO.BinaryReader(InputStream)

Dim BytesWritten As Long = 0 'Counts the length of the current file
Dim ChunkIndex As Integer = 0 'Keep track of the number of chunks
While InputStream.Position < InputStream.Length

BytesWritten = 0 'Reset length counter

' Create the chunk file to Write the bytes.
While BytesWritten < ChunkSize AndAlso InputStream.Position < InputStream.Length 'Read until you have reached the end of the input
Dim ReadBytes() As Byte = BinaryReader.ReadBytes(ChunkSize) 'Read

Chunks.Add(ReadBytes)

BytesWritten += ReadBytes.Count 'Increment size counter
End While

ChunkIndex += 1 'Increment file counter
End While

End Using ' BinaryReader
End Using ' InputStream

For n = 0 To Chunks.Count - 1
SendUpload(authToken, projectId, fi.Name, Chunks(n), n + 1, Chunks.Count)
Next

Return fi.Length

Catch ex As Exception
ShowMessageBox(ex.ToString)
Return 0
End Try

End Function

 

'Then a Function to do the upload of each chunk:

 

Private Function SendUpload(ByVal authToken As String, ByVal projectId As String, FileName As String, chunk As Byte(), chunk_number As Integer, chunk_total As Integer) As String
Dim timeStamp As String = Utils.GetUNIXEpochTimestamp.ToString
Dim signature As String = Utils.ComputeMD5Hash((apiKey _
+ (apiSecret + timeStamp)))

Dim client = New RestClient
client.BaseUrl = baseApiUrl

Dim request = New RestRequest
request.Resource = "model/v1/upload"
request.Method = Method.POST

request.AddHeader("Content-Type", "multipart/form-data")

request.AddParameter("format", "json")
request.AddParameter("company_id", companyId)
request.AddParameter("api_key", apiKey)
request.AddParameter("timestamp", timeStamp)
request.AddParameter("sig", signature)
request.AddParameter("auth_token", authToken)
request.AddParameter("project_id", projectId)

request.AddFile("chunk", chunk, FileName)

request.AddParameter("chunk_number", chunk_number)
request.AddParameter("chunk_total", chunk_total)

Dim response As IRestResponse = client.Execute(request)

' Save response. This is to see the response for our learning.
Me.m_lastResponse = response
If Not (response.StatusCode = HttpStatusCode.OK) Then
Return Nothing
End If

Return "OK"
End Function