Which are uri files for GAE java cloud storage emulation using the GCS client library?

I am developing a web application using the Google app engine for Java. I will use the Google Cloud storage and according to the documentation , I use the GCS client library to emulate the cloud storage on the local drive.

I have no problems saving files, I see them from eclipse under the war folder (along the WEB-INF / appengine path-generated), and I can see them from the web admins panel, accessible from the URL

local: 8888 / _ah / admin

as stated in this question

My question is the following. What are the URI files under the local host to access them with GCS emulation?

An example of one of the downloaded files on localhost:

  • Key file - aglub19hcHBfaWRyJwsSF19haF9GYWtlQ2xvdWRTdG9yYWdlX18xIgpxcmNvZGUuanBnDA
  • Identifier / Name encoded_gs_key: L2dzLzEvcXJjb2RlLmpwZw
  • filename is / gs / 1 / qrcode.jpg

Thanks in advance.

+4
source share
3 answers

You can see how this is done here: https://code.google.com/p/appengine-gcs-client/source/browse/trunk/java/src/main/java/com/google/appengine/tools/cloudstorage /dev/LocalRawGcsService.java

To date, this mapping is supported using a local data store. This may change in the future, but you should be able to simply call this class or one of the higher-level classes provided by the GCS client to get the data.

+3
source

Using getServingUrl ()

The local gcs file is saved in blob format. When saving it, I can use a location, for example, your file name "/gs/1/qrcode.jpg" However, when accessing it, this fake location does not work. I have found a way. It may not be the best, but it works for me.

BlobKey bk = BlobstoreServiceFactory.getBlobstoreService().createGsBlobKey(location); String url = ImagesServiceFactory.getImagesService().getServingUrl(bk); 

The URL will look like this:

 http://127.0.0.1:8080/_ah/img/encoded_gs_key:yourkey 

(I hardly found any direct google search solution. I hope this answer helps others who need it.)

Resource: ImagesServiceFactory ImageService FileServiceFactory

+3
source

For those who want to service the local GCS files that were created in the GAE GCS library, one solution is to expose the Java servlet as follows:

 package my.applicaion.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.google.appengine.api.blobstore.BlobKey; import com.google.appengine.api.blobstore.BlobstoreService; import com.google.appengine.api.blobstore.BlobstoreServiceFactory; public final class GoogleCloudStorageServlet extends HttpServlet { @Override protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException { final BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService(); final String fileName = "/gs" + request.getPathInfo(); final BlobKey blobKey = blobstoreService.createGsBlobKey(fileName); blobstoreService.serve(blobKey, response); } } 

and in your web.xml:

 <servlet> <servlet-name>GoogleCloudStorage</servlet-name> <servlet-class>my.applicaion.servlet.GoogleCloudStorageServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>GoogleCloudStorage</servlet-name> <url-pattern>/gcs/*</url-pattern> </servlet-mapping> 

If you host this servlet in your GAE application, the URL to access the GCS file with bucket-name and fileName is http://localhost:8181:/gcs/bucket-name/fileName , then the server’s local port number GAE's development is 8181 .

It works, at least from GAE v1.9.50.

And if you intend to use the local GCS server in unit test with Jetty, here is the work, hopefully with the right comments:

  final int localGcsPortNumber = 8081; final Server localGcsServer = new Server(localGcsPortNumber); final ServletContextHandler context = new ServletContextHandler(ServletContextHandler.NO_SESSIONS); final String allPathSpec = "/*"; context.addServlet(new ServletHolder(new HttpServlet() { @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { final BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService(); final String fileName = "/gs" + request.getRequestURI(); final BlobKey blobKey = blobstoreService.createGsBlobKey(fileName); if (blobKey != null) { // This is a work-around over the "ServeBlobFilter" which does not take the "Content-Type" from the "blobInfo", but attempts to retrieve it from the "blobKey" final BlobInfo blobInfo = BlobStorageFactory.getBlobInfoStorage().loadGsFileInfo(blobKey); if (blobInfo != null) { final String contentType = blobInfo.getContentType(); if (contentType != null) { response.addHeader(HttpHeaders.CONTENT_TYPE, contentType); } } } blobstoreService.serve(blobKey, response); } }), allPathSpec); // The filter is responsible for taken the "blobKey" from the HTTP header and for fulfilling the response with the corresponding GCS content context.addFilter(ServeBlobFilter.class, allPathSpec, EnumSet.of(DispatcherType.REQUEST)); // This attribute must be set, otherwise a "NullPointerException" is thrown context.getServletContext().setAttribute("com.google.appengine.devappserver.ApiProxyLocal", LocalServiceTestHelper.getApiProxyLocal()); localGcsServer.setHandler(context); localGcsServer.start(); 
0
source

Source: https://habr.com/ru/post/1486548/


All Articles