Android Question Google Cloud Storage upload

Discussion in 'Android Questions' started by foakgul, Jul 8, 2019.

  1. foakgul

    foakgul Member Licensed User

    Hello all,

    What I'd like to accomplish is to simply upload a file (zip) to a bucket that I've already created on Google Cloud Storage.

    REST API URI is given as:
    https://www.googleapis.com/upload/storage/v1/b/[BUCKET_NAME]/o?uploadType=media&name=[OBJECT_NAME]

    I do understand that I need oauth2 to access the API and I'll need to use oauth2 library:

    Code:
    oauth2.GetAccessToken
       
    Wait For OAuth2_AccessTokenAvailable (Success As Boolean, Token As String)
       
    If Success = False Then
         
    ToastMessageShow("Error accessing account."True)
         
    Return
       
    End If
    Dim j As HttpJob
       j.Initialize(
    "", Me)
    The part I'm trying to figure out is do I use:
    -j.download2 OR
    -j.postfile

    In either case, what would be the proper full command to upload the file to my bucket? Where do I fill in access_token if I need to use postfile command?

    Thanks a lot!
    Ferit
     
  2. DonManfred

    DonManfred Expert Licensed User

    1. https://www.b4x.com/android/forum/threads/firebasestorage-simple-file-storage-backend.68350/
    2. https://www.b4x.com/android/help/firebasestorage.html#firebasestorage_uploadfile

    If you still want to use rest; Add a authentification header to your request.
    Code:
    Dim job As HttpJob
        job.Initialize(
    "Profile",Me)
        job.Download(
    "https://url...."' Download, PosString, ect.
        job.GetRequest.SetHeader("Authorization","Bearer "&access_token) 'Set the header AFTER the Download/Postfile
        wait for (job) JobDone(j As HttpJob)
        
    If j.Success Then
            
    Log(j.GetString)
            
    'Dim OutStream As OutputStream = File.OpenOutput(File.DirRootExternal, "Result.txt", False)
            'File.Copy2(job.GetInputStream,OutStream) ' save the file
            'OutStream.Close
        Else
            
    Log("JobError: "&job.ErrorMessage)
        
    End If
        j.Release
    End Sub
     
    Last edited: Jul 8, 2019
    Erel likes this.
Loading...
  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