简体   繁体   English

如何从独立的Java应用程序(而不是从GAE网络应用程序)连接到本地Google Cloud Storage?

[英]How to connect to the local google cloud Storage from standalone java application (not from a GAE web-app)?

I've GAE application which creates some data in the Google Cloud Datastore and stores some binary files into the Google Cloud Storage - let's call the application WebApp . 我有GAE应用程序,它可以在Google Cloud Datastore中创建一些数据并将一些二进制文件存储到Google Cloud Storage中-我们将其称为WebApp应用程序 Now I have a different application running on the Google compute engine. 现在,我在Google计算引擎上运行了另一个应用程序。 Let's call the application ComputeApp . 让我们将应用程序称为ComputeApp

The ComputeApp is a backend process which is processing data created by the WebApp . ComputeApp是一个后端过程,正在处理由WebApp创建的数据。 I asked here in this question previously which API can I use to communicate with Storage from the ComputeApp . 我之前在此问题中过,我可以使用哪个API通过ComputeApp与Storage通信。 I implemented the Storage communication using of the Google Cloud Storage JSON API Client Library for Java . 我使用JavaGoogle Cloud Storage JSON API客户端库实现了存储通信。

Everything works fine as far as I'm communicating with the Storage in the Google cloud. 就我与Google云中的存储进行通信而言,一切正常。 I'm using the service account authentication. 我正在使用服务帐户身份验证。

Now I need to run my ComputeApp locally, in my development PC so I'll take data created by my local WebApp and stored into the local debug Storage. 现在,我需要在开发PC上本地运行ComputeApp,以便将本地WebApp创建的数据存储到本地调试存储中。 I need it because I want to have a testing environment so I can debug may app locally. 我需要它是因为我想拥有一个测试环境,以便可以在本地调试may app。 My WebApp running locally stores binary data to the local Datastore. 我在本地运行的WebApp将二进制数据存储到本地数据存储中。 I can see it through the local admin console: (localhost:8080/_ah/admin). 我可以通过本地管理控制台查看它:(localhost:8080 / _ah / admin)。 There is list of entities GsFileInfo and a list of the __ah_FakeCloudStorage... entities, representing my storage data. 有实体列表GsFileInfo和__ah_FakeCloudStorage ...实体列表,它们代表我的存储数据。

How should I modify my ComputeApp code to force it to connect to my local debug Storage and access these binary data stored locally instead of connect to the Google cloud? 我应如何修改ComputeApp代码以强制其连接到本地调试存储并访问本地存储的这些二进制数据,而不是连接到Google云?

Create account in Google Project Console, and Service account credentials 在Google Project Console中创建帐户和服务帐户凭据

Initialize: 初始化:

private static final String APPLICATION_NAME = "your-webapp";
      private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();

       GoogleCredential credential = GoogleCredential.getApplicationDefault();
              if (credential.createScopedRequired()) {
                credential = credential.createScoped(StorageScopes.all());
              }
              HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
              storageService = new Storage.Builder(httpTransport, JSON_FACTORY, credential)
                  .setApplicationName(APPLICATION_NAME).build();



   InputStreamContent mediaContent = new InputStreamContent(contentType, inputStream);


    StorageObject objectMetadata = null;
      objectMetadata = new StorageObject()
          .setName(name)
          .setMetadata(ImmutableMap.of("date", new Date().toString()))
          .setAcl(ImmutableList.of(
             new ObjectAccessControl().setEntity("allUsers").setRole("READER")

              ))
          .setContentDisposition("attachment");


    Storage.Objects.Insert insertObject = storageService.objects().insert("staging.your-app.appspot.com", objectMetadata,
        mediaContent);


    // For small files, you may wish to call setDirectUploadEnabled(true), to
    // reduce the number of HTTP requests made to the server.
    if (mediaContent.getLength() > 0 && mediaContent.getLength() <= 2 * 1000 * 1000 /* 2MB */) {
      insertObject.getMediaHttpUploader().setDirectUploadEnabled(true);
    }

    insertObject.execute();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NON-GAE Java应用中的Google Cloud Storage - Google Cloud Storage in NON-GAE Java app 即使使用BlobstoreService从GAE托管应用程序上传了文件,Google Cloud Storage也会返回404 - Google Cloud Storage returns 404 even file is uploaded from GAE hosted application using BlobstoreService 通过Google App Engine(Java)将文件上传到Google云端存储 - Upload file to Google cloud storage from Google App Engine (Java) 无法使用GCS客户端库+ java将文件从GAE项目上传到Google云存储 - Not able to upload files from GAE project to Google cloud Storage using GCS Client library+java 从xml JSF / Java配置web-app数据目录 - Config web-app data directory from xml JSF/Java 使用GAE和Google Cloud Storage从CSV文件上传到存储桶中的图片 - Upload to bucket pictures from a csv file with GAE and Google Cloud Storage 如何在本地开发环境中从App Engine访问Google云端存储? - How to access Google Cloud Storage from App Engine in local development environment? 从Java登录到Twitter和Twitter(网络应用程序) - Login to twitter and tweet from Java (web-app) 从App Engine Java将urlconnection文件保存到Google云存储 - saving a urlconnection file to google cloud storage from app engine java 我可以使用普通Web应用程序(而非GAE应用程序)中的GCS和Cloud Sql API吗 - Can I use GCS and Cloud Sql APIs from normal Web Application (not GAE app)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM