com.google.appengine.api.blobstore.BlobstoreServiceFactory Java Examples
The following examples show how to use
com.google.appengine.api.blobstore.BlobstoreServiceFactory.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BlobserviceServeServlet.java From appengine-tck with Apache License 2.0 | 6 votes |
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { BlobstoreService service = BlobstoreServiceFactory.getBlobstoreService(); String blobKey = request.getParameter("blobKey"); ByteRange range = service.getByteRange(request); String blobRange = request.getParameter("blobRange"); String blobRangeString = request.getParameter("blobRangeString"); BlobKey key = new BlobKey(blobKey); if (range != null) { service.serve(key, range, response); } else if (blobRange != null) { service.serve(key, new ByteRange(Long.parseLong(blobRange)), response); } else if (blobRangeString != null) { service.serve(key, blobRangeString, response); } else { service.serve(key, response); } }
Example #2
Source File: GcsBlobstoreUploadTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@Test @InSequence(100) public void testCreateGsBlobKey() throws Exception { final long ts = System.currentTimeMillis(); final byte[] bytes = "FooBar".getBytes(); GcsService service = GcsServiceFactory.createGcsService(); GcsFilename filename = new GcsFilename("GcsBucket", String.valueOf(ts)); GcsFileOptions options = new GcsFileOptions.Builder().mimeType(CONTENT_TYPE).build(); try (GcsOutputChannel out = service.createOrReplace(filename, options)) { IOUtils.copy(Channels.newChannel(new ByteArrayInputStream(bytes)), out); } BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService(); BlobKey key = blobstoreService.createGsBlobKey("/gs/GcsBucket/" + ts); byte[] fetched = blobstoreService.fetchData(key, 0, 10); Assert.assertArrayEquals(bytes, fetched); }
Example #3
Source File: DownloadServlet.java From solutions-photo-sharing-demo-java with Apache License 2.0 | 5 votes |
@Override public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException { String user = req.getParameter("user"); String id = req.getParameter("id"); Long photoId = ServletUtils.validatePhotoId(id); if (photoId != null && user != null) { Photo photo = AppContext.getAppContext().getPhotoManager().getPhoto(user, photoId); BlobKey blobKey = photo.getBlobKey(); BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService(); blobstoreService.serve(blobKey, res); } else { res.sendError(400, "One or more parameters are not set"); } }
Example #4
Source File: GalleryServiceImpl.java From appinventor-extensions with Apache License 2.0 | 5 votes |
/** * It will return a dev server serving url for given image url * @param url image url */ @Override public String getBlobServingUrl(String url) { BlobKey bk = BlobstoreServiceFactory.getBlobstoreService().createGsBlobKey(url); String u = null; try { u = ImagesServiceFactory.getImagesService().getServingUrl(bk); } catch (Exception IllegalArgumentException) { LOG.info("Could not read blob"); } return u; }
Example #5
Source File: PhotoServiceManager.java From solutions-photo-sharing-demo-java with Apache License 2.0 | 5 votes |
public String getUploadUrl() { BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService(); UploadOptions uploadOptions = null; String bucket = configManager.getGoogleStorageBucket(); if (bucket == null || bucket.isEmpty()) { uploadOptions = UploadOptions.Builder.withDefaults(); } else { uploadOptions = UploadOptions.Builder.withGoogleStorageBucketName(bucket); } return blobstoreService.createUploadUrl(configManager.getUploadHandlerUrl(), uploadOptions); }
Example #6
Source File: UploadHandlerServlet.java From appengine-tck with Apache License 2.0 | 5 votes |
@Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { BlobstoreService blobstore = BlobstoreServiceFactory.getBlobstoreService(); lastUploadedBlobKey = getFirst(blobstore.getUploads(request)); lastUploadedBlobInfo = getFirst(blobstore.getBlobInfos(request)); lastUploadedFileInfo = getFirst(blobstore.getFileInfos(request)); BlobKey tmp = lastUploadedBlobKey; if (tmp != null) { response.getWriter().write(tmp.getKeyString()); } }
Example #7
Source File: LocalRawGcsServiceTest.java From appengine-gcs-client with Apache License 2.0 | 5 votes |
@Test public void testGetMetadataAfterBlobstoreUpload() throws IOException { Date expectedDate = new Date(12345678L); String expectedFilename = "my-file"; long expectedSize = 123456789L; String expectedMd5Hash = "abcdefghijklmnop"; BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService(); GcsFilename expectedGcsFilename = new GcsFilename("my-bucket", expectedFilename); BlobKey expectedKey = blobstoreService.createGsBlobKey( getPathForGcsFilename(expectedGcsFilename)); BlobStorageFactory .getBlobInfoStorage() .saveBlobInfo( new BlobInfo( expectedKey, "text", expectedDate, expectedFilename, expectedSize, expectedMd5Hash)); GcsFileMetadata metadata = rawGcsService.getObjectMetadata(expectedGcsFilename, 0); assertEquals("", metadata.getEtag()); assertEquals(expectedGcsFilename, metadata.getFilename()); assertEquals(expectedDate, metadata.getLastModified()); assertEquals(GcsFileOptions.getDefaultInstance(), metadata.getOptions()); assertEquals(expectedSize, metadata.getLength()); assertEquals(Collections.emptyMap(), metadata.getXGoogHeaders()); }
Example #8
Source File: GcsExampleServlet.java From appengine-gcs-client with Apache License 2.0 | 5 votes |
/** * Retrieves a file from GCS and returns it in the http response. * If the request path is /gcs/Foo/Bar this will be interpreted as * a request to read the GCS file named Bar in the bucket Foo. */ //[START doGet] @Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { GcsFilename fileName = getFileName(req); if (SERVE_USING_BLOBSTORE_API) { BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService(); BlobKey blobKey = blobstoreService.createGsBlobKey( "/gs/" + fileName.getBucketName() + "/" + fileName.getObjectName()); blobstoreService.serve(blobKey, resp); } else { GcsInputChannel readChannel = gcsService.openPrefetchingReadChannel(fileName, 0, BUFFER_SIZE); copy(Channels.newInputStream(readChannel), resp.getOutputStream()); } }
Example #9
Source File: ServeBlobServlet.java From appengine-java-vm-runtime with Apache License 2.0 | 5 votes |
@Override public void service(HttpServletRequest req, HttpServletResponse resp) throws IOException { BlobKey blobKey = new BlobKey(req.getParameter("key")); String contentType = req.getParameter("content-type"); String blobRange = req.getParameter("blob-range"); if (blobRange != null) { resp.setHeader("X-AppEngine-BlobRange", blobRange); } if (req.getParameter("delete") != null) { BlobstoreServiceFactory.getBlobstoreService().delete(blobKey); } if (req.getParameter("commitBefore") != null) { resp.getWriter().println("Committing before."); resp.getWriter().flush(); } BlobstoreServiceFactory.getBlobstoreService().serve(blobKey, resp); if (contentType != null) { resp.setContentType(contentType); } if (req.getParameter("commitAfter") != null) { resp.getWriter().println("Committing after."); resp.getWriter().flush(); } }
Example #10
Source File: LocalRawGcsService.java From appengine-gcs-client with Apache License 2.0 | 4 votes |
private void ensureInitialized() throws IOException { blobStorage = BlobStorageAdapter.getInstance(); blobstoreService = BlobstoreServiceFactory.getBlobstoreService(); datastore = DatastoreServiceFactory.getDatastoreService(); }
Example #11
Source File: UploadUrlServerServlet.java From appengine-tck with Apache License 2.0 | 4 votes |
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { BlobstoreService blobstore = BlobstoreServiceFactory.getBlobstoreService(); response.getWriter().println(blobstore.createUploadUrl(parseSuccessPath(request))); }
Example #12
Source File: UploadUrlServerServlet.java From appengine-tck with Apache License 2.0 | 4 votes |
@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { BlobstoreService blobstore = BlobstoreServiceFactory.getBlobstoreService(); response.getWriter().println(blobstore.createUploadUrl(parseSuccessPath(request), parseOptions(request))); }
Example #13
Source File: TestBlobUploadServlet.java From appengine-java-vm-runtime with Apache License 2.0 | 4 votes |
@Override public void init() { this.blobstoreService = BlobstoreServiceFactory.getBlobstoreService(); }
Example #14
Source File: BlobstoreFetchDataTest.java From appengine-tck with Apache License 2.0 | 4 votes |
@Before public void setUp() throws Exception { blobstore = BlobstoreServiceFactory.getBlobstoreService(); blobKey = writeNewBlobFile("Uploaded text"); }
Example #15
Source File: GoogleCloudStorageHelper.java From teammates with GNU General Public License v2.0 | 4 votes |
private static BlobstoreService service() { return BlobstoreServiceFactory.getBlobstoreService(); }
Example #16
Source File: UploadHandlerServlet.java From solutions-photo-sharing-demo-java with Apache License 2.0 | 4 votes |
@Override public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException { AppContext appContext = AppContext.getAppContext(); DemoUser user = appContext.getCurrentUser(); if (user == null) { res.sendError(401, "You have to login to upload image."); return; } BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService(); Map<String, List<BlobKey>> blobs = blobstoreService.getUploads(req); List<BlobKey> keys = blobs.get("photo"); String id = null; boolean succeeded = false; if (keys != null && keys.size() > 0) { PhotoManager photoManager = appContext.getPhotoManager(); Photo photo = photoManager.newPhoto(user.getUserId()); String title = req.getParameter("title"); if (title != null) { photo.setTitle(title); } String isPrivate = req.getParameter(ServletUtils.REQUEST_PARAM_NAME_PRIVATE); photo.setShared(isPrivate == null); photo.setOwnerNickname(ServletUtils.getProtectedUserNickname(user.getNickname())); BlobKey blobKey = keys.get(0); photo.setBlobKey(blobKey); photo.setUploadTime(System.currentTimeMillis()); photo = photoManager.upsertEntity(photo); id = photo.getId().toString(); succeeded = true; } if (succeeded) { res.sendRedirect(appContext.getPhotoServiceManager().getRedirectUrl( req.getParameter(ServletUtils.REQUEST_PARAM_NAME_TARGET_URL), user.getUserId(), id)); } else { res.sendError(400, "Request cannot be handled."); } }
Example #17
Source File: ImagesServlet.java From java-docs-samples with Apache License 2.0 | 4 votes |
@Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { // [START original_image] // Read the image.jpg resource into a ByteBuffer. FileInputStream fileInputStream = new FileInputStream(new File("WEB-INF/image.jpg")); FileChannel fileChannel = fileInputStream.getChannel(); ByteBuffer byteBuffer = ByteBuffer.allocate((int) fileChannel.size()); fileChannel.read(byteBuffer); byte[] imageBytes = byteBuffer.array(); // Write the original image to Cloud Storage gcsService.createOrReplace( new GcsFilename(bucket, "image.jpeg"), new GcsFileOptions.Builder().mimeType("image/jpeg").build(), ByteBuffer.wrap(imageBytes)); // [END original_image] // [START resize] // Get an instance of the imagesService we can use to transform images. ImagesService imagesService = ImagesServiceFactory.getImagesService(); // Make an image directly from a byte array, and transform it. Image image = ImagesServiceFactory.makeImage(imageBytes); Transform resize = ImagesServiceFactory.makeResize(100, 50); Image resizedImage = imagesService.applyTransform(resize, image); // Write the transformed image back to a Cloud Storage object. gcsService.createOrReplace( new GcsFilename(bucket, "resizedImage.jpeg"), new GcsFileOptions.Builder().mimeType("image/jpeg").build(), ByteBuffer.wrap(resizedImage.getImageData())); // [END resize] // [START rotate] // Make an image from a Cloud Storage object, and transform it. BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService(); BlobKey blobKey = blobstoreService.createGsBlobKey("/gs/" + bucket + "/image.jpeg"); Image blobImage = ImagesServiceFactory.makeImageFromBlob(blobKey); Transform rotate = ImagesServiceFactory.makeRotate(90); Image rotatedImage = imagesService.applyTransform(rotate, blobImage); // Write the transformed image back to a Cloud Storage object. gcsService.createOrReplace( new GcsFilename(bucket, "rotatedImage.jpeg"), new GcsFileOptions.Builder().mimeType("image/jpeg").build(), ByteBuffer.wrap(rotatedImage.getImageData())); // [END rotate] // [START servingUrl] // Create a fixed dedicated URL that points to the GCS hosted file ServingUrlOptions options = ServingUrlOptions.Builder.withGoogleStorageFileName("/gs/" + bucket + "/image.jpeg") .imageSize(150) .crop(true) .secureUrl(true); String url = imagesService.getServingUrl(options); // [END servingUrl] // Output some simple HTML to display the images we wrote to Cloud Storage // in the browser. PrintWriter out = resp.getWriter(); out.println("<html><body>\n"); out.println( "<img src='//storage.cloud.google.com/" + bucket + "/image.jpeg' alt='AppEngine logo' />"); out.println( "<img src='//storage.cloud.google.com/" + bucket + "/resizedImage.jpeg' alt='AppEngine logo resized' />"); out.println( "<img src='//storage.cloud.google.com/" + bucket + "/rotatedImage.jpeg' alt='AppEngine logo rotated' />"); out.println("<img src='" + url + "' alt='Hosted logo' />"); out.println("</body></html>\n"); }
Example #18
Source File: BlobEndpoint.java From io2014-codelabs with Apache License 2.0 | 3 votes |
/** * Retrieves the Image from Google Cloud Storage. * * @param bucketName the bucket name of the Google Cloud Storage. * @param objectName the object path to the image in the Google Cloud Storage. * @return */ private Image getImageFromGcs(String bucketName, String objectName) { BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService(); BlobKey blobKey = blobstoreService.createGsBlobKey( "/gs/" + bucketName + "/" + objectName); return ImagesServiceFactory.makeImageFromBlob(blobKey); }