Skip to content
This repository was archived by the owner on Sep 25, 2026. It is now read-only.
This repository was archived by the owner on Sep 25, 2026. It is now read-only.

Blob download to provide InputStream #928

Closed

Description

@nanda-kumar-office1

com.google.cloud.storage.Blob has download method, but it takes OutputSteam, but instead it should provide access to InputStream, which can open more options on the data.

Activity

  1. added
    type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.
    on Jul 27, 2021
  2. self-assigned this
    on Jul 27, 2021
  3. BenWhitehead commented on Jul 27, 2021

    @BenWhitehead
    Collaborator

    Hi @nanda-kumar-office1, Can you expand on what types of things you are hoping to accomplish via using an InputStream directly?

    Currently downloadTo transparently takes care of retrying a request that may have been interrupted, decompressing if there was any transport compression as well as moving the bytes to the provided OutputStream with minimal copying.

  4. nanda-kumar-office1 commented on Jul 28, 2021

    @nanda-kumar-office1
    Author

    Thank you Ben for the response.
    downloadTo is perfect for Desktop kind of app, but on server side programming, we may have to set few headers before streaming the file content to user. Also, it may not be a good practice to pass servlet response handler all the way to the backend layer.
    Feel free if you have more question, I can get specific examples based on api that I had been working across other vendor apis too.

  5. BenWhitehead commented on Oct 5, 2021

    @BenWhitehead
    Collaborator

    Unfortunately we haven't been able to get around to implementing this yet as we've ran into some potential issues needing to manage the streams that are passed out from the underlying rpc systems.

    For the interim, your best bet is to do something like the following where you leverage our resumable download reader:

        OutputStream outputStream = request.getOutputStream();
        Blob b = storage.get(BlobId.of("bucket", "blob"));
        try (InputStream inputStream = java.nio.channels.Channels.newInputStream(b.reader())) {
          com.google.common.io.ByteStreams.copy(inputStream, outputStream);
        }
  6. nanda-kumar-office1 commented on Oct 6, 2021

    @nanda-kumar-office1
    Author

    Thank you Ben,
    During the course, I used the below workaround code that worked for us.

    BlobInfo blobInfo = BlobInfo.newBuilder(BlobId.of(bucketName, filePath)).build();
    URL url = storage.signUrl(blobInfo,5,TimeUnit.MINUTES,SignUrlOption.withV4Signature());

    InputSteam in = url.openStream()

  7. BenWhitehead commented on Oct 6, 2021

    @BenWhitehead
    Collaborator

    Glad to hear you were able to figure out something that worked for you. One thing of note however, if you use the reader adapter I mentioned above you will receive our automatic retry for downloading of individual chunks. So if you have medium~ish size blobs (more than say 5mb), this might help in the reliability perception for your app.

  8. nanda-kumar-office1 commented on Oct 6, 2021

    @nanda-kumar-office1
    Author

    Sure Ben, let me try it and update you.
    Thanks,

  9. BenWhitehead commented on Feb 1, 2023

    @BenWhitehead
    Collaborator

    Apologies on the very late reply to this issue. Recently we've been able to clean up a large portion of the internal middleware of the client for the read path of objects. This allows storage.reader (and blob.reader) to provide bandwidth competitive with downloadTo[1] and providing transparent automatic retries - something downloadTo can't do. As such, the recommended path moving forward is to leverage the code shared in #928 (comment).

    [1] #1799

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api: storageIssues related to the googleapis/java-storage API.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions