Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Blob.uploadFrom(InputStream) #62

Closed
broady opened this issue Jan 14, 2020 · 5 comments · Fixed by #162
Closed

Add Blob.uploadFrom(InputStream) #62

broady opened this issue Jan 14, 2020 · 5 comments · Fixed by #162
Assignees
Labels
api: storage Issues related to the googleapis/java-storage API. semver: minor A new feature was added. No breaking changes. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.

Comments

@broady
Copy link

broady commented Jan 14, 2020

We have Blob.downloadTo(OutputStream) (and other downloadFrom variants)

Should probably have uploadFrom with a stream. People currently have to buffer/chunk their writes manually.

/cc @crwilcox

@yoshi-automation yoshi-automation added the triage me I really want to be triaged. label Jan 15, 2020
@elharo elharo added semver: minor A new feature was added. No breaking changes. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. labels Jan 15, 2020
@yoshi-automation yoshi-automation removed the triage me I really want to be triaged. label Jan 15, 2020
@dmitry-fa dmitry-fa self-assigned this Jan 17, 2020
@google-cloud-label-sync google-cloud-label-sync bot added the api: storage Issues related to the googleapis/java-storage API. label Jan 29, 2020
@dmitry-fa
Copy link
Contributor

New methods of Blob are suggested:

public void uploadFrom(Path path, BlobWriteOption... options) {...}
public void uploadFrom(Path path, int chunkSize, BlobWriteOption... options) {...}
public void uploadFrom(InputStream input, BlobWriteOption... options) {...}

  /**
   * Uploads the given content to this blob using specified blob write options and the provided
   * chunk size. The default chunk size is 2 MB. Larger chunk sizes may improve the upload
   * performance but require more memory. It could cause OutOfMemoryError or add significant garbage
   * collection overhead. Chunk sizes which are less than 256 KB are not allowed, they will be
   * treated as 256 KB.
   *
   * @param input content to be uploaded
   * @param chunkSize the minimum size that will be written by a single RPC.
   * @param options blob write options
   * @throws StorageException upon failure
   */
  public void uploadFrom(InputStream input, int chunkSize, BlobWriteOption... options) {
    chunkSize = Math.max(chunkSize, 262144); // adjust with MinChunkSize of BaseWriteChannel
    try (WriteChannel writer = storage.writer(this, options)) {
      writer.setChunkSize(chunkSize);
      byte[] buffer = new byte[chunkSize];
      int length;
      while ((length = input.read(buffer)) >= 0) {
        writer.write(ByteBuffer.wrap(buffer, 0, length));
      }
    } catch (IOException e) {
      throw new StorageException(e);
    }
  }

@dmitry-fa
Copy link
Contributor

The following extension to the Blob class is proposed:

  public Blob uploadFrom(Path path, BlobWriteOption... options)
  public Blob uploadFrom(InputStream input, BlobWriteOption... options)

  /**
   * Uploads the given content to the storage using specified write channel and the given
   * buffer size. The default buffer size is 15 MB. Larger buffer sizes may improve the upload
   * performance but require more memory. It could cause OutOfMemoryError or add significant garbage
   * collection overhead. Buffer sizes which are less than 256 KB are not allowed, they will be
   * treated as 256 KB.
   *
   * <p>Note that this method does not close neither InputStream nor WriterChannel</p>
   *
   * <p>Example of uploading:
   *
   * <pre>{@code
   * Path file = Paths.get("humongous.file");
   * try (InputStream input = Files.newInputStream(file); WriteChannel writer = storage.writer(blobInfo)) {
   *     Blob.upload(input, writer, 150 * 1024 * 1024);
   * }
   *
   * @param input content to be uploaded
   * @param writer channel
   * @param bufferSize size of the buffer to read from input and send over writer
   */
  public static void upload(InputStream input, WriteChannel writer, int bufferSize)

@dmitry-fa
Copy link
Contributor

Similar extension to the Storage interface is planned to be proposed with a separate PR

@dmitry-fa
Copy link
Contributor

fix was reverted by #190

@dmitry-fa
Copy link
Contributor

#214 fixes this issue as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: storage Issues related to the googleapis/java-storage API. semver: minor A new feature was added. No breaking changes. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.
Projects
None yet
4 participants