问题描述
我想从我的Android应用程序上传图像谷歌云存储
。对于我搜索,发现 GCS JSON阿比
提供了这个功能。我做了很多的研究Android的样品这表明它的使用。在他们提供$的开发者网站C $只支持Java℃实例。我不知道如何使用API中的Android。我提到这和this联系,但未能获得太多的想法。请指导我如何,我可以使用这个API与Android应用程序。
I want to upload image on Google Cloud Storage
from my android app. For that I searched and found that GCS JSON Api
provides this feature. I did a lot of research for Android sample which demonstrates its use. On the developer site they have provided code example that only support java. I don't know how to use that API in Android. I referred this and this links but couldn't get much idea. Please guide me on how i can use this api with android app.
推荐答案
好球员,因此我解决了它,并得到了我的照片被上传到云存储都很好。这就是:
Ok guys so I solved it and got my images being uploaded in Cloud Storage all good.This is how:
注:我使用的XML API是pretty的大同小异
Note: I used the XML API it is pretty much the same.
第一,您需要下载大量的库。要做到这一点最简单的方法是创建一个Maven项目,让它下载所需要的所有的依赖关系。从这个示例项目:示例项目这些库应该是:
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 ProjectThe libraries should be:
二,你必须熟悉使用 API控制台云存储你必须创建一个项目,建立一个水桶,给斗权限,等等。你可以找到有关详情这里
Second, you must be familiar with Cloud Storage using the api consoleYou must create a project, create a bucket, give the bucket permissions, etc.You can find more details about that here
第三,一旦你把所有这些东西准备好,是时候开始编码。比方说,我们要上传图片:云存储可与OAuth的,这意味着你必须是经过认证的用户使用API。对于最好的办法是使用服务帐户的授权。不要担心,你需要做的唯一事情是在API控制台得到这样的服务帐户:
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:
我们将使用此服务帐户在我们的code。
We will use this service account on our code.
第四,让我们写一些code,可以说,上传图片到云存储。对于这个code工作,你必须投入的资产文件夹步骤3中的密钥生成,我把它命名为key.p12。
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.
有关API的更多信息请检查该链接云XML API
For more info on the api check this link Cloud XML API
这篇关于在Android中使用谷歌云存储JSON API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!