1. Raja RAMASAMY
  2. PowerBuilder
  3. Tuesday, 31 January 2023 03:39 AM UTC

Hi

We are using PowerBuilder 2019 R3.

I need to upload files to Azure BLOB store. I have tried using HTTPClient and RESTClient (Post, SendRequest, etc) to upload files. It is not working. FYI I am using SAS Tokens. There is no documentation I could find.

Any help is appreciated.

Thanks

 

Accepted Answer
mike S Accepted Answer Pending Moderation
  1. Tuesday, 31 January 2023 05:40 AM UTC
  2. PowerBuilder
  3. # Permalink
Comment
There are no comments made yet.
Raja RAMASAMY Accepted Answer Pending Moderation
  1. Tuesday, 31 January 2023 06:09 AM UTC
  2. PowerBuilder
  3. # 1

Mike, thank you so much.

Are there any sample PB code (or any recommendation)?

Thanks in advance.

Comment
  1. mike S
  2. Tuesday, 31 January 2023 15:49 PM UTC
are you having a problem with any particular rest call?

The hardest part is getting the SAS token - then you simply construct the rest call to get and put blobs.
  1. Helpful
There are no comments made yet.
Raja RAMASAMY Accepted Answer Pending Moderation
  1. Wednesday, 1 February 2023 00:49 AM UTC
  2. PowerBuilder
  3. # 2

Good day Mike.

For now I am hardcoding the SAS token.

See below code snippet.

lnv_HttpClient = Create HttpClient
lnv_restclient = create restclient
ls_url = "https://abcdefgh.blob.core.windows.net/images/1.pdf?"
ls_sastoken = "<SAS TOKEN>"

li_fp = FileOpen("C:\Users\ramasar\Documents\_reports\104378.pdf", StreamMode!)

FileReadEx(li_fp, lblob_content)

fileclose( li_fp)

lnv_HttpClient.SetRequestHeader("Accept", "*/*")
lnv_HttpClient.SetRequestHeader("Accept-Encoding", "gzip, deflate, br")
lnv_HttpClient.SetRequestHeader("Content-Length", "~"" + string(len(lblob_content)) + "~"")
lnv_HttpClient.SetRequestHeader("Content-Type", "application/pdf")
lnv_HttpClient.SetRequestHeader("x-ms-blob-type", "BlockBlob")

// Send request using GET method

/* tried this. this is not working
li_rc = lnv_HttpClient.SendRequest("PUT", ls_url + ls_sastoken, blob("Hello world!!!"))
*/

/* trying to upload using postdata. this is also not working
li_rc = lnv_httpclient.postdatastart(ls_url + ls_sastoken)

li_Rc = lnv_httpclient.postdata( lblob_content, len(lblob_content))

li_rc = lnv_httpclient.postdataend()
*/
Comment
  1. mike S
  2. Wednesday, 1 February 2023 16:35 PM UTC
you are missing several parts in the required headers.

The container MUST exist, so make sure it is created. your url looks fine.

always check your error messages from the service.





ls_GMTformattedDate = this.of_getutcformateddate( ) //get this from database or from win32 api call



ls_contenttype = 'application/octet-stream'



ls_len = string( LEN( BLOBFILE ) )



ls_URL = "https://"; + <youraccount> + ".blob.core.windows.net/" + <CONTAINER> + "/" + <yourfilename>

ls_URL += '?' + ls_YOURSAS





ll_rval = inv_http.SetRequestHeader("x-ms-blob-type", 'BlockBlob' ) //:<BlockBlob &#124; PageBlob &#124; AppendBlob>

ll_rval = inv_http.SetRequestHeader("x-ms-date", ls_GMTformattedDate) //DATE

ll_rval = inv_http.SetRequestHeader("x-ms-version", is_x_ms_version )

ll_rval = inv_http.SetRequestHeader("Content-Length", ls_len ) //string( lul_len ) )

ll_rval = inv_http.SetRequestHeader("Content-Type", ls_contenttype )



ll_rval = inv_http.sendrequest("PUT", ls_URL, BLOBFILE)



inv_http.GetResponseBody(is_response, Encodingutf8!)



if inv_http.getresponsestatuscode( ) = 201 then

//good!

else

ls_error = string( inv_http.getresponsestatuscode( ) ) + ' ' + inv_http.getresponsestatustext( )

end if

  1. Helpful 2
There are no comments made yet.
Raja RAMASAMY Accepted Answer Pending Moderation
  1. Thursday, 2 February 2023 06:17 AM UTC
  2. PowerBuilder
  3. # 3

Mike, U R A STAR

Following code is working for me. How can I get the SAS token dynamically? Any suggestions?

 

httpclient	lnv_HttpClient
restclient lnv_restclient
integer	li_rc, li_fp
blob lblob_content, lblob_resp
string ls_sastoken
string ls_url

li_fp = FileOpen("C:\myfile.pdf", StreamMode!)

FileReadEx(li_fp, lblob_content)

fileclose( li_fp)

lnv_HttpClient = Create HttpClient
lnv_restclient = create restclient
ls_url = "https://mydomain.blob.core.windows.net/images/1.pdf?"
ls_sastoken = "SAS-TOKEN"

string ls_GMTformattedDate = "2023-02-02 05:36:33" // this.of_getutcformateddate( ) //get this from database or from win32 api call
string ls_contenttype = 'application/octet-stream'

lnv_HttpClient.SetRequestHeader("Accept", "*/*")
lnv_HttpClient.SetRequestHeader("Accept-Encoding", "gzip, deflate, br")
lnv_HttpClient.SetRequestHeader("Content-Length", string( len( lblob_content)))
lnv_HttpClient.SetRequestHeader("Content-Type", "application/pdf")
lnv_HttpClient.SetRequestHeader("x-ms-blob-type", "BlockBlob")
lnv_HttpClient.setrequestheader("x-ms-date", ls_GMTformattedDate)

// Not to read data automatically after sending request (default is true)
lnv_HttpClient.AutoReadData = false

// Send request using GET method
li_rc = lnv_HttpClient.SendRequest("PUT", ls_url + ls_sastoken, lblob_content)

lnv_httpclient.getresponsebody( lblob_resp)

// Receive large data
messagebox("Open from Azure failed!", lnv_httpclient.GetResponseStatusText())
Comment
  1. mike S
  2. Thursday, 2 February 2023 14:35 PM UTC
IMO, no good way to do it in PB since you need your keys. Best if hosted as an api in azure. I use an azure function to generate it.
  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.