Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Azure blob文件下载在下载几个文件后失败_Java_Azure Blob Storage - Fatal编程技术网

Java Azure blob文件下载在下载几个文件后失败

Java Azure blob文件下载在下载几个文件后失败,java,azure-blob-storage,Java,Azure Blob Storage,我尝试使用BlobClient.downloadToFile(dest)下载blob文件,但在下载了一些文件后失败了。我在尝试从Azure Blob存储下载一个3GB的单一文件时遇到了同样的问题 package com.blob.download; import com.azure.storage.blob.BlobContainerClient; import com.azure.storage.blob.BlobServiceClient; import com.azure.storage.

我尝试使用BlobClient.downloadToFile(dest)下载blob文件,但在下载了一些文件后失败了。我在尝试从Azure Blob存储下载一个3GB的单一文件时遇到了同样的问题

package com.blob.download;
import com.azure.storage.blob.BlobContainerClient;
import com.azure.storage.blob.BlobServiceClient;
import com.azure.storage.blob.BlobServiceClientBuilder;
import com.azure.storage.blob.models.BlobItem;
import com.azure.storage.blob.models.ListBlobsOptions;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Iterator;
public class BlobDownload {
    public static void main(String args[]){
        BlobServiceClientBuilder builder = new BlobServiceClientBuilder();
        //Specify storage uri
        String storageBaseUri = "";
        ///Specify connection string
        String connectionString = "";
        //Specify blob list prefix
        String key = "";
        builder.connectionString(connectionString);
        BlobServiceClient client = builder.buildClient();
        if (!storageBaseUri.startsWith(client.getAccountUrl())) {
            throw new IllegalArgumentException(
                    "The given credential can not be used for the specified container.");
        }
        String containerName = storageBaseUri.replace(client.getAccountUrl() + "/", "");
        BlobContainerClient blobContainerClient = client.getBlobContainerClient(containerName);
        System.out.println("Downloading " + blobContainerClient.getBlobContainerName() + "/" + key);
        Iterator<BlobItem> keyspaceBlobs =
                blobContainerClient.listBlobs(new ListBlobsOptions().setPrefix(key + "/"), null).iterator();
        while (keyspaceBlobs.hasNext()){
            BlobItem blob = keyspaceBlobs.next();
            Path destFile = Paths.get("/opt/data/", blob.getName());
            try {
                Files.createDirectories(destFile.getParent());
            } catch (IOException e) {
                System.out.println(e);
            }
            blobContainerClient
                    .getBlobClient(blob.getName())
                    .downloadToFile(destFile.toString());
            System.out.println("Download file succeeded : " + destFile.toString());
        }
    }
}
我收到了以下错误消息

Exception in thread "main" reactor.core.Exceptions$ReactiveException: java.io.IOException: Connection reset by peer
    at reactor.core.Exceptions.propagate(Exceptions.java:393)
    at reactor.core.publisher.BlockingSingleSubscriber.blockingGet(BlockingSingleSubscriber.java:97)
    at reactor.core.publisher.Mono.block(Mono.java:1678)
    at com.azure.storage.common.implementation.StorageImplUtils.blockWithOptionalTimeout(StorageImplUtils.java:99)
    at com.azure.storage.blob.specialized.BlobClientBase.downloadToFileWithResponse(BlobClientBase.java:563)
    at com.azure.storage.blob.specialized.BlobClientBase.downloadToFile(BlobClientBase.java:488)
    at com.azure.storage.blob.specialized.BlobClientBase.downloadToFile(BlobClientBase.java:457)
    at com.blob.download.BlobDownload.main(BlobDownload.java:57)
    Suppressed: java.lang.Exception: #block terminated with an error
        at reactor.core.publisher.BlockingSingleSubscriber.blockingGet(BlockingSingleSubscriber.java:99)
        ... 6 more
Caused by: java.io.IOException: Connection reset by peer
    at sun.nio.ch.FileDispatcherImpl.read0(Native Method)
    at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:39)
    at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223)
    at sun.nio.ch.IOUtil.read(IOUtil.java:192)
    at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:379)
    at io.netty.buffer.PooledByteBuf.setBytes(PooledByteBuf.java:247)
    at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1147)
    at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:347)
    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:148)
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:700)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:635)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:552)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:514)
    at io.netty.util.concurrent.SingleThreadEventExecutor$6.run(SingleThreadEventExecutor.java:1044)
    at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
    at java.lang.Thread.run(Thread.java:748)

如果在我的实现中有任何错误用法,请您指导我好吗?

我已经做了一个测试。你的代码基本上没有问题

但下载了一些文件后失败了

你下载了多少文件?我使用你的代码,它似乎工作得很好

我在尝试从中下载一个3GB的单一文件时遇到了同样的问题 Azure Blob存储

package com.blob.download;
import com.azure.storage.blob.BlobContainerClient;
import com.azure.storage.blob.BlobServiceClient;
import com.azure.storage.blob.BlobServiceClientBuilder;
import com.azure.storage.blob.models.BlobItem;
import com.azure.storage.blob.models.ListBlobsOptions;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Iterator;
public class BlobDownload {
    public static void main(String args[]){
        BlobServiceClientBuilder builder = new BlobServiceClientBuilder();
        //Specify storage uri
        String storageBaseUri = "";
        ///Specify connection string
        String connectionString = "";
        //Specify blob list prefix
        String key = "";
        builder.connectionString(connectionString);
        BlobServiceClient client = builder.buildClient();
        if (!storageBaseUri.startsWith(client.getAccountUrl())) {
            throw new IllegalArgumentException(
                    "The given credential can not be used for the specified container.");
        }
        String containerName = storageBaseUri.replace(client.getAccountUrl() + "/", "");
        BlobContainerClient blobContainerClient = client.getBlobContainerClient(containerName);
        System.out.println("Downloading " + blobContainerClient.getBlobContainerName() + "/" + key);
        Iterator<BlobItem> keyspaceBlobs =
                blobContainerClient.listBlobs(new ListBlobsOptions().setPrefix(key + "/"), null).iterator();
        while (keyspaceBlobs.hasNext()){
            BlobItem blob = keyspaceBlobs.next();
            Path destFile = Paths.get("/opt/data/", blob.getName());
            try {
                Files.createDirectories(destFile.getParent());
            } catch (IOException e) {
                System.out.println(e);
            }
            blobContainerClient
                    .getBlobClient(blob.getName())
                    .downloadToFile(destFile.toString());
            System.out.println("Download file succeeded : " + destFile.toString());
        }
    }
}

根据您的代码,3gb将导致问题。您需要分块下载文件以防止握手超时问题。

>您下载了多少文件?以下是测试结果*Azure blob文件下载可正常使用1200个文件(总文件大小为52.32 MiB)*Azure blob文件下载失败,使用单个3GB文件(java.io.IOException:由对等方重置连接)*Azure blob文件下载在下载251个文件后失败(167MB是单个文件的最大大小,总下载文件大小为3.6G)*Azure blob文件下载5个文件后下载失败(每个文件大小为167MB)