com.google.appengine.tools.cloudstorage.GcsInputChannel Java Examples
The following examples show how to use
com.google.appengine.tools.cloudstorage.GcsInputChannel.
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: ObjectifyStorageIo.java From appinventor-extensions with Apache License 2.0 | 6 votes |
@Override public InputStream openTempFile(String fileName) throws IOException { if (!fileName.startsWith("__TEMP__")) { throw new RuntimeException("deleteTempFile (" + fileName + ") Invalid File Name"); } GcsFilename gcsFileName = new GcsFilename(GCS_BUCKET_NAME, fileName); int fileSize = (int) gcsService.getMetadata(gcsFileName).getLength(); ByteBuffer resultBuffer = ByteBuffer.allocate(fileSize); GcsInputChannel readChannel = gcsService.openReadChannel(gcsFileName, 0); int bytesRead = 0; try { while (bytesRead < fileSize) { bytesRead += readChannel.read(resultBuffer); } } finally { readChannel.close(); } return new ByteArrayInputStream(resultBuffer.array()); }
Example #2
Source File: GCSClientTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@Test @InSequence(2) public void testReadGsObj() throws IOException { GcsFilename filename = new GcsFilename(bucket, OBJECT_NAME); String objContent; try (GcsInputChannel readChannel = gcsService.openReadChannel(filename, 0)) { BufferedReader reader = new BufferedReader(Channels.newReader(readChannel, "UTF8")); String line; objContent = ""; while ((line = reader.readLine()) != null) { objContent += line; } } assertTrue(objContent.indexOf(CONTENT) == 0); assertTrue(objContent.indexOf(MORE_WORDS) > 0); }
Example #3
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 #4
Source File: GalleryServiceImpl.java From appinventor-extensions with Apache License 2.0 | 4 votes |
/** * when an app is published/updated, we need to move the image * that was temporarily uploaded into projects/projectid/image * into the gallery image * @param app gallery app */ private void setGalleryAppImage(GalleryApp app) { // best thing would be if GCS has a mv op, we can just do that. // don't think that is there, though, so for now read one and write to other // First, read the file from projects name boolean lockForRead = false; //String projectImageKey = app.getProjectImageKey(); GallerySettings settings = loadGallerySettings(); String projectImageKey = settings.getProjectImageKey(app.getProjectId()); try { GcsService gcsService = GcsServiceFactory.createGcsService(); //GcsFilename filename = new GcsFilename(GalleryApp.GALLERYBUCKET, projectImageKey); GcsFilename filename = new GcsFilename(settings.getBucket(), projectImageKey); GcsInputChannel readChannel = gcsService.openReadChannel(filename, 0); InputStream gcsis = Channels.newInputStream(readChannel); byte[] buffer = new byte[8000]; int bytesRead = 0; ByteArrayOutputStream bao = new ByteArrayOutputStream(); while ((bytesRead = gcsis.read(buffer)) != -1) { bao.write(buffer, 0, bytesRead); } // close the project image file readChannel.close(); // if image is greater than 200 X 200, it will be scaled (200 X 200). // otherwise, it will be stored as origin. byte[] oldImageData = bao.toByteArray(); byte[] newImageData; ImagesService imagesService = ImagesServiceFactory.getImagesService(); Image oldImage = ImagesServiceFactory.makeImage(oldImageData); //if image size is too big, scale it to a smaller size. if(oldImage.getWidth() > 200 && oldImage.getHeight() > 200){ Transform resize = ImagesServiceFactory.makeResize(200, 200); Image newImage = imagesService.applyTransform(resize, oldImage); newImageData = newImage.getImageData(); }else{ newImageData = oldImageData; } // set up the cloud file (options) // After publish, copy the /projects/projectId image into /apps/appId //String galleryKey = app.getImageKey(); String galleryKey = settings.getImageKey(app.getGalleryAppId()); //GcsFilename outfilename = new GcsFilename(GalleryApp.GALLERYBUCKET, galleryKey); GcsFilename outfilename = new GcsFilename(settings.getBucket(), galleryKey); GcsFileOptions options = new GcsFileOptions.Builder().mimeType("image/jpeg") .acl("public-read").cacheControl("no-cache").build(); GcsOutputChannel writeChannel = gcsService.createOrReplace(outfilename, options); writeChannel.write(ByteBuffer.wrap(newImageData)); // Now finalize writeChannel.close(); } catch (IOException e) { // TODO Auto-generated catch block LOG.log(Level.INFO, "FAILED WRITING IMAGE TO GCS"); e.printStackTrace(); } }