我无法将Azure Blob文件下载到本地磁盘。请在下面找到我用来下载文件的Java代码。运行并执行HTTP触发测试时,看不到文件下载到本地路径中。另外,我已经将身份验证授予了公共访问权限。并且,我能够使用blob.downloadText()读取txt文件的内容。但是我无法将其下载到文件中。
我的要求是将Blob存储中的pdf下载到我的本地磁盘。
@FunctionName("BlobDownload-Java")
public void run1(
@HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.FUNCTION) HttpRequestMessage<Optional<String>> request,
final ExecutionContext context) {
context.getLogger().info("Java HTTP trigger processed a request.");
try {
// Retrieve storage account from connection-string.
CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
// Create the blob client.
CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
// Get a reference to a container.
// The container name must be lower case
CloudBlobContainer container = blobClient.getContainerReference("doccontainer");
CloudBlockBlob blob1 = container.getBlockBlobReference("AssembledDoc.pdf");
context.getLogger().info("File Name Check 1 ----->" + blob1.getName());
CloudBlockBlob blob2 = container.getBlockBlobReference("Test.txt");
String s= blob2.downloadText();
context.getLogger().info("Text Document content ----->" + s );
File file = new File("C:\\Users\\wb541348\\Project\\Temp.txt");
blob2.downloadToFile(file.getAbsolutePath());
答案 0 :(得分:0)
@Prasanna,您无法将Blob下载到您的本地路径中,因为您的路径模式错误,它应该是“ c:/Users/wb541348/Project/Temp.txt”,还有一点要注意,即需要文件名,这意味着路径不应该只是文件夹,希望它会有所帮助。 您也可以单击此sample of azure blob for java sdk以供参考。
答案 1 :(得分:0)
如果您只是想将PDF文件下载到本地,这是我的测试代码,可以尝试一下。
@Test
public void downloadBlob(){
try {
CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
//Create the service client object for credentialed access to the Blob service.
CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
// Retrieve a reference to a container.
CloudBlobContainer container = blobClient.getContainerReference("blobcontainer");
CloudBlob blob1 =container.getBlockBlobReference("thinking-in-java.pdf");
blob1.download(new FileOutputStream("C:\\Users\\georgec\\Documents\\" + blob1.getName()));
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (StorageException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
如果您仍有疑问,请告诉我。