How do you upload a file using an app created in Android Studio to Google Cloud Storage?

0 views
Skip to first unread message

Tom via StackOverflow

unread,
Apr 2, 2016, 1:48:05 PM4/2/16
to google-appengin...@googlegroups.com

Would anyone please take the time to step-by-step document this seemingly simple task?

I am new to java and the latest Google Cloud Platform APIs. I cannot figure out how to do the simple task of uploading a file to Google Cloud Platform from an Android App. Most examples are three years old or are not end-to-end. Either way, they fail so mysteriously at some point that I cannot figure out how to proceed. For example, the exception I'm getting with using the simple-cloud-storage demo is "null." I can't even figure out what is generating "null."

So far I've only been successful in doing something by using the endpoints tutorial: https://cloud.google.com/appengine/docs/python/endpoints/getstarted/clients/android/ But there is no talk about exchanging files in that demo.



Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/36376712/how-do-you-upload-a-file-using-an-app-created-in-android-studio-to-google-cloud

Tom via StackOverflow

unread,
Apr 3, 2016, 8:03:06 PM4/3/16
to google-appengin...@googlegroups.com

Complete code example is here. This code example was created from the Google HelloEndpoints tutorial. This is probably a terrible implementation of using GCS in Android, but it works unlike most examples currently available.

Steps to achieve this:

  1. Create a GCS bucket with service account credentials.
  2. Get a .p12 private key and place that into src/main/res/raw. You have to remove the .p12 extension and replace the - with a _. You need the password, too, so keep that handy.
  3. Look in MainActivity.java at public void onClickFileChooser(View view). What this does is call a class that allows you to navigate and select a file. There's some commented-out code that checks with the server for an upload URL in case you want to try to store the file with the blobstore API. I could not figure that out, so I tried GCS. The GCS stuff is the next bit of code. You'll notice it's a two step process. There are some globals defined for GCS because I'm not a good programmer.
  4. The first step is getting a "Google credential." The code loads the private key with keystore.load(getResources().openRawResource(R.raw.thomasmhardy_ebc515c808a6) and does some other stuff. You might have to change the key password. Look for "notasecret" and replace that with your password.
  5. The actual storage of the file is next. You'll see the URI is set per the Google example. Next, I set up an asynchronous task because Android does not want you doing anything on the network in the main thread. The task first refreshes the GoogleCredential. Once's that's complete, it uses android-async-http to post the file to GCS. Note, the header for Authorization concatenates "Bearer " before the access token. That uploads the file.
  6. All this requires a few dependencies. Check out the gradle.
  7. I also had to enable multiDex.

Note: I removed the contents of my private key "thomasmhardy_ebc515c808a6."



Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/36376712/how-do-you-upload-a-file-using-an-app-created-in-android-studio-to-google-cloud/36392747#36392747
Reply all
Reply to author
Forward
0 new messages