【发布时间】:2021-04-19 16:33:55
【问题描述】:
我知道有类似的问题,但没有一个对我有用。 我正在尝试从 Azure 存储下载文件。但我收到错误消息,错误消息是指定的 blob 不存在。但是 blob 确实存在,我的连接字符串也是有效的。我在下面分享我的代码。 如果有人可以帮助我,那将很有帮助。
代码:
public void downloadFile(ShowContentInfo showContentInfo){
ExecutorService executor = Executors.newSingleThreadExecutor();
Handler handler = new Handler(Looper.getMainLooper());
executor.execute(() ->{
CloudStorageAccount storageAccount = null;
try {
String dir = "/storage/emulated/0/Download/LmsContents/";
if (!dt.file.folderExists(dir)) {
dt.file.createFolder(dir);
}
storageAccount = CloudStorageAccount.parse(CONNECTION_STRING);
CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
CloudBlobContainer container = blobClient.getContainerReference("containerName");
CloudBlockBlob blockBlob = container.getBlockBlobReference("rblobName");
File file = new File("/storage/emulated/0/Download/LmsContents/" + showContentInfo.getContentTitle());
blockBlob.downloadToFile("/storage/emulated/0/Download/LmsContents/" + showContentInfo.getContentTitle());
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (StorageException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
handler.post(() -> {
Toast.makeText(context, "Successful", Toast.LENGTH_SHORT).show();
});
});
}
【问题讨论】:
-
您的代码在我看来没问题。请仔细检查容器名称和 blob 名称以及存储帐户是否存在。
-
我也有同样的想法,因为代码对我来说似乎还不错。看看别人的看法吧。