Java Code Examples for com.google.appengine.tools.cloudstorage.GcsService#createOrReplace()

The following examples show how to use com.google.appengine.tools.cloudstorage.GcsService#createOrReplace() . 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: 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 2
Source File: GalleryServiceImpl.java    From appinventor-extensions with Apache License 2.0 5 votes vote down vote up
/**
 * store aia file on cloud server
 * @param galleryId gallery id
 * @param projectId project id
 * @param projectName project name
 */
private void storeAIA(long galleryId, long projectId, String projectName) throws IOException {

  final String userId = userInfoProvider.getUserId();
  // build the aia file name using the ai project name and code stolen
  // from DownloadServlet to normalize...
  String aiaName = StringUtils.normalizeForFilename(projectName) + ".aia";
  // grab the data for the aia file using code from DownloadServlet
  RawFile aiaFile = null;
  byte[] aiaBytes= null;
  ProjectSourceZip zipFile = fileExporter.exportProjectSourceZip(userId,
    projectId, true, false, aiaName, false, false, false, true);
  aiaFile = zipFile.getRawFile();
  aiaBytes = aiaFile.getContent();
  LOG.log(Level.INFO, "aiaFile numBytes:"+aiaBytes.length);
  // now stick the aia file into the gcs

  //String galleryKey = GalleryApp.getSourceKey(galleryId);//String.valueOf(galleryId);
  GallerySettings settings = loadGallerySettings();
  String galleryKey = settings.getSourceKey(galleryId);
  // setup cloud
  GcsService gcsService = GcsServiceFactory.createGcsService();

  //GcsFilename filename = new GcsFilename(GalleryApp.GALLERYBUCKET, galleryKey);
  GcsFilename filename = new GcsFilename(settings.getBucket(), galleryKey);

  GcsFileOptions options = new GcsFileOptions.Builder().mimeType("application/zip")
    .acl("public-read").cacheControl("no-cache").addUserMetadata("title", aiaName).build();
  GcsOutputChannel writeChannel = gcsService.createOrReplace(filename, options);
  writeChannel.write(ByteBuffer.wrap(aiaBytes));

  // Now finalize
  writeChannel.close();
}
 
Example 3
Source File: GalleryServiceImpl.java    From appinventor-extensions with Apache License 2.0 4 votes vote down vote up
/**
 * 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();
  }
}
 
Example 4
Source File: GalleryServlet.java    From appinventor-extensions with Apache License 2.0 4 votes vote down vote up
@Override
  public void doPost(HttpServletRequest req, HttpServletResponse resp) {
    setDefaultHeader(resp);
    UploadResponse uploadResponse;

    String uri = req.getRequestURI();
    // First, call split with no limit parameter.
    String[] uriComponents = uri.split("/");

    if (true) {
      String requestType = uriComponents[REQUEST_TYPE_INDEX];
      if (DEBUG) {
        LOG.info("######### GOT IN URI");
        LOG.info(requestType);
      }

      long project_Id = -1;
      String user_Id = "-1";
      if (requestType.equalsIgnoreCase("apps")) {
        project_Id = Long.parseLong(uriComponents[GALLERY_OR_USER_ID_INDEX]);
      } else if (requestType.equalsIgnoreCase("user")) {
        //the code below doesn't check if user_Id is the id of current user
        //user_Id = uriComponents[GALLERY_OR_USER_ID_INDEX];
        user_Id = userInfoProvider.getUserId();
      }
      InputStream uploadedStream;
      try {
        if(req.getContentLength() < MAX_IMAGE_FILE_SIZE){
          uploadedStream = getRequestStream(req, ServerLayout.UPLOAD_FILE_FORM_ELEMENT);

          // Converts the input stream to byte array
          byte[] buffer = new byte[8000];
          int bytesRead = 0;
          ByteArrayOutputStream bao = new ByteArrayOutputStream();
          while ((bytesRead = uploadedStream.read(buffer)) != -1) {
            bao.write(buffer, 0, bytesRead);
          }
          // Set up the cloud file (options)
          String key = "";
          GallerySettings settings = galleryService.loadGallerySettings();
          if (requestType.equalsIgnoreCase("apps")) {
            key = settings.getProjectImageKey(project_Id);
          } else if (requestType.equalsIgnoreCase("user")) {
            key =  settings.getUserImageKey(user_Id);
          }

          // setup cloud
          GcsService gcsService = GcsServiceFactory.createGcsService();
          GcsFilename filename = new GcsFilename(settings.getBucket(), key);
          GcsFileOptions options = new GcsFileOptions.Builder().mimeType("image/jpeg")
                  .acl("public-read").cacheControl("no-cache").build();
          GcsOutputChannel writeChannel = gcsService.createOrReplace(filename, options);
          writeChannel.write(ByteBuffer.wrap(bao.toByteArray()));

          // Now finalize
          writeChannel.close();

          uploadResponse = new UploadResponse(UploadResponse.Status.SUCCESS);
        }else{
          /*file exceeds size of MAX_IMAGE_FILE_SIZE*/
          uploadResponse = new UploadResponse(UploadResponse.Status.FILE_TOO_LARGE);
        }

        // Now, get the PrintWriter for the servlet response and print the UploadResponse.
        // On the client side, in the onSubmitComplete method in ode/client/utils/Uploader.java, the
        // UploadResponse value will be retrieved as a String via the
        // FormSubmitCompleteEvent.getResults() method.
        PrintWriter out = resp.getWriter();
        out.print(uploadResponse.formatAsHtml());

      } catch (Exception e) {
        throw CrashReport.createAndLogError(LOG, req, null, e);
      }
      // Set http response information
      resp.setStatus(HttpServletResponse.SC_OK);
    }

    // Now, get the PrintWriter for the servlet response and print the UploadResponse.
    // On the client side, in the onSubmitComplete method in ode/client/utils/Uploader.java, the
    // UploadResponse value will be retrieved as a String via the
    // FormSubmitCompleteEvent.getResults() method.
//    PrintWriter out = resp.getWriter();
//    out.print(uploadResponse.formatAsHtml());

    // Set http response information
    resp.setStatus(HttpServletResponse.SC_OK);
  }
 
Example 5
Source File: GcsTestingUtils.java    From nomulus with Apache License 2.0 4 votes vote down vote up
/** Writes a Cloud Storage file. */
public static void writeGcsFile(GcsService gcsService, GcsFilename file, byte[] data)
    throws IOException {
  gcsService.createOrReplace(file, GcsFileOptions.getDefaultInstance(), ByteBuffer.wrap(data));
}