public static boolean deleteImage(String objectName) {
    try {
      GcsService gcsService = GcsServiceFactory.createGcsService(RetryParams.getDefaultInstance());
      GcsFilename fileName = new GcsFilename(getBucket(), objectName);

      return gcsService.delete(fileName);
    } catch (IOException ex) {
      log.log(Level.WARNING, "Exception while deleting file", ex);
      return false;
    }
  }
 @Override
 public List<? extends InputReader<byte[]>> createReaders() {
   GcsService gcsService =
       GcsServiceFactory.createGcsService(MapReduceConstants.GCS_RETRY_PARAMETERS);
   GcsFileMetadata metadata;
   try {
     metadata = gcsService.getMetadata(file);
     if (metadata == null) {
       throw new RuntimeException("File does not exist: " + file.toString());
     }
   } catch (IOException e) {
     throw new RuntimeException("Unable to read file metadata: " + file.toString(), e);
   }
   long blobSize = metadata.getLength();
   return split(file, blobSize, shardCount);
 }
public class BlobAccessorGcsImpl implements BlobAccessor {

  private static final Logger logger = Logger.getLogger(BlobAccessorGcsImpl.class.getName());

  private static final GcsService gcsService =
      GcsServiceFactory.createGcsService(RetryParams.getDefaultInstance());

  private final String bucketName;

  public BlobAccessorGcsImpl(String bucketName) {
    this.bucketName = bucketName;
  }

  @Override
  public BlobEntry newBlob(String fileName) {
    return new BlobEntryGcsImpl(fileName);
  }

  @Override
  public BlobEntry newBlob(String fileName, byte[] data, String mimeType) {
    return new BlobEntryGcsImpl(fileName, data, mimeType);
  }

  @Override
  public List<String> getNameList(String prefix) throws IOException {
    ListOptions.Builder options = new ListOptions.Builder();
    options.setPrefix(prefix);

    ListResult result = gcsService.list(bucketName, options.build());

    List<String> fileNameList = new LinkedList<>();
    while (result.hasNext()) {
      String fileName = result.next().getName();
      if (!fileName.equals(prefix)) fileNameList.add(fileName.substring(prefix.length()));
    }
    return fileNameList;
  }

  @Override
  public BlobEntry getBlob(String fileName) throws UnexpectedServerException {
    GcsFilename gcsFileName = new GcsFilename(bucketName, fileName);

    try {

      GcsFileMetadata gcsFileMetadata = gcsService.getMetadata(gcsFileName);

      if (gcsFileMetadata == null) return null;

      if (gcsFileMetadata.getLength() == 0) return null;

      GcsInputChannel gcsInputChannel = gcsService.openReadChannel(gcsFileName, 0);

      ByteBuffer byteBuffer = ByteBuffer.allocate((int) gcsFileMetadata.getLength());
      gcsInputChannel.read(byteBuffer);

      return new BlobEntryGcsImpl(byteBuffer, gcsFileMetadata);

    } catch (IOException ex) {
      logger.log(Level.INFO, "Failed to fetch blob with name '" + fileName + "'", ex);
      throw new UnexpectedServerException();
    }
  }

  @Override
  public void createOrUpdateBlob(BlobEntry blobEntry) throws UnexpectedServerException {
    GcsFilename gcsFileName = new GcsFilename(bucketName, blobEntry.getName());

    Builder builder = new GcsFileOptions.Builder();
    if (blobEntry.getMimeType() != null) builder.mimeType(blobEntry.getMimeType());
    if (blobEntry.getCacheControl() != null) builder.cacheControl(blobEntry.getCacheControl());
    GcsFileOptions gcsFileOptions = builder.build();

    try {
      GcsOutputChannel gcsOutputChannel = gcsService.createOrReplace(gcsFileName, gcsFileOptions);
      gcsOutputChannel.write(ByteBuffer.wrap(blobEntry.getData()));
      gcsOutputChannel.close();
    } catch (IOException ex) {
      logger.log(
          Level.INFO, "Failed to create/update blob with name '" + blobEntry.getName() + "'", ex);
      throw new UnexpectedServerException();
    }
  }

  @Override
  public void deleteBlob(BlobEntry blobEntry) throws UnexpectedServerException {
    try {
      gcsService.delete(new GcsFilename(bucketName, blobEntry.getName()));
    } catch (IOException ex) {
      logger.log(Level.INFO, "Failed to delete blob with name '" + blobEntry.getName() + "'", ex);
      throw new UnexpectedServerException();
    }
  }
}
/** @author KaranJeet */
@SuppressWarnings("serial")
public class UploadController extends HttpServlet {

  private static final Logger log = Logger.getLogger(FileUpload.class.getName());

  private final GcsService gcsService =
      GcsServiceFactory.createGcsService(
          new RetryParams.Builder()
              .initialRetryDelayMillis(10)
              .retryMaxAttempts(10)
              .totalRetryPeriodMillis(15000)
              .build());

  @Override
  public void doPost(HttpServletRequest req, HttpServletResponse res)
      throws ServletException, IOException {
    try {
      ServletFileUpload upload = new ServletFileUpload();
      res.setContentType("text/plain");
      log.warning("reached upload controller doPost");
      FileItemIterator iterator = upload.getItemIterator(req);
      // log.warning("req.tostring = "+req.toString());
      final List<FileItemStream> list = new ArrayList<FileItemStream>();
      log.warning("List population started");
      while (iterator.hasNext()) {
        list.add(iterator.next());
      }
      log.warning("List completed started");

      for (int i = 0; i < 1; i++) {
        Thread thread =
            ThreadManager.createThreadForCurrentRequest(
                new Runnable() {
                  public void run() {
                    try {
                      log.warning("Thread started");
                      log.warning("list size = " + list.size());

                      for (int i = 0; i < list.size(); i++) {
                        log.warning("starting upload");
                        FileItemStream item = list.get(i);
                        InputStream stream = item.openStream();
                        log.warning("reached iterator");
                        if (item.isFormField()) {
                          log.warning("Got a form field: " + item.getFieldName());
                        } else {
                          log.warning(
                              "Got an uploaded file: "
                                  + item.getFieldName()
                                  + ", name = "
                                  + item.getName());

                          GcsOutputChannel outputChannel =
                              gcsService.createOrReplace(
                                  getFileName(item.getName()), GcsFileOptions.getDefaultInstance());
                          copy(stream, Channels.newOutputStream(outputChannel));
                        }
                      }
                    } catch (Exception ex) {
                      log.warning(ex.getMessage());
                      throw new RuntimeException("Interrupted in loop:", ex);
                    }
                  }
                });
        thread.start();
      }
      TimeUnit.SECONDS.sleep(5);

      /* while (iterator.hasNext()) {
        FileItemStream item = iterator.next();
        InputStream stream = item.openStream();
        log.warning("reached iterator");
        if (item.isFormField()) {
          log.warning("Got a form field: " + item.getFieldName());
        } else {
          log.warning("Got an uploaded file: " + item.getFieldName() +
                      ", name = " + item.getName());

          GcsOutputChannel outputChannel =
        	        gcsService.createOrReplace(getFileName(item.getName()), GcsFileOptions.getDefaultInstance());
        	copy(stream, Channels.newOutputStream(outputChannel));

        }
      }*/
      log.warning("exited upload controller doPost");
    } catch (Exception ex) {
      log.warning("exception - " + ex.getMessage());
      throw new ServletException(ex);
    }
  }

  private GcsFilename getFileName(String filename) {
    return new GcsFilename("ass3gcs", "test/" + filename);
  }

  /** Transfer the data from the inputStream to the outputStream. Then close both streams. */
  private void copy(InputStream input, OutputStream output) throws IOException {
    int size = 1024 * 1024;
    try {
      byte[] buffer = new byte[size];
      int bytesRead = input.read(buffer);
      while (bytesRead != -1) {
        output.write(buffer, 0, bytesRead);
        bytesRead = input.read(buffer);
      }
    } finally {
      input.close();
      output.close();
    }
  }
}