Using Google Cloud Storage JSON api in android

我是研究僧i 提交于 2019-11-28 18:03:57

Ok guys so I solved it and got my images being uploaded in Cloud Storage all good. This is how:

Note: I used the XML API it is pretty much the same.

First, you will need to download a lot of libraries. The easiest way to do this is create a maven project and let it download all the dependencies required. From this sample project : Sample Project The libraries should be:

Second, you must be familiar with Cloud Storage using the api console You must create a project, create a bucket, give the bucket permissions, etc. You can find more details about that here

Third, once you have all those things ready it is time to start coding. Lets say we want to upload an image: Cloud storage works with OAuth, that means you must be an authenticated user to use the API. For that the best way is to authorize using Service Accounts. Dont worry about it, the only thing you need to do is in the API console get a service account like this:

We will use this service account on our code.

Fourth, lets write some code, lets say upload an image to cloud storage. For this code to work you must put your key generated in step 3 in assets folder, i named it "key.p12".

I don't recommend you to do this on your production version, since you will be giving out your key.

try{
httpTransport= new com.google.api.client.http.javanet.NetHttpTransport();


//agarro la key y la convierto en un file
AssetManager am = context.getAssets();
InputStream inputStream = am.open("key.p12"); //you should not put the key in assets in prod version.



//convert key into class File. from inputstream to file. in an aux class.
File file = UserProfileImageUploadHelper.createFileFromInputStream(inputStream,context);


//Google Credentianls
GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport)
        .setJsonFactory(JSON_FACTORY)
        .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
        .setServiceAccountScopes(Collections.singleton(STORAGE_SCOPE))
        .setServiceAccountPrivateKeyFromP12File(file)
        .build();




String URI = "https://storage.googleapis.com/" + BUCKET_NAME+"/"+imagename+".jpg";
HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential);


GenericUrl url = new GenericUrl(URI);




//byte array holds the data, in this case the image i want to upload in bytes.

HttpContent contentsend = new ByteArrayContent("image/jpeg", byteArray );


HttpRequest putRequest = requestFactory.buildPutRequest(url, contentsend);



com.google.api.client.http.HttpResponse response = putRequest.execute();
String content = response.parseAsString();
Log.d("debug", "response is:"+response.getStatusCode());
Log.d("debug", "response content is:"+content);} catch (Exception e) Log.d("debug", "Error in  user profile image uploading", e);}

This will upload the image to your cloud bucket.

For more info on the api check this link Cloud XML API

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!