【问题标题】:How to send inputstream of a file as response with azure-storage-file-datalake sdk?如何使用 azure-storage-file-datalake sdk 发送文件的输入流作为响应?
【发布时间】:2021-02-24 18:24:38
【问题描述】:

我想在 Web 应用程序中下载一个大的 csv 文件。 Web 应用程序向服务发送 api 请求,然后使用 azure-storage-file-datalake 中的 ADLSClient 访问 Azure Data Lake Storage API。服务中的示例代码如下:

val client = getADLSClientGen2(dataSourceInstanceName, fileSystem)
val fileClient = client.getFileClient(filePath)
val outputStream: OutputStream = ByteArrayOutputStream()
fileClient.read(outputStream)
outputStream.close()
val buffer = outputStream as ByteArrayOutputStream
return ByteArrayInputStream(buffer.toByteArray())

在上面的代码中,整个文件在输出流中被读取,然后它的输入流作为 api 请求中的响应发送。我希望能够直接发送从 adls 文件系统读取的文件的输入流

【问题讨论】:

    标签: java azure jax-rs azure-data-lake azure-data-lake-gen2


    【解决方案1】:

    据我了解,在您的 Web 应用程序中,您希望为存储在 ADLS Gen2 中的 .CSV 文件提供输入流。由于 ADLS Gen2 是基于 Azure 存储服务构建的,因此在服务器端,我们可以为您的 Web 应用程序提供一个 SAS token,并且 Web 应用程序发出 HTTP 请求以直接从 ADLS Gen2 下载此文件,以便您可以获取此文件来自 HTTP 响应的输入流。

    这是生成 blob sas 令牌的代码:

            String connString = "<conntion string>";
            String containerName = "<container name>";
            String blobName = "<.csv name>";
    
            BlobServiceClient client = new BlobServiceClientBuilder().connectionString(connString).buildClient();
            BlobClient blobClient = client.getBlobContainerClient(containerName).getBlobClient(blobName);
    
            BlobSasPermission blobSasPermission = new BlobSasPermission().setReadPermission(true); // grant read
                                                                                                   // permission
                                                                                                   // onmy
            OffsetDateTime expiryTime = OffsetDateTime.now().plusDays(1); // 1 day to expire
            BlobServiceSasSignatureValues values = new BlobServiceSasSignatureValues(expiryTime, blobSasPermission)
                            .setStartTime(OffsetDateTime.now());
    
            System.out.println(blobClient.getBlobUrl() + "?" + blobClient.generateSas(values));
    

    maven 依赖:

       <dependency>
          <groupId>com.azure</groupId>
          <artifactId>azure-storage-blob</artifactId>
          <version>12.9.0</version>
        </dependency>
    

    Web 应用程序示例代码:

    <html>
    
        <body>
        
            
        
        
        </body>
        
        <script>
        
            var xhr = new XMLHttpRequest();
                xhr.open('GET', 'CVS file URL with sas');
                xhr.seenBytes = 0;
    
                xhr.onreadystatechange = function() {
                  console.log("state change.. state: "+ xhr.readyState);
    
                  if(xhr.readyState == 3) {
                    var newData = xhr.response.substr(xhr.seenBytes);
                    console.log("newData: <<" +newData+ ">>");
                    document.body.innerHTML += "New data: " +newData+ "<br />";
    
                    xhr.seenBytes = xhr.responseText.length;
                    console.log("seenBytes: " +xhr.seenBytes);
                  }
                };
    
                xhr.addEventListener("error", function(e) {
                  console.log("error: " +e);
                });
    
                console.log(xhr);
                xhr.send();
        </script>
    
    </html>

    CSV 文件内容以增量方式加载:

    【讨论】:

      猜你喜欢
      • 2021-01-10
      • 2022-11-20
      • 2021-10-26
      • 2020-06-20
      • 2016-01-17
      • 2021-01-17
      • 1970-01-01
      • 2023-03-15
      • 2021-03-17
      相关资源
      最近更新 更多