Blobstore Java API Overview

Source: Internet
Author: User
Tags http post

The Blobstore API allows your application to use data objects called blobs (serve). This data object is much larger than the size of objects allowed by the Datastore service. BLOBs can effectively serve large files such as video and images, allowing users to upload big data files. Upload a file via HTTP request to create a blobs. Typically, your application is implemented by presenting a table (form) to the user, and a file upload area (a upload field). When this table is submitted, Blobstore creates a blob from the contents of the file and returns an opaque reference to the BLOB called Blob key. You can use it later with this blob (serve). The application can provide (serve) This full BLOB value to the corresponding user's request, or you can use a class flow file interface (streaming File-like interface) to read the BLOB value directly.

Blobstore Introduction

Gae includes the Blobstore service, which allows applications to use (serve) data objects, limited by the number of data, and can be uploaded via an HTTP connection. These objects are called Blobstore values or blobs. Blobstore values are used as responses from request processing and are created via Web Forms uploads (Blobstore values is served as response from request handlers and is Crea Ted as uploads via Web Forms). The app does not directly create BLOB data; BLOBs is created indirectly through a committed Web form or HTTP POST request. Blobstore values can be used by the user or accessed by the application in the class file stream. (using the Blobstore API)

Note: The blobs created by the Blobstore service has no relation to the Blob attribute value used by datastore.

Your app prompts the user to upload a Blobstore value by presenting a Web form and file upload area. The app generates the action URL for the table by calling the Blobstore API. The user's browser uploads the file directly to Blobstore via this generated URL. Then Blobstore store the blob, rewrite the request to include the BLOB key, and pass it to a path to your app. A request processing on that path of your app can perform additional table processing.

In order to use (serve) a blob, your app sets a header in the output response, and app engine replaces the response with this BLOB value.

BLOBs cannot be modified after it is created, but can be deleted. Each blob has a corresponding BLOB info record. It is stored in Datastore, which contains the details of the blob, such as its creation time and text type. You can use this BLOB key to get the BLOB info record and query their properties.

An application can use an API call to read a Blobstore value part at a time. The size of this section can be the maximum size of an API return value. This size is less than 32M and is represented in Java by Com.google.appengine.api.blobstore.BlobstoreService.MAX_BLOB_FETCH_SIZE. The app is not able to create and modify Blobstore values unless the user uploads the file.

Using Blobstore

Apps can use Blobstore to accept large files uploaded from users and to use (serve) these files. Once the file is uploaded, it is called blobs. The app does not directly access blobs. Instead, it works by using the BLOB info entity in Datastore (rendered as the Blobinfo Class).

The user creates a BLOB by submitting an HTML table that contains one or more file input fields. Your app is set blobstoreService.createUploadUrl() as the target address (action) of this table, which is passed to this function in the application URL path of a processing (handler). When the user submits this form, the user's browser uploads the specified file directly into the Blobstore. Blobstore rewrites the user's request and stores the uploaded file data, replacing the uploaded file data with one or more corresponding blob keys, and then passing the rewritten request to the Blobstoreservice.createuploadurl () that you provided to the Processing on the URL (handler). This processing (handler) can handle additional processing (processing) based on BLOB key.

The application can use the class file stream interface to read the portion of Blobstore value. See Blobstoreinputstream class.

Upload a blob

To create and upload a blob, follow these steps.

1. Create a upload URL

Called blobstoreService.createUploadUrl() to create a upload URL for a table populated by the user, and when the post of the form is completed, the path of the application is passed to load.

<Body>    <formAction= "<%= blobstoreservice.createuploadurl ("/upload ")%>"method=" post "enctype=" Multipart/form-data "><inputtype= "File"name= "MyFile">        <inputtype= "Submit"value= "Submit">    </form></Body>

Note that this is what the upload form looks like if it is created with a JSP.

2. Create an Upload form

This table must contain the File upload field and the enctype of the table must be set to Multipart/form-data. When the user submits this form, the post is processed by the Blobstore API and the BLOB is created. This API also creates an info record for the Blob and stores the record in Datastore, passing the rewrite request to your app on the given path [as a blob key].

3. Implement upload processing.

In this process, you can store blob key with the rest of your app's data model. This BLOB key can be accessed in the Datastore BLOB info entity. Note that after the user submits the table and your processing is called, the BLOB has been saved and BLOB info is added to the datastore. If your app doesn't want to keep the blob, you should immediately delete the BLOB to prevent it from becoming orphaned.

In the following code, GETUPLOADS returns the BLOBs collection that has been uploaded. The Map object is a list that associates the name and blobs of the upload field.

Map<String, List<blobkey>> BLOBs = blobstoreservice.getuploads (req); List<blobkey>  blobkeys = Blobs.get ("MyFile"); if (Blobkeys = = NULL | | Blobkeys.isempty ()) {    res.sendredirect ("/");} else {    res.sendredirect ("/serve?blob-key=" + blobkeys.get (0) . getkeystring ());}

When Blobstore rewrites the user's request, the MIME portion of the uploaded file clears their bodies, and the BLOB key is added as the head of the MIME section. All other table fields and sections are saved, passed to upload processing (upload handler). If you do not specify a text type, Blobstore will attempt to infer from the file's extension. If the text type is not determined, the text type of the newly created BLOB is assigned as Application/octet-stream.

Use (serve) a blob

Note: If you use the (serve) image, a more effective, and possibly cheaper, approach is to use the app Engine images API instead of Blobstoreservice.serve () Getservingurl (). This Getservingurl () method allows you to use the (serve) image directly, without having to go through your app Engine instance.

In order to use (serve) BLOBs, you must include a download processing [as a path] in the application. This processing should pass the BLOB key of the desired blob toblobstoreService.serve(blobKey, res)。在这个例子中,这个blob key作为URL参数被传递给下载处理(req.getParameter(‘blob-key‘))。在实践中,下载处理可以通过多种方式获得blob key。比如通过其他方法或用户操作(user action)。

 Public void doget (httpservletrequest req, httpservletresponse res)     throws IOException {        new Blobkey (Req.getparameter ("Blob-key"));        Blobstoreservice.serve (Blobkey, res);

Blobs can served from any application URL. To serve a blob in your application, place a special header in the response that contains the BLOB key. APP engine uses BLOB text instead of the body of the response.

BLOB byte range

Blobstore supports providing (serve) a large portion of the value instead of all of the values in response to a request. To provide partial values, include the X-appengine-blobrange header in the output response. Its value is a standard HTTP byte range. This byte number starts from 0. The blank x-appengine-blobrange indicates that the API ignores this range header and provides the entire BLOB. Examples of this range include:

0-499 provides the first 500 bytes of value.

500-999 provides 500 bytes starting from the No. 501 byte.

500-provides all bytes starting from the No. 501 byte to the last.

-500 provides the last 500 bytes of value.

If this byte range is valid for Blobstore value, then blobstore emits a 206 partial content status code and the requested byte range to the client. If this range is not valid, Blobstore emits 416 requested range not satisfiable.

Blobstore does not support multibyte ranges in a request (for example, 100-199,200-299), regardless of whether they overlap.

A complete example application

In the following example application, the application's main URL is loaded to the user looking for a file upload form, and the upload process immediately invokes the download process to provide (serve) data. This is to simplify the example application. In practice, you may not use the main URL to request the uploading of data, nor do you immediately provide the blob you just uploaded.

//file Upload.javaImportjava.io.IOException;Importjava.util.List;ImportJava.util.Map;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportCom.google.appengine.api.blobstore.BlobKey;ImportCom.google.appengine.api.blobstore.BlobstoreService;Importcom.google.appengine.api.blobstore.BlobstoreServiceFactory; Public classUploadextendsHttpServlet {PrivateBlobstoreservice Blobstoreservice =Blobstoreservicefactory.getblobstoreservice (); @Override Public voidDoPost (httpservletrequest req, httpservletresponse Res)throwsservletexception, IOException {Map<string, list<blobkey>> blobs =blobstoreservice.getuploads (req); List<BlobKey> Blobkeys = Blobs.get ("MyFile"); if(Blobkeys = =NULL||Blobkeys.isempty ()) {Res.sendredirect ("/"); } Else{res.sendredirect ("/serve?blob-key=" + blobkeys.get (0). getkeystring ()); }    }}//file Serve.javaImportjava.io.IOException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportCom.google.appengine.api.blobstore.BlobKey;ImportCom.google.appengine.api.blobstore.BlobstoreService;Importcom.google.appengine.api.blobstore.BlobstoreServiceFactory; Public classServeextendsHttpServlet {PrivateBlobstoreservice Blobstoreservice =Blobstoreservicefactory.getblobstoreservice (); @Override Public voiddoget (httpservletrequest req, httpservletresponse Res)throwsIOException {blobkey Blobkey=NewBlobkey (Req.getparameter ("Blob-key"));        Blobstoreservice.serve (Blobkey, RES); }}//file index.jsp<%@ pageImport= "Com.google.appengine.api.blobstore.BlobstoreServiceFactory"%><%@ pageImport= "Com.google.appengine.api.blobstore.BlobstoreService"%><%Blobstoreservice Blobstoreservice=Blobstoreservicefactory.getblobstoreservice ();%>//Web. XML<?xml version= "1.0" encoding= "Utf-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns= "Http://java.sun.com/xml/ns/javaee"Xmlns:web= "Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "version=" 2.5 "><servlet> <servlet-name>Upload</servlet-name> <servlet-class>upload</servlet-class> </servlet> <servlet> <servlet-name>Serve</servlet-name> <servlet-class>serve</servlet-class> </servlet> <servlet-mapping> <servlet-name>Upload</servlet-name> <url-pattern> /upload</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>serve</ servlet-name> <url-pattern>/serve</url-pattern> </servlet-mapping> </web-app>

Use the images service with Blobstore

The images service can use Blobstore value as the source of the transformation. The source image can be as large as the maximum size of Blobstore value. Image Service still returns the converted image to the app, so the converted images must be less than 32M. This is useful for generating thumbnails of large images uploaded by users.

For information about using image services with Blobstore, see image Service documentation.

Use the Blobstore API with Google Cloud storage

You can use the Blobstore API to present blobs in Google Cloud storage instead of Blobstore. You need to create something called a bucket in the Google Cloud storage document. Specify the bucket in the Blobstoreservice createuploadurl and specify the bucket name in the uploadoptions parameter. In your upload processing (upload handler), you need to process the returned FileInfo metadata and store the Google Cloud storage file name in the display. This file name is then needed to retrieve the blob.

You can also use the Blobstore API with Google Cloud Storage. The following code snippet shows how to implement:

This example is in a request processing, in which the bucket name and object name are obtained. It creates the Blobstore service, uses it and provides buckets and object names to create blob keys for Google Cloud storage.

Blobstoreservice Blobstoreservice == Blobstoreservice.creategsblobkey (    "/gs/" + Filename.getbucketname () + "/" + Filename.getobjectname ()); Blobstoreservice.serve (Blobkey, resp);

Note: Once obtain a for blobKey the Google Cloud Storage object, you can pass it around, serialize it, and otherwise use it in terchangeably anywhere you can use a for blobKey objects stored in Blobstore. This allows for usage where a app stores some data in Blobstore and some in Google Cloud Storage, but treats the data oth Erwise identically by the rest of the app. (However, BlobInfo objects is currently not available for Google Cloud Storage ob Jects.)

Write a file to Blobstore (obsolete) using the Files API

Warning: The files API used here for writing to Blobstore is obsolete and will be removed. See the Files API Service turndown

。。。。。。

Quotas and restrictions

Blobstore values use space contributes to Stored Data (billing) quotas.

The number of BLOB info entities in Datastore is a datastore-related limitation.

The security quotas for the entire system are described in the Quota Details section of quotas,the Administration console.

There is a limit to the use of blobstore for the security quotas for the entire system. The maximum size of blobstore data that can be read by an API call is 32M.

Source Address: Blobstore Java API Overview

Blobstore Java API Overview

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.