3

I'm trying to upload an image to google cloud storage.
I found an example like this https://github.com/pliablematter/simple-cloud-storage that explains how to do that.
I created a project bucket, etc., but when I try to create a client id I get this error:

An error has occurred. Please retry laterenter image description here

Where did this error come from?
Maybe there is another method to upload file to the google cloud storage with an android application?

EDIT____________ in the new console I can see a message telling me only owner of the project can create clients for the application web and account services. So error is caused because I connect with a account collaborator

enter image description here EDIT__________ Now I can create client id, but I don't know how I can upload file from android to bucket, I read this https://github.com/pliablematter/simple-cloud-storage but it is for java not Android, anyone has an example how can I do that ?

Any help will be appreciated

Pradeep Bhadani
  • 4,435
  • 6
  • 29
  • 48
tamtoum1987
  • 1,957
  • 3
  • 27
  • 56
  • 1
    Could you try to create the service account from the [new Developers Console](https://console.developers.google.com/) clicking on your project -> APIs & auth -> Credentials -> Create new Client ID and inform us if you are able to do it in this way? – Adrián Apr 08 '15 at 09:14
  • Hi, I just edit my first message – tamtoum1987 Apr 08 '15 at 09:53
  • Hi @tamtoum1987, then this question could be considered as answered, right? All you need to do is ask a project owner either to give you owner permissions or to create a service account. – Adrián Apr 08 '15 at 11:48

2 Answers2

4

I can finally upload images on google storage like this

  class RetrieveFeedTask extends AsyncTask<Void, Void, String> {

    private Exception exception;

    protected String doInBackground(Void... params) {

        try {
            List<String> scopes = new ArrayList<String>();
            scopes.add(StorageScopes.DEVSTORAGE_FULL_CONTROL);
              httpTransport= new com.google.api.client.http.javanet.NetHttpTransport();


              //agarro la key y la convierto en un file
              AssetManager am = getAssets();
              String STORAGE_SCOPE = "https://www.google.com/analytics/feeds/" ;
              InputStream inputStream = am.open("*********114db0.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 =stream2file(inputStream);


              //Google Credentianls
              GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport)
                      .setJsonFactory(JSON_FACTORY)
                      .setServiceAccountId("**********ic1bgevf3h@developer.gserviceaccount.com")
                      .setServiceAccountScopes((scopes))
                      .setServiceAccountPrivateKeyFromP12File(file)
                      .build();




              String URI = "https://storage.googleapis.com/" + "BUCKET_NAME"+"/"+"zzzzz3"+".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.

              Resources res = getResources();
              Drawable drawable = res.getDrawable(R.drawable.camera);
              Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
              ByteArrayOutputStream stream = new ByteArrayOutputStream();
              bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
              byte[] bitMapData = stream.toByteArray();


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


              HttpRequest putRequest;

                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 (IOException | GeneralSecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        return "";

    }

    protected void onPostExecute(String feed) {
        int i  = 0;
        int j = i;
        // TODO: check this.exception 
        // TODO: do something with the feed
    }
}

Don't forget to download jar and put it on lib folder: - com.fasterxml.jackson.core.jar - google-api-client-1.20.0.jar - google-api-services-storage-v1beta2-rev21-1.15.0-rc.jar - google-http-client-1.20.0.jar - google-http-client-jackson2-1.20.0.jar - google-http-client-jdo-1.20.0.jar - google-oauth-client-1.20.0.jar

tamtoum1987
  • 1,957
  • 3
  • 27
  • 56
  • Hi can you tell me what is stream2file(),JSON_FACTORY,am.open("*********114db0.p12"),**********ic1bgevf3h@developer.gserviceaccount.com, – PankajAndroid Jul 16 '15 at 16:31
  • 1
    Sorry for the delay i juste saw your message, :(, stream2file: @SuppressLint("NewApi") public static File stream2file(InputStream in) throws IOException { final File tempFile = File.createTempFile("okkk", null); tempFile.deleteOnExit(); FileOutputStream out = new FileOutputStream(tempFile); IOUtils.copy(in, out); return tempFile; } – tamtoum1987 Aug 07 '15 at 12:46
  • Do you have those libraries as Gradle? I use Android studio – Jay Apr 07 '16 at 18:44
  • 1
    Sorry i used eclipse for this, doing a quick search i found this library for gradle : may be it helps you ? compile 'com.google.api-client:google-api-client:1.20.0' compile 'com.google.oauth-client:google-oauth-client-jetty:1.20.0' compile 'com.google.apis:google-api-services-admin-directory:directory_v1-rev53-1.20.0' – tamtoum1987 Apr 11 '16 at 08:09
  • Sorry, but I am getting error as cannot found symbol StorageScopes and JSON_FACTORY. I have used following in gradle: compile 'com.google.api-client:google-api-client:1.20.0' compile 'com.google.apis:google-api-services-admin-directory:directory_v1-rev53-1.20.0' compile 'com.google.http-client:google-http-client-jdo:1.20.0' compile group: 'com.google.oauth-client', name: 'google-oauth-client', version: '1.20.0' – Pratibha Sarode Dec 12 '16 at 11:33
0
public class RetrieveFeedTask extends AsyncTask<Void, Void, String> {

    private Exception exception;

    protected String doInBackground(Void... params) {

        JsonFactory jsonFactory = new JacksonFactory();

        try {
            List<String> scopes = new ArrayList<String>();

            scopes.add(StorageScopes.DEVSTORAGE_FULL_CONTROL);
            HttpTransport httpTransport = new com.google.api.client.http.javanet.NetHttpTransport();

            //convert key into class File. from inputstream to file. in an aux class.
            File file =getTempPkc12File();


            //Google Credentianls
            GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport)
                    .setJsonFactory(jsonFactory)
                    .setServiceAccountId("xxxxxx-xxxx-xxx@xxxxx-xxxxxx.iam.gserviceaccount.com")
                    .setServiceAccountScopes((scopes))
                    .setServiceAccountPrivateKeyFromP12File(file)
                    .build();




            String URI = "https://storage.googleapis.com/" + GOOGLE_STORAGE_BUCKET+"/images/"+createImageFile();
            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.

            Resources res = getResources();
            Drawable drawable = res.getDrawable(R.drawable.camera);
            Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
            byte[] bitMapData = stream.toByteArray();


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


            HttpRequest putRequest;

            putRequest = requestFactory.buildPutRequest(url, contentsend);




            com.google.api.client.http.HttpResponse response = putRequest.execute();
            String content = response.parseAsString();
            Log.i("debug", "response is:"+response.getStatusCode());
            Log.i("debug", "response content is:"+content);
        } catch (IOException | GeneralSecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Log.i("ErrorUpload",""+e.toString());
        }
        return "";

    }

    protected void onPostExecute(String feed) {
        int i  = 0;
        int j = i;
        // TODO: check this.exception
        // TODO: do something with the feed
        Log.i("OnPost",""+feed);
    }
}


private  File getTempPkc12File() throws IOException {
    // xxx.p12 export from google API console
    InputStream pkc12Stream = (NewProduceInfo.this).getResources().getAssets().open("xxxxx-xxxxxxx-xxxxxx.p12");
    File tempPkc12File = File.createTempFile("temp_pkc12_file", "p12");
    OutputStream tempFileStream = new FileOutputStream(tempPkc12File);
    int read = 0;
    byte[] bytes = new byte[1024];
    while ((read = pkc12Stream.read(bytes)) != -1) {
        tempFileStream.write(bytes, 0, read);
    }
    return tempPkc12File;
}
  1. Blockquote
Jaba
  • 39
  • 3