上传图片到谷歌云存储与应用程序的Andr​​oid上传图片、应用程序、oid、到谷歌云

2023-09-06 07:11:40 作者:没人心疼自己疼

我想上传图片到谷歌云存储。结果我发现了这样一个例子 https://github.com/pliablematter/simple-cloud-storage,说明如何做到这一点。结果我创建了一个项目水桶等,但是当我尝试创建一个客户端ID我得到这个错误:

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 later

在哪里从何而来这个错误?结果也许有上传文件到谷歌云存储与Android应用程序的另一种方法?

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

_______________________________ EDIT___________________________________________在新的控制台,我可以看到一条消息,告诉我,只有项目的所有者可以创造客户应用程序的网络和客户服务。所以,因为我用的帐户colaborator连接错误造成

_______________________________EDIT___________________________________________ in the new console i can see a message telling me only owner of the project can creat clients for the application web and account services. So error is caused because i connect with a account colaborator

_________________________________编辑___________________________________________现在,我可以创建客户端ID,但我不知道我可以在Android上载文件水桶,我读到这的 https://github.com/pliablematter/simple-cloud-storage 但它是Java的Andr​​oid的不是,任何人有一个例子,我该怎么办呢?

_________________________________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 a example how can i do that ?

任何帮助将AP preciated

Any help will be appreciated

推荐答案

我终于可以在谷歌存储这样的图片上传

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
    }
}

不要忘了下载罐子,把它放在lib文件夹:  - com.fasterxml.jackson.core.jar  - 谷歌的API客户端,1.20.0.jar  - 谷歌的API服务,存储v1beta2-rev21-1.15.0-rc.jar  - 谷歌-HTTP客户端,1.20.0.jar  - 谷歌的HTTP客户端,jackson2-1.20.0.jar  - 谷歌的HTTP客户端JDO-1.20.0.jar  - 谷歌OAuth的客户1.20.0.jar

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