Stream file from Google Cloud Storage

后端 未结 4 912
有刺的猬
有刺的猬 2021-01-03 00:54

Here is a code to download File from Google Cloud Storage:

@Override
public void write(OutputStream outputStream) throws IOException {
    try {
        LOG.         


        
4条回答
  •  自闭症患者
    2021-01-03 01:13

    Code, based on @Tuxdude answer

     @Nullable
        public byte[] getFileBytes(String gcsUri) throws IOException {
    
            Blob blob = getBlob(gcsUri);
            ReadChannel reader;
            byte[] result = null;
            if (blob != null) {
                reader = blob.reader();
                InputStream inputStream = Channels.newInputStream(reader);
               result = IOUtils.toByteArray(inputStream);
            }
            return result;
        }
    

    or

    //this will work only with files 64 * 1024 bytes on smaller
     @Nullable
        public byte[] getFileBytes(String gcsUri) throws IOException {
            Blob blob = getBlob(gcsUri);
    
            ReadChannel reader;
            byte[] result = null;
            if (blob != null) {
                reader = blob.reader();
                ByteBuffer bytes = ByteBuffer.allocate(64 * 1024);
    
                while (reader.read(bytes) > 0) {
                    bytes.flip();
                    result = bytes.array();
                    bytes.clear();
                }
            }
            return result; 
        }
    

    helper code:

       @Nullable
        Blob getBlob(String gcsUri) {
            //gcsUri is "gs://" + blob.getBucket() + "/" + blob.getName(),
            //example "gs://myapp.appspot.com/ocr_request_images/000c121b-357d-4ac0-a3f2-24e0f6d5cea185dffb40eee-850fab211438.jpg"
    
            String bucketName = parseGcsUriForBucketName(gcsUri);
            String fileName = parseGcsUriForFilename(gcsUri);
    
            if (bucketName != null && fileName != null) {
                return storage.get(BlobId.of(bucketName, fileName));
            } else {
                return null;
            }
        }
    
        @Nullable
        String parseGcsUriForFilename(String gcsUri) {
            String fileName = null;
            String prefix = "gs://";
            if (gcsUri.startsWith(prefix)) {
                int startIndexForBucket = gcsUri.indexOf(prefix) + prefix.length() + 1;
                int startIndex = gcsUri.indexOf("/", startIndexForBucket) + 1;
                fileName = gcsUri.substring(startIndex);
            }
            return fileName;
        }
    
        @Nullable
        String parseGcsUriForBucketName(String gcsUri) {
            String bucketName = null;
            String prefix = "gs://";
            if (gcsUri.startsWith(prefix)) {
                int startIndex = gcsUri.indexOf(prefix) + prefix.length();
                int endIndex = gcsUri.indexOf("/", startIndex);
                bucketName = gcsUri.substring(startIndex, endIndex);
            }
            return bucketName;
        }
    

提交回复
热议问题