Getting Started (Google Cloud Storage Client Library)

Source: Internet
Author: User
Tags finally block

Before you run the following steps, make sure that:

1. Your project has activated Google Cloud Storage and app Engine, including the creation of at least one Cloud Storage bucket.

2. You have downloaded the client library and unzipped it.

3. You have installed and configured the latest app Engine Java SDK.

Run Localexample.java Example

Localexample.java is a non-deployment example that helps you quickly test and investigate the capabilities of the cloud storage. In addition to the Eclipse console output it does not have a UI component. (Cloud Storage Client library deployable samples with UI is also available)

Run Localexample.java in eclipse

1. Start Eclipse

2. In eclipse, click Windows->preferneces->google->app Engine and click Add.

3. Follow the prompts, provide the installation path of the app Engine SDK, and click OK.

4. From the Files menu, click Files->new->java Project to create a project called Localexample and use the package namecom.google.appengine.demos。

5. Select the project in the Package Explorer, click Files->new->class and give the Class a name of Localexample, and use the Com.google.appengine.demos.

6. Copy the contents of the Localexamplejava source into this class file.

7. Select the project again in the Package explorer and right-click on the Properties->java Build Path.

8. In Libraries tab, click Add External Jars. The following jars must be appended:

Appengine-gcs-client.jar from the Cloud Storage client library you installed

Guava-15.0.jar from the Cloud Storage client library you installed

Joda-time-2.3.jar from the Cloud Storage client library you installed

Appengine-testing.jar from the app Engine installation subdirectory/lib/testing

Appengine-api.jar from the app Engine installation subdirectory/lib/impl

Appengine-api-stubs.jar from the app Engine installation subdirectory/lib/impl

9. Compiling, running run as-> Java application

10. See the following output in the Eclipse console:

Survey Localexample.java Examples

Localexample.java is described in detail below, explaining the use of the Cloud Storage client library.

Imports

There are some uses of imports that you may not need, or local tests will use. An example of imports is listed below.

Importcom.google.appengine.tools.cloudstorage.GcsFileOptions;ImportCom.google.appengine.tools.cloudstorage.GcsFilename;ImportCom.google.appengine.tools.cloudstorage.GcsInputChannel;ImportCom.google.appengine.tools.cloudstorage.GcsOutputChannel;ImportCom.google.appengine.tools.cloudstorage.GcsService;Importcom.google.appengine.tools.cloudstorage.GcsServiceFactory;ImportCom.google.appengine.tools.cloudstorage.RetryParams;ImportCom.google.appengine.tools.development.testing.LocalBlobstoreServiceTestConfig;ImportCom.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;ImportCom.google.appengine.tools.development.testing.LocalServiceTestHelper;Importjava.io.IOException;ImportJava.io.ObjectInputStream;ImportJava.io.ObjectOutputStream;ImportJava.nio.ByteBuffer;ImportJava.nio.channels.Channels;Importjava.util.Arrays;ImportJava.util.HashMap;ImportJava.util.Map;

The following is a brief introduction to these imports:

Com.google.appengine.tools.cloudstorage.* allows us to use the Cloud Storage client library.

com.google.appengine.tools.development.testing.*Required only for local unit testing of certain app engine features.

java.io.ObjectInputStream 和java.io.ObjectOutputStreamUsed to read and write objects.

Java.nio.ByteBuffer for non-buffered read and write

Java.io.IOException Required for error handling (although not listed)

Java.nio.channels.Channels for converting an input-output channel to a stream

Java.nio.channels.ReadableByteChannel for reading data from the cloud storage

Create a gcsservice to send the request

To send and receive requests from the library to cloud Storage, you need a Gcsservice instance:

Private Final Gcsservice gcsservice =    gcsservicefactory.creategcsservice (Retryparams.getdefaultinstance ());

In this fragment, note the use of retryparams in Creategcsservice. As shown above, createGcsService(RetryParams.getDefaultInstance()) this sets the default retry configuration and retries when a time-out or unexpected error occurs while accessing cloud storage. To specify a different value, such as the maximum number of retries, you use Retryparams.builder to change the configuration in a new Retryparams object and provide it when you create the Gcsservice. Note that once the Gcsservice object is created, its retry parameters cannot be changed.

You can create any number of Gcsservice instances. Each instance is independent, immutable (and therefore thread-safe), reusable. For example, one can use one of the parameter settings to write a file, while the other can read different files using different retry parameters.

A recommended practice is to use separate instances in each of your I/O classes. This will help keep your class independent, as well as less overhead.

Write data to cloud storage

The following example shows how to write data to a cloud storage file. A separate fragment provides write serializable object data and a byte array.

The Cloud storage file will not be fully created until close is called

In the following fragment, close is not present in the finally block, as is often done in Java. So when there is an exception, any part that is written will be automatically erased.

Write objects to cloud storage

Here is a description of how to serialize the object to the cloud Storage file. First, get a writable byte channel:

Gcsoutputchannel Outputchannel =    gcsservice.createorreplace (FileName, Gcsfileoptions.getdefaultinstance () );

is called in this fragment GcsService.createOrReplace , gcsfilename as the first parameter. This object contains the name of the bucket to be used and the name of the object. If a bucket already has an object of the same name, and your app has write permissions, the call overwrites the existing file (Cloud storage does not support attaching). If there is no file with that name, the result of this call is to create a new file.

The second parameter is gcsfileoptions. To use the default options, use theGcsFileOptions.getDefaultInstance。要使用自己的设置,使用GcsFileOptions.Builder来设置选项并创建一个GcsFileOptions作为第二个参数。

This file option is passed to cloud storage to tell it the text type of the file, the user metadata that you want to pass in the header, the file access management ACL, and so on. If you do not provide a text type (mimeType), this file will be provided by the cloud storage with the default MIME type (serve), currently using Binary/octet-stream. If you do not specify an ACL, the assigned object access permission is the current default object ACL.

Note that all file option information can be obtained from an object by calling Gcsservice.getmetadata (FileName) after a close, without requiring it to download the object itself.

For more configuration information, browse the ACLs and file options for the cloud storage document.

Now use an output stream to write the data

@SuppressWarnings ("resource"=    new  ObjectOutputStream (Channels.newoutputstream ( Outputchannel)); Oout.writeobject (content); Oout.close ();

Write a byte array to the cloud storage file

Here's how to write a byte array to the cloud storage file.

@SuppressWarnings ("resource"=    gcsservice.createorreplace (FileName, Gcsfileoptions.getdefaultinstance ()); Outputchannel.write (Bytebuffer.wrap (content)); Outputchannel.close () ;

For a description of the parameters used in the Createorreplace call, see the instructions under writing an Object to Cloud storage.

Reading data from the cloud storage

The following example shows how to read data from a file stored in cloud storage. Separate fragments provide read cloud storage files to Objects (serialization) and byte arrays.

Read cloud storage to an object

This method is useful for reading large files into a buffer.

Gcsinputchannel Readchannel = Gcsservice.openprefetchingreadchannel (fileName, 0, 1024 * 1024);

This call Gcsservice.openprefetchingreadchannel has a gcsfilename that contains the name of the bucket to read and the object to be read. The second parameter is the byte at which the file starts to read, and 0 indicates that it starts at the beginning of the file. If you provide other starting positions in the file, such as Byte 300, the read operation starts at byte 300 and ends at the end of the file, or until you stop reading. (The read-ahead buffer will read first, and it will contain bytes that exceed the position you stopped)

Read-Ahead is a major advantage for most applications, because when more data is being downloaded in the background, part of the file is allowed to be processed in parallel.

The third parameter is the read-ahead buffer size, which is set to 1MB in this example.

Now, read the file from the channel:

Try New ObjectInputStream (Channels.newinputstream (Readchannel))) {  return  oin.readobject ();}

Read data to byte array

For small files, you can read all the files in a byte array at a time:

int fileSize = (int= bytebuffer.allocate (fileSize); Try (Gcsinputchannel Readchannel = Gcsservice.openreadchannel (fileName, 0)) {  readchannel.read (result);}

In the above fragment, note that the use of java.nio.ByteBuffer, especially the size of the buffer, is equal to the size of the file to be read from the channel.

Note that for most applications, the preferred method is to read (stream) the file (read the data into the object) because it does not need to save all the data in memory at once.

Example of a deployable cloud Storage client library with UI

For a deployable example with a UI, browse the code:

Gcsexampleservlet.java upload a download file from the cloud storage

Portoffilesapiguestbookservlet.java is the port for the example program, which used the now obsolete files API. For more information on migration, see migrating from the Files API.

You need to compile this example before running and deploying on development server.

Compile This example:

1. Checkout the code from the terminal

SVN checkout http://appengine-gcs-client.googlecode.com/svn/trunk/appengine-gcs-client-read-only 

2. Change the directory toppengine-gcs-client-read-only/java

3. Call Ant Compile_example, which compiles the example with Build.xml in the directory. For more information on using Apache ant, see Using Apache Ant.

4. Call the following command to run Dev server:

/path/to/appengsdk/dev_appserver.sh  /path/to/example/war

5. Browse in your browserlocalhost:8080。看到:

Getting Started (Google Cloud Storage Client Library)

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.