来自Google云端存储的流文件

xyb*_*rek 3 java servlets google-cloud-storage

以下是从Google云端存储下载文件的代码:

@Override
public void write(OutputStream outputStream) throws IOException {
    try {
        LOG.info(path);
        InputStream stream = new ByteArrayInputStream(GoogleJsonKey.JSON_KEY.getBytes(StandardCharsets.UTF_8));
        StorageOptions options = StorageOptions.newBuilder()
                .setProjectId(PROJECT_ID)
                .setCredentials(GoogleCredentials.fromStream(stream)).build();
        Storage storage = options.getService();
        final CountingOutputStream countingOutputStream = new CountingOutputStream(outputStream);
        byte[] read = storage.readAllBytes(BlobId.of(BUCKET, path));
        countingOutputStream.write(read);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        outputStream.close();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是有效的,但这里的问题是它必须首先缓冲所有字节,然后再流回到此方法的客户端.这导致了很多延迟,尤其是当存储在GCS中的文件很大时.

有没有办法从GCS获取文件并将其直接流式传输到OutputStream,这个OutputStream这里btw是一个Servlet.

Tux*_*ude 11

只是为了澄清,你需要一个OutputStream还是一个InputStream?一种看待这种情况的方法是将存储在Google云端存储对象中的数据作为文件存储,并且您有一个InputStream来读取该文件.如果可行,请继续阅读.

Storage API中没有现有的方法提供InputStream或者OutputStream.但是,云存储客户端库中2个API,它们公开了一个ReadChannelReadableByteChannel(来自Java NIO API)扩展的对象.

ReadChannel reader(String bucket, String blob, BlobSourceOption... options);
ReadChannel reader(BlobId blob, BlobSourceOption... options);
Run Code Online (Sandbox Code Playgroud)

使用它的一个简单示例(取自StorageSnippets.java):

/**
   * Example of reading a blob's content through a reader.
   */
  // [TARGET reader(String, String, BlobSourceOption...)]
  // [VARIABLE "my_unique_bucket"]
  // [VARIABLE "my_blob_name"]
  public void readerFromStrings(String bucketName, String blobName) throws IOException {
    // [START readerFromStrings]
    try (ReadChannel reader = storage.reader(bucketName, blobName)) {
      ByteBuffer bytes = ByteBuffer.allocate(64 * 1024);
      while (reader.read(bytes) > 0) {
        bytes.flip();
        // do something with bytes
        bytes.clear();
      }
    }
    // [END readerFromStrings]
  }
Run Code Online (Sandbox Code Playgroud)

您还可以使用该newInputStream()方法来包装一个InputStreamReadableByteChannel.

public static InputStream newInputStream(ReadableByteChannel ch)

即使你需要一个OutputStream,你也应该能够将数据从对象中复制InputStream或更好地复制到ReadChannel对象中OutputStream.

完整的例子

运行此示例为: PROGRAM_NAME <BUCKET_NAME> <BLOB_PATH>

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.WritableByteChannel;

import com.google.cloud.ReadChannel;
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.BucketInfo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;

/**
 * An example which reads the contents of the specified object/blob from GCS
 * and prints the contents to STDOUT.
 *
 * Run it as PROGRAM_NAME <BUCKET_NAME> <BLOB_PATH>
 */
public class ReadObjectSample {
  private static final int BUFFER_SIZE = 64 * 1024;

  public static void main(String[] args) throws IOException {
    // Instantiates a Storage client
    Storage storage = StorageOptions.getDefaultInstance().getService();

    // The name for the GCS bucket
    String bucketName = args[0];
    // The path of the blob (i.e. GCS object) within the GCS bucket.
    String blobPath = args[1];

    printBlob(storage, bucketName, blobPath);
  }

  // Reads from the specified blob present in the GCS bucket and prints the contents to STDOUT.
  private static void printBlob(Storage storage, String bucketName, String blobPath) throws IOException {
    try (ReadChannel reader = storage.reader(bucketName, blobPath)) {
      WritableByteChannel outChannel = Channels.newChannel(System.out);
      ByteBuffer bytes = ByteBuffer.allocate(BUFFER_SIZE);
      while (reader.read(bytes) > 0) {
        bytes.flip();
        outChannel.write(bytes);
        bytes.clear();
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)


Tob*_*ias 9

目前我能找到的最干净的选项是这样的:

Blob blob = bucket.get("some-file");
ReadChannel reader = blob.reader();
InputStream inputStream = Channels.newInputStream(reader);
Run Code Online (Sandbox Code Playgroud)

频道来自 java.nio。此外,您可以使用 commons io 轻松地将 InputStream 读取到 OutputStream 中:

IOUtils.copy(inputStream, outputStream);
Run Code Online (Sandbox Code Playgroud)