gpt4 book ai didi

java - 无法读取上传到 Azure Blob 的文件

转载 作者:行者123 更新时间:2023-12-02 04:19:08 24 4
gpt4 key购买 nike

尝试使用 startCopy() 将文档从公共(public) url 上传到 Blob 时,无法上传文件内容

使用 uploadFromFile(filePath) 上传相同的网址

非工作代码

URL url =new URL  ("http://irpages2.equitystory.com/download/companies/douglasgmbh/Pres_web/6M_FY2018-19_InvestorUpdate.pdf");

newBlobReference.startCopy(new URI(url.toString()));

工作代码

        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
try (ReadableByteChannel rbc = Channels.newChannel(conn.getInputStream());
final FileOutputStream fos = new FileOutputStream(filePath)) {

fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);

}


newBlobReference.uploadFromFile(filePath);

希望上传文档,而不必先将它们下载到本地系统上,因为这对于大量文档来说需要时间。其次,即使容器中存在 blob,也无法读取使用 startCopy() 下载的 URL

最佳答案

我尝试使用 startCopy 并得到与您相同的结果,该文件没有内容。我不知道原因,所以我使用 BlobOutputStream 上传文件,它成功了。下面是我的代码,你可以尝试一下。

        String connectionString = String.format("DefaultEndpointsProtocol=http;AccountName=%s;AccountKey=%s", ACCOUNT_NAME, ACCOUNT_KEY);
CloudStorageAccount account = CloudStorageAccount.parse(connectionString);
CloudBlobContainer container = null;

CloudBlobClient client = account.createCloudBlobClient();

container = client.getContainerReference("blobcontainer");

CloudBlockBlob blob = container.getBlockBlobReference("6M_FY2018-19_InvestorUpdate.pdf");

URL uri =new URL("http://irpages2.equitystory.com/download/companies/douglasgmbh/Pres_web/6M_FY2018-19_InvestorUpdate.pdf");

InputStream is=uri.openStream();

BlobOutputStream blobOutputStream = blob.openOutputStream();

int next = is.read();
while (next != -1) {
blobOutputStream.write(next);
next = is.read();
}
blobOutputStream.close();

关于java - 无法读取上传到 Azure Blob 的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56647058/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com