com.google.appengine.api.blobstore.BlobKey Java Examples

The following examples show how to use com.google.appengine.api.blobstore.BlobKey. 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: TestServingViaBlobstoreApi.java    From appengine-gcs-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testFoundUrl() throws IOException {
  GcsFilename gcsFilename =
      new GcsFilename(TestServingViaBlobstoreApi.class.getName(), "testFoundUrl");
  @SuppressWarnings("resource")
  GcsOutputChannel channel = GCS_SERVICE.createOrReplace(
      gcsFilename, new GcsFileOptions.Builder().mimeType("image/png").build());
  byte[] bytes = BaseEncoding.base64().decode(PNG);
  channel.write(ByteBuffer.wrap(bytes));
  channel.close();

  BlobKey blobKey = BLOB_STORE.createGsBlobKey(
      "/gs/" + gcsFilename.getBucketName() + "/" + gcsFilename.getObjectName());

  byte[] imageData = BLOB_STORE.fetchData(blobKey, 0, bytes.length);
  assertArrayEquals(bytes, imageData);

  ServingUrlOptions opts = ServingUrlOptions.Builder.withBlobKey(blobKey);
  opts.imageSize(bytes.length);
  String url = IMAGES_SERVICE.getServingUrl(opts);
  assertTrue(url.length() > 0);
}
 
Example #2
Source File: PhotoManagerSql.java    From solutions-photo-sharing-demo-java with Apache License 2.0 6 votes vote down vote up
@Override
public Photo fromResultSet(ResultSet rs) throws SQLException {
  int count = 1;
  Long photoId = rs.getLong(count++);
  String title = rs.getString(count++);
  String blobKey = rs.getString(count++);
  Timestamp ts = rs.getTimestamp(count++);
  String userId = rs.getString(count++);
  String nickname = rs.getString(count++);
  Boolean isShared = rs.getBoolean(count++);
  Boolean isActive = rs.getBoolean(count++);
  Photo photo = new PhotoSql(photoId, userId);
  photo.setTitle(title);
  photo.setBlobKey(new BlobKey(blobKey));
  photo.setUploadTime(ts.getTime());
  photo.setOwnerNickname(nickname);
  photo.setShared(isShared != null && isShared);
  photo.setActive(isActive == null || isActive);
  return photo;
}
 
Example #3
Source File: BlobserviceServeServlet.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@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 #4
Source File: ServeBlobServlet.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String mimeType = request.getParameter("mimeType");
    String contents = request.getParameter("contents");
    String blobRange = request.getParameter("blobRange");
    String name = request.getParameter("name");

    AppEngineFile file = service.createNewBlobFile(mimeType, name);
    writeToFile(file, contents);
    BlobKey blobKey = service.getBlobKey(file);

    response.addHeader("X-AppEngine-BlobKey", blobKey.getKeyString());
    if (blobRange != null) {
        response.addHeader("X-AppEngine-BlobRange", blobRange);
    }
}
 
Example #5
Source File: BlobstoreUploadTestBase.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
@InSequence(20)
public void testUploadedFileHasCorrectContent_assert() throws Exception {
    BlobKey blobKey = UploadHandlerServlet.getLastUploadedBlobKey();
    assertNotNull("blobKey should not be null", blobKey);

    String contents = getFileContents(blobKey);
    assertEquals(new String(UPLOADED_CONTENT), contents);

    BlobInfo blobInfo = UploadHandlerServlet.getLastUploadedBlobInfo();
    assertNotNull("blobInfo should not be null", blobInfo);
    assertEquals(blobKey, blobInfo.getBlobKey());
    assertEquals(FILENAME, blobInfo.getFilename());
    assertEquals(CONTENT_TYPE, blobInfo.getContentType());
    assertEquals(UPLOADED_CONTENT.length, blobInfo.getSize());
    assertEquals(MD5_HASH, blobInfo.getMd5Hash());

    FileInfo fileInfo = UploadHandlerServlet.getLastUploadedFileInfo();
    assertNotNull("fileInfo should not be null", fileInfo);
    assertEquals(FILENAME, fileInfo.getFilename());
    assertEquals(CONTENT_TYPE, fileInfo.getContentType());
    assertEquals(UPLOADED_CONTENT.length, fileInfo.getSize());
    assertEquals(MD5_HASH, fileInfo.getMd5Hash());
}
 
Example #6
Source File: GcsBlobstoreUploadTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@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 #7
Source File: BlobInputStreamTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testBlobInputStreamWithOffset() throws Exception {
    BlobKey blobKey = writeNewBlobFile("BlobInputStreamTest");

    int OFFSET = 4;
    BlobstoreInputStream stream = new BlobstoreInputStream(blobKey, OFFSET);
    assertEquals("InputStreamTest", toString(stream));
}
 
Example #8
Source File: LocalRawGcsServiceTest.java    From appengine-gcs-client with Apache License 2.0 5 votes vote down vote up
@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 #9
Source File: ImageRequestTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void blobKeyAndImageSize() throws Exception {
    ImageRequest request = new ImageRequest("/blobkey123/=s32");
    assertEquals(new BlobKey("blobkey123"), request.getBlobKey());
    assertTrue(request.isTransformationRequested());
    assertEquals(32, request.getImageSize());
    assertFalse(request.isCrop());
}
 
Example #10
Source File: ImageRequestTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void blobKeyAndImageSizeAndCrop() throws Exception {
    ImageRequest request = new ImageRequest("/blobkey123/=s32-c");
    assertEquals(new BlobKey("blobkey123"), request.getBlobKey());
    assertTrue(request.isTransformationRequested());
    assertEquals(32, request.getImageSize());
    assertTrue(request.isCrop());
}
 
Example #11
Source File: ImageRequest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
public ImageRequest(String pathInfo) {
    StringTokenizer tokenizer = new StringTokenizer(pathInfo, "/");
    blobKey = new BlobKey(tokenizer.nextToken());
    if (tokenizer.hasMoreTokens()) {
        parseSizeAndCrop(tokenizer.nextToken());
    }
}
 
Example #12
Source File: BlobInputStreamTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testBlobInputStream() throws Exception {
    String CONTENT = "BlobInputStreamTest";
    BlobKey blobKey = writeNewBlobFile(CONTENT);

    BlobstoreInputStream stream = new BlobstoreInputStream(blobKey);
    assertEquals(CONTENT, toString(stream));
}
 
Example #13
Source File: ObjectifyStorageIo.java    From appinventor-extensions with Apache License 2.0 5 votes vote down vote up
private byte[] getBlobstoreBytes(String blobKeyString) throws BlobReadException {
  BlobKey blobKey = new BlobKey(blobKeyString);
  if (blobKey == null) {
    throw new BlobReadException("Could not find BlobKey for " + blobKeyString);
  }
  try {
    InputStream blobInputStream = new BlobstoreInputStream(blobKey);
    return ByteStreams.toByteArray(blobInputStream);
  } catch (IOException e) {
    throw new BlobReadException(e, "Error trying to read blob from " + blobKey);
  }
}
 
Example #14
Source File: UploadHandlerServlet.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@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 #15
Source File: BlobstoreFetchDataTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
private void assertFetchDataThrowsIAE(BlobKey blobKey, int startIndex, int endIndex) {
    try {
        blobstore.fetchData(blobKey, startIndex, endIndex);
        fail("Expected IllegalArgumentException when invoking fetchData(blobKey, " + startIndex + ", " + endIndex + ")");
    } catch (IllegalArgumentException ex) {
        // pass
    }
}
 
Example #16
Source File: BlobstoreTestBase.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
protected BlobKey writeNewBlobFile(String text) throws IOException {
    FileService fileService = FileServiceFactory.getFileService();
    AppEngineFile file = fileService.createNewBlobFile("text/plain", "uploadedText.txt");
    FileWriteChannel channel = fileService.openWriteChannel(file, true);
    try {
        channel.write(ByteBuffer.wrap(text.getBytes()));
    } finally {
        channel.closeFinally();
    }
    return fileService.getBlobKey(file);
}
 
Example #17
Source File: PhotoServiceManager.java    From solutions-photo-sharing-demo-java with Apache License 2.0 5 votes vote down vote up
public String getPhotoDisplayUrl(BlobKey blobKey) {
  ServingUrlOptions options = ServingUrlOptions.Builder.withBlobKey(blobKey);
  options.imageSize(configManager.getPhotoThumbnailSizeInPixels());
  options.crop(configManager.isPhotoThumbnailCrop());
  try {
    return ImagesServiceFactory.getImagesService().getServingUrl(options);
  } catch (ImagesServiceFailureException e) {
    logger.severe("Failed to get image serving url: " + e.getMessage());
    return "";
  }
}
 
Example #18
Source File: DownloadServlet.java    From solutions-photo-sharing-demo-java with Apache License 2.0 5 votes vote down vote up
@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 #19
Source File: GalleryServiceImpl.java    From appinventor-extensions with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #20
Source File: GoogleCloudStorageHelper.java    From teammates with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns true if a file with the specified {@code fileKey} exists in the
 *         Google Cloud Storage.
 */
public static boolean doesFileExistInGcs(String fileKey) {
    try {
        service().fetchData(new BlobKey(fileKey), 0, 1);
        return true;
    } catch (IllegalArgumentException e) {
        return false;
    }
}
 
Example #21
Source File: GoogleCloudStorageHelper.java    From teammates with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Deletes the file with the specified {@code fileKey} in the Google Cloud Storage.
 */
public static void deleteFile(String fileKey) {
    try {
        service().delete(new BlobKey(fileKey));
    } catch (Exception e) {
        log.warning("Trying to delete non-existent file with key: " + fileKey);
    }
}
 
Example #22
Source File: ServeBlobServlet.java    From appengine-java-vm-runtime with Apache License 2.0 5 votes vote down vote up
@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 #23
Source File: GcsExampleServlet.java    From appengine-gcs-client with Apache License 2.0 5 votes vote down vote up
/**
   * 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 #24
Source File: LocalRawGcsService.java    From appengine-gcs-client with Apache License 2.0 5 votes vote down vote up
private Object getParam(BlobKey blobKey) throws IOException {
  if (blobKey.getClass() == storeBlobMethod.getParameterTypes()[0]) {
    return blobKey;
  }
  try {
    return blobKeyConstructor.newInstance(blobKey.getKeyString());
  } catch (Exception e) {
    throw new IOException(e);
  }
}
 
Example #25
Source File: PhotoSql.java    From solutions-photo-sharing-demo-java with Apache License 2.0 4 votes vote down vote up
@Override
public void setBlobKey(BlobKey blobKey) {
  this.blobKey = blobKey;
}
 
Example #26
Source File: PhotoSql.java    From solutions-photo-sharing-demo-java with Apache License 2.0 4 votes vote down vote up
@Override
public BlobKey getBlobKey() {
  return blobKey;
}
 
Example #27
Source File: PhotoNoSql.java    From solutions-photo-sharing-demo-java with Apache License 2.0 4 votes vote down vote up
@Override
public void setBlobKey(BlobKey blobKey) {
  entity.setProperty(FIELD_NAME_BLOB_KEY, blobKey);
}
 
Example #28
Source File: PhotoNoSql.java    From solutions-photo-sharing-demo-java with Apache License 2.0 4 votes vote down vote up
@Override
public BlobKey getBlobKey() {
  return (BlobKey) entity.getProperty(FIELD_NAME_BLOB_KEY);
}
 
Example #29
Source File: FileMetaData.java    From sc2gears with Apache License 2.0 4 votes vote down vote up
public BlobKey getBlobKey() {
 return blobKey;
}
 
Example #30
Source File: QueryFilteringByGAEPropertyTypesTest.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
@Test
public void testBlobKeyProperty() {
    testEqualityQueries(new BlobKey("foo"), new BlobKey("bar"));
    testInequalityQueries(new BlobKey("aaa"), new BlobKey("bbb"), new BlobKey("ccc"));
}