使用BlobOutputStream在Azure中上传Blob [英] Upload blob in Azure using BlobOutputStream

查看:168
本文介绍了使用BlobOutputStream在Azure中上传Blob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试直接从流中上传Blob,因为我不知道流的长度,因此我决定尝试使用

I'm trying to upload a blob directly from a stream, since I don't know the length of the stream I decided to try with this answer.

这是行不通的,即使它从流中读取并且没有引发任何异常,也没有将内容没有上传到我的容器中.

This doesn't work, even though it reads from the stream and doesn't throw any exceptions the content isn't uploaded to my container.

我从文件上传没有问题,仅在从流中上传时发生.

I have no problem uploading from files, it only occurs when uploading from a stream.

这是我的代码,我添加了一些输出来检查它是否正在读取某些内容,但这不是问题所在:

This is my code, I added a few outs to check whether it was reading something or not but that wasn't the problem:

try {
    CloudBlockBlob blob = PublicContainer.getBlockBlobReference(externalFileName);
    if (externalFileName.endsWith(".tmp")) {
        blob.getProperties().setContentType("image/jpeg");
    }
    BlobOutputStream blobOutputStream = blob.openOutputStream();
    int next = input.read();
    while (next != -1) {
        System.err.println("writes");
        blobOutputStream.write(next);
        next = input.read();
    }
    blobOutputStream.close();
    return blob.getUri().toString();

} catch (Exception usex) {
    System.err.println("ERROR " + usex.getMessage());
    return "";
}

它不会失败,但是不会起作用.

It doesn't fails but it doesn't works.

还有另一种方法吗?还是我错过了什么?

Is there another way of doing this? Or am I missing something?

更新:我一直在检查,我认为问题出在InputStream本身,但我不知道为什么,因为如果我使用同一流进行上传,它也可以正常工作例如Amazon s3

UPDATE: I've been checking and I think that the problem is with the InputStream itself, but I don't know why since the same stream will work just fine if I use it to upload to Amazon s3 for instance

推荐答案

我尝试重现您的问题,但失败了.根据您的代码,似乎唯一明显的丢失是在通过blobOutputStream.close();关闭输出流之前没有调用blobOutputStream.flush();,但是如果缺少flush方法,则可以正常工作

I tried to reproduce your issue, but failed. According to your code, it seems that the only obvious missing thing is no calling blobOutputStream.flush(); before close the output stream via blobOutputStream.close();, but it works if missing flush method

这是我的测试代码,如下所示.

Here is my testing code as below.

String STORAGE_CONNECTION_STRING_TEMPLATE = "DefaultEndpointsProtocol=https;AccountName=%s;AccountKey=%s;";

String accountName = "xxxx";
String key = "XXXXXX";
CloudStorageAccount account = CloudStorageAccount.parse(String.format(STORAGE_CONNECTION_STRING_TEMPLATE, accountName, key));
CloudBlobClient client = account.createCloudBlobClient();
CloudBlobContainer container = client.getContainerReference("mycontainer");
container.createIfNotExists();
String externalFileName = "test.tmp";
CloudBlockBlob blob = container.getBlockBlobReference(externalFileName);
if (externalFileName.endsWith(".tmp")) {
    blob.getProperties().setContentType("image/jpeg");
}
BlobOutputStream blobOutputStream = blob.openOutputStream();
String fileName = "test.jpg";
InputStream input = new FileInputStream(fileName);
int next = -1;
while((next = input.read()) != -1) {
    blobOutputStream.write(next);
}
blobOutputStream.close(); // missing in your code, but works if missing.
input.close();

如果您可以更详细地更新,我认为这有助于分析问题.如有任何疑问,请随时告诉我.

If you can update in more details, I think it's help for analysising the issue. Any concern, please feel free to let me know.

这篇关于使用BlobOutputStream在Azure中上传Blob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆