Android Question Google Cloud Storage upload

foakgul

Member
Licensed User
Longtime 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:

B4X:
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
 

DonManfred

Expert
Licensed User
Longtime 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

Where do I fill in access_token if I need to use postfile command?

If you still want to use rest; Add a authentification header to your request.
B4X:
    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:
Upvote 1
Cookies are required to use this site. You must accept them to continue using the site. Learn more…