是否可以用Java代码来管理Azure blob? 可以。在代码中加入azure-storage-blob依赖。即可使用以下类操作Azure Storage Blob。
首先,设置VS Code上执行Java 代码的环境,这里需要的步骤比较多。完全参考VS Code for Java文档即可:https://code.visualstudio.com/docs/java/java-tutorial.
以下是创建项目步骤:
1)创建blob-quickstart-v12项目
跳转到blob-quickstart-v12目录,并在目录中创建一个data目录,用于在接下来的代码中创建本地文件及下载bolb中的文件
cd blob-quickstart-v12
mkdir data
2) 修改pom.xml,添加对Java azure-storage-blob SDK的依赖
<dependencies> <dependency> <groupId>junitgroupId> <artifactId>junitartifactId> <version>4.11version> <scope>testscope> dependency> <dependency> <groupId>com.azuregroupId> <artifactId>azure-storage-blobartifactId> <version>12.6.0version> dependency> dependencies>
3)在App.java代码文件中添加引用
package com.blobs.quickstart; /** * Azure blob storage v12 SDK quickstart */ import com.azure.storage.blob.*; import com.azure.storage.blob.models.*; import java.io.*; public class App { public static void main( String[] args ) throws IOException { System.out.println("Azure Blob storage v12 - Java quickstart sample\n");
4)添加Stroage Account的连接字符串
5)创建blobServiceClient对象,同时调用createBlobContainer方法创建容器
// Create a BlobServiceClient object which will be used to create a container client BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().connectionString(connectStr).buildClient(); //Create a unique name for the container String cOntainerName= "quickstartblobs" + java.util.UUID.randomUUID(); // Create the container and return a container client object BlobContainerClient cOntainerClient= blobServiceClient.createBlobContainer(containerName);
6)创建BlobClient对象,同时调用uploadFromFile方法上传文件(.tx)
// Create a local file in the ./data/ directory for uploading and downloading String localPath = "./data/"; String fileName = "quickstart" + java.util.UUID.randomUUID() + ".txt"; File localFile = new File(localPath + fileName); // Write text to the file FileWriter writer = new FileWriter(localPath + fileName, true); writer.write("Hello, World!"); writer.close(); // Get a reference to a blob BlobClient blobClient = containerClient.getBlobClient(fileName); System.out.println("\nUploading to Blob storage as blob:\n\t" + blobClient.getBlobUrl()); // Upload the blob blobClient.uploadFromFile(localPath + fileName);
7)使用containerClient对象的listBlobs方法列出容器中的BlobItem
System.out.println("\nListing blobs..."); // List the blob(s) in the container. for (BlobItem blobItem : containerClient.listBlobs()) { System.out.println("\t" + blobItem.getName()); }
8)使用blobClient对象的downloadToFile方法下载文件到本地
// Download the blob to a local file // Append the string "DOWNLOAD" before the .txt extension so that you can see both files. String downloadFileName = fileName.replace(".txt", "DOWNLOAD.txt"); File downloadedFile = new File(localPath + downloadFileName); System.out.println("\nDownloading blob to\n\t " + localPath + downloadFileName); blobClient.downloadToFile(localPath + downloadFileName);
9)使用containerClient对象的delete方法删除容器
// Clean up System.out.println("\nPress the Enter key to begin clean up"); System.console().readLine(); System.out.println("Deleting blob container..."); containerClient.delete(); System.out.println("Deleting the local source and downloaded files..."); localFile.delete(); downloadedFile.delete(); System.out.println("Done");
10) 在VS Code中调试或执行,右键选择Run/Debug
App.java文件
package com.blobs.quickstart; /** * Azure blob storage v12 SDK quickstart */ import com.azure.storage.blob.*; import com.azure.storage.blob.models.*; import java.io.*; public class App { public static void main( String[] args ) throws IOException { System.out.println("Azure Blob storage v12 - Java quickstart sample\n"); // Retrieve the connection string for use with the application. The storage // connection string is stored in an environment variable on the machine // running the application called AZURE_STORAGE_CONNECTION_STRING. If the environment variable // is created after the application is launched in a console or with // Visual Studio, the shell or application needs to be closed and reloaded // to take the environment variable into account. String cOnnectStr="DefaultEndpointsProtocol=https;AccountName=xxxxxxxx;AccountKey=xxxxxxxxxxxxxxx;EndpointSuffix=core.chinacloudapi.cn";// System.getenv("AZURE_STORAGE_CONNECTION_STRING"); // Create a BlobServiceClient object which will be used to create a container client BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().connectionString(connectStr).buildClient(); //Create a unique name for the container String cOntainerName= "quickstartblobs" + java.util.UUID.randomUUID(); // Create the container and return a container client object BlobContainerClient cOntainerClient= blobServiceClient.createBlobContainer(containerName); // Create a local file in the ./data/ directory for uploading and downloading String localPath = "./data/"; String fileName = "quickstart" + java.util.UUID.randomUUID() + ".txt"; File localFile = new File(localPath + fileName); // Write text to the file FileWriter writer = new FileWriter(localPath + fileName, true); writer.write("Hello, World!"); writer.close(); // Get a reference to a blob BlobClient blobClient = containerClient.getBlobClient(fileName); System.out.println("\nUploading to Blob storage as blob:\n\t" + blobClient.getBlobUrl()); // Upload the blob blobClient.uploadFromFile(localPath + fileName); System.out.println("\nListing blobs..."); // List the blob(s) in the container. for (BlobItem blobItem : containerClient.listBlobs()) { System.out.println("\t" + blobItem.getName()); } // Download the blob to a local file // Append the string "DOWNLOAD" before the .txt extension so that you can see both files. String downloadFileName = fileName.replace(".txt", "DOWNLOAD.txt"); File downloadedFile = new File(localPath + downloadFileName); System.out.println("\nDownloading blob to\n\t " + localPath + downloadFileName); blobClient.downloadToFile(localPath + downloadFileName); // Clean up System.out.println("\nPress the Enter key to begin clean up"); System.console().readLine(); System.out.println("Deleting blob container..."); containerClient.delete(); System.out.println("Deleting the local source and downloaded files..."); localFile.delete(); downloadedFile.delete(); System.out.println("Done"); } }
pom.xml文件
xml version="1.0" encoding="UTF-8"?> <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.0modelVersion> <groupId>com.blobs.quickstartgroupId> <artifactId>blob-quickstart-v12artifactId> <version>1.0-SNAPSHOTversion> <name>blob-quickstart-v12name> <url>http://www.example.comurl> <properties> <project.build.sourceEncoding>UTF-8project.build.sourceEncoding> <maven.compiler.source>1.7maven.compiler.source> <maven.compiler.target>1.7maven.compiler.target> properties> <dependencies> <dependency> <groupId>junitgroupId> <artifactId>junitartifactId> <version>4.11version> <scope>testscope> dependency> <dependency> <groupId>com.azuregroupId> <artifactId>azure-storage-blobartifactId> <version>12.6.0version> dependency> dependencies> <build> <pluginManagement> <plugins> <plugin> <artifactId>maven-clean-pluginartifactId> <version>3.1.0version> plugin> <plugin> <artifactId>maven-resources-pluginartifactId> <version>3.0.2version> plugin> <plugin> <artifactId>maven-compiler-pluginartifactId> <version>3.8.0version> plugin> <plugin> <artifactId>maven-surefire-pluginartifactId> <version>2.22.1version> plugin> <plugin> <artifactId>maven-jar-pluginartifactId> <version>3.0.2version> plugin> <plugin> <artifactId>maven-install-pluginartifactId> <version>2.5.2version> plugin> <plugin> <artifactId>maven-deploy-pluginartifactId> <version>2.8.2version> plugin> <plugin> <artifactId>maven-site-pluginartifactId> <version>3.7.1version> plugin> <plugin> <artifactId>maven-project-info-reports-pluginartifactId> <version>3.0.0version> plugin> plugins> pluginManagement> build> project>
Getting Started with Java in VS Code:https://code.visualstudio.com/docs/java/java-tutorial
https://docs.azure.cn/zh-cn/storage/blobs/storage-quickstart-blobs-java#code-examples