问题描述
使用Azure Storage Account 的File Share,如何通过Java 代码来下载、上传文件呢?
问题解答
参考Github中Azure File Share代码介绍( https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/storage/azure-storage-file-share#azure-file-share-client-library-for-java )。可以使用 azure-storage-file-share 来实现上传下载功能。
第一步:引入 azure-storage-file-share JDK
<dependency> <groupId>com.azure</groupId> <artifactId>azure-storage-file-share</artifactId> <version>12.28.0-beta.1</version> </dependency>
第二步:根据 Storage Account的Connection String和File Share Endpoint创建 ShareDirectoryClient 或 ShareFileClient 对象
第三步:调用 upload() 或 downloadToFile() 上传/下载文件
完整的示例代码如下:
package com.blobs.quickstart; /** * Azure blob storage v12 SDK quickstart */ import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; import com.azure.storage.file.share.ShareDirectoryClient; import com.azure.storage.file.share.ShareFileClient; import com.azure.storage.file.share.ShareFileClientBuilder; public class App { public static void main( String[] args ) throws IOException { System.out.println("Azure file share\n"); final String ACCOUNT_NAME = ""; final String CONNECTION_STRING = ""; final String SHARE_NAME = ""; final String DIRECTORY_PATH = "test"; final String FILE_NAME = "code.txt"; String fileURL = String.format("https://%s.file.core.chinacloudapi.cn", ACCOUNT_NAME); ShareDirectoryClient directoryClient = new ShareFileClientBuilder() .connectionString(CONNECTION_STRING) .endpoint(fileURL) .shareName(SHARE_NAME) .resourcePath(DIRECTORY_PATH) .buildDirectoryClient(); //Upload file String fileName = "testfile1.txt"; long maxSize = 1024; ShareFileClient fclient = directoryClient.createFile(fileName, maxSize); String uploadText = "Hello, World! This is a test file for Azure file share.\n"; InputStream data = new ByteArrayInputStream(uploadText.getBytes(StandardCharsets.UTF_8)); fclient.upload(data, uploadText.length()); //Download file ShareFileClient fileClient= new ShareFileClientBuilder() .connectionString(CONNECTION_STRING) .endpoint(fileURL) .shareName(SHARE_NAME) .resourcePath(DIRECTORY_PATH + "/" + FILE_NAME) .buildFileClient(); fileClient.downloadToFile("downloaded_"+java.util.UUID.randomUUID() + "_" + FILE_NAME); System.out.println("Done"); } }
完整的POM.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.blobs.quickstart</groupId> <artifactId>blob-quickstart-v12</artifactId> <version>1.0-SNAPSHOT</version> <name>blob-quickstart-v12</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>com.azure</groupId> <artifactId>azure-storage-file-share</artifactId> <version>12.28.0-beta.1</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.25</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.25</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> </dependencies> <build> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle --> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-jar-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle --> <plugin> <artifactId>maven-site-plugin</artifactId> <version>3.7.1</version> </plugin> <plugin> <artifactId>maven-project-info-reports-plugin</artifactId> <version>3.0.0</version> </plugin> </plugins> </pluginManagement> </build> </project>
测试结果:
参考资料
Azure File Share client library for Java : https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/storage/azure-storage-file-share#azure-file-share-client-library-for-java
当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!