Skip to main content

Store Image to Google Cloud Store using Google App engine java

Here is sample code for uploading image files to cloud storage bucket in google cloud platform


Step 1 Create a google cloud project in cloud (https://console.cloud.google.com/)  enable cloud storage

Step 2: Create a new web application project
I am using Eclipse with Java for App engine Project

Step 3: create a POST web service method

the below code for upload image to the existing bucket in google cloud and the uploaded image url will be returned

Code



 @Path("/addres")
 @POST    
 @Consumes(MediaType.MULTIPART_FORM_DATA)  
 public String addRestaurant(
@FormDataParam("imgname") String imagename,           @FormDataParam("file") InputStream uploadedInputStream,        @FormDataParam("file") FormDataContentDisposition restImages    ) throws IOException  {
  try {
// Create your service object Storage storage = StorageOptions.getDefaultInstance().getService();
String bucketName = "abc-188706.appspot.com"; // Change this to something unique  // // Upload a Text blob to the  bucket
// BlobId blobId = BlobId.of(bucketName, "my_blob_name");
// BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();
// Blob blob = storage.create(blobInfo, "a simple blob".getBytes(UTF_8));
// Upload a Image blob to the  bucket
BlobId blobId = BlobId.of(bucketName, "IMG"+imagename); BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("image/jpeg").build();     try (WriteChannel writer = storage.writer(blobInfo)) {               byte[] buffer = new byte[1024];             int limit;             while ((limit = uploadedInputStream.read(buffer)) >= 0) {               try {    
             writer.write(ByteBuffer.wrap(buffer, 0, limit));               } catch (Exception ex) {                 ex.printStackTrace();               }             }         }         System.out.println("DONE:");
        BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
    BlobKey blobKey = blobstoreService.createGsBlobKey("/gs/" + bucketName + "/" + blobId.toString());        
 System.out.println(" DONE: "+blobKey.getKeyString()+" bobo id="+blobKey.getKeyString());

 
    BlobstoreService blobStore = BlobstoreServiceFactory.getBlobstoreService();
BlobKey blobKey1 = blobStore.createGsBlobKey("/gs/" + bucketName + "/" + blobId.getName()); // blobKey can be persisted  
ImagesService imagesService = ImagesServiceFactory.getImagesService();  
String url = imagesService.getServingUrl(ServingUrlOptions.Builder.withBlobKey(blobKey1));        
System.out.println(" imageUrl:"+url);     return url;     }catch(Exception e) {
System.err.println("errr:"+e);
e.printStackTrace();
}
return null;
}



Please Note 

The application need to be deployed in google cloud platform.the above code will not work in local instance

Also add the necessary jar files


Comments

Popular posts from this blog

Bluetooth Chat Example

Manifest File <manifest xmlns:android="http://schemas.android.com/apk/res/android"       package="com.example.android.BluetoothChat"       android:versionCode="1"       android:versionName="1.0">     <uses-sdk minSdkVersion="7" />     <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />     <uses-permission android:name="android.permission.BLUETOOTH" />     <application android:label="@string/app_name"                  android:icon="@drawable/app_icon" >         <activity android:name=".BluetoothChat"                   android:label="@string/app_name"                   android:configChanges="orientation|keyboardHidden">             <intent-filter> ...

Custom TextView With Images in Android

Create a custom XML Tag with image and text 1) Class extends Textview import java . util . regex . Matcher ; import java . util . regex . Pattern ; import android . content . Context ; import android . text . Spannable ; import android . text . style . ImageSpan ; import android . util . AttributeSet ; import android . util . Log ; import android . widget . TextView ; public class TextViewWithImages extends TextView { public TextViewWithImages ( Context context , AttributeSet attrs , int defStyle ) { super ( context , attrs , defStyle ); } public TextViewWithImages ( Context context , AttributeSet attrs ) { super ( context , attrs ); } public TextViewWithImages ( Context context ) { super ( context ); } @Override public void setText ( CharSequence text , BufferType type ) { Spannable s = getTextWithImages ( getContext (), text ); super . setText...

AcceleroMeter Sensors with SurfaceMovements on Canvas

MainActivity.java import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; public class MainActivity extends Activity implements SensorEventListener { float x, y, sensorX, sensorY; Bitmap ball; SensorManager sm; Sensor s; MyBringBackSurface ourSurfaceView; /**  * Canvas Movement  */ public class MyBringBackSurface extends SurfaceView implements Runnable {     SurfaceHolder ourHolder;     Thread ourThread = null;     boolean isRunning = false;     public MyBringBackSurface(Context context) {         super(context)...