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
@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;
}
Also add the necessary jar files
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
@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
Post a Comment