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> ...

Google Map v2 signed map isssue fixed

Hi Step 1: Develop Map Application  using android studio Step 2: For Map viewing we need to have google api key .in order to get api key we need to register application in google api console  https://console.developers.google.com/apis/ Step 3; For getting SHA1 Key please follow the steps            1) Go the java installed folder location , open cmd                 Eg: C:\Program Files\Java\jdk1.7.0_04\bin            2)  Type                   Windows keytool -list -v -keystore "CHANGE WITH OUR KEYSTORE LOC" -alias ALIASNAME -storepass PASSWORD -keypass PASSWORD                   Mac & Linux  keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android Ref:  http://www.truiton.com/2015/04/obtaining-sha1...