简体   繁体   中英

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

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.

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."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM