本文介绍如何使用适用于 Java 的 Azure Blob 存储客户端库连接到 Azure Blob 存储。 连接后,可以针对 Blob 存储服务的容器、Blob 和功能运行代码。
API 参考
|
包 (Maven)
|
库源代码
|
示例
|
提供反馈
Azure 订阅 -
创建免费帐户
Azure 存储帐户 -
创建存储帐户
Java 开发工具包 (JDK)
8 或更高版本
在本示例中,
Apache Maven
用于项目管理
使用 Maven 创建新的控制台应用,或打开现有项目。 在文本编辑器中打开 pom.xml 文件
pom.xml
。
添加 azure-sdk-bom,以在最新版本的库中采用一个依赖项。 在以下代码片段中,将
{bom_version_to_target}
占位符替换为版本号。 使用 azure-sdk-bom 则无需指定每个依赖项的版本。 若要了解有关 BOM 的详细信息,请参阅
Azure SDK BOM 自述文件
。
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-sdk-bom</artifactId>
<version>{bom_version_to_target}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
将以下依赖项元素添加到依赖项组。 与 Azure 服务建立无密码连接需要 azure-identity 依赖项。
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-storage-blob</artifactId>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-storage-common</artifactId>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
</dependency>
然后,打开代码文件并添加必要的 import
指令。 在此示例中,在 App.java 文件中添加以下指令:
import com.azure.core.credential.*;
import com.azure.identity.*;
import com.azure.storage.blob.*;
import com.azure.storage.blob.models.*;
import com.azure.storage.blob.specialized.*;
import com.azure.storage.common.*;
Blob 客户端库信息:
com.azure.storage.blob:包含可用于对服务、容器和 Blob 执行操作的主要类(客户端对象)。
com.azure.storage.blob.models:包含实用工具类、结构和枚举类型。
com.azure.storage.blob.specialized:包含可用于执行特定于 Blob 类型(例如追加 Blob)的操作的类。
授予访问权限并连接到 Blob 存储
要将应用连接到 Blob 存储,请创建 BlobServiceClient 类的实例。 此对象是在存储帐户级别与数据资源交互的起点。 可以将其用于对存储账户及其容器执行操作。 还可以使用服务客户端创建容器客户端或 blob 客户端,具体取决于需要使用的资源。
要详细了解如何创建和管理客户端对象,请参阅 创建和管理与数据资源交互的客户端对象。
你可以使用 Azure Active Directory (Azure AD) 授权令牌、帐户访问密钥或共享访问签名 (SAS) 来为 BlobServiceClient
对象授权。
Azure AD(推荐)
SAS 令牌
使用 DefaultAzureCredential 授权访问
要授予访问权限并连接到 Blob 存储,一种简单且安全的方法是通过创建 DefaultAzureCredential 实例来获取 OAuth 令牌。 然后就可以使用该凭据创建 BlobServiceClient 对象。
确保你在 pom.xml 中拥有正确的依赖项以及必要的导入指令,如设置项目中所述。
以下示例使用 BlobServiceClientBuilder 通过 DefaultAzureCredential
生成 BlobServiceClient
对象:
public static BlobServiceClient GetBlobServiceClientTokenCredential() {
TokenCredential credential = new DefaultAzureCredentialBuilder().build();
// Azure SDK client builders accept the credential as a parameter
// TODO: Replace <storage-account-name> with your actual storage account name
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
.endpoint("https://<storage-account-name>.blob.core.windows.net/")
.credential(credential)
.buildClient();
return blobServiceClient;
使用 BlobServiceClientBuilder 通过 SAS 令牌生成 BlobServiceClient 对象:
public static BlobServiceClient GetBlobServiceClientSAS(String sasToken) {
// TODO: Replace <storage-account-name> with your actual storage account name
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
.endpoint("https://<storage-account-name>.blob.core.windows.net/")
.sasToken(sasToken)
.buildClient();
return blobServiceClient;
若要生成和管理 SAS 令牌,请参阅使用共享访问签名 (SAS) 授予对 Azure 存储资源的有限访问权限。
使用存储帐户名称和帐户密钥创建 StorageSharedKeyCredential。 然后,使用该对象初始化 BlobServiceClient 对象。
public static BlobServiceClient GetBlobServiceClientAccountKey(String accountName, String accountKey) {
StorageSharedKeyCredential credential = new StorageSharedKeyCredential(accountName, accountKey);
// Azure SDK client builders accept the credential as a parameter
// TODO: Replace <storage-account-name> with your actual storage account name
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
.endpoint("https://<storage-account-name>.blob.core.windows.net/")
.credential(credential)
.buildClient();
return blobServiceClient;
还可以使用连接字符串创建 BlobServiceClient
对象。
public static BlobServiceClient GetBlobServiceClientConnectionString(String connectionString) {
// Azure SDK client builders accept the credential as a parameter
// TODO: Replace <storage-account-name> with your actual storage account name
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
.endpoint("https://<storage-account-name>.blob.core.windows.net/")
.connectionString(connectionString)
.buildClient();
return blobServiceClient;
有关如何获取帐户密钥的信息以及正确管理和保护密钥的最佳做法指导,请参阅管理存储帐户访问密钥。
应谨慎使用帐户访问密钥。 如果帐户访问密钥丢失或意外放置在不安全的位置,服务可能会变得易受攻击。 具有访问密钥的任何人都可以授权针对存储帐户的请求,并且实际上有权访问所有数据。 DefaultAzureCredential
提供增强的安全功能和优势,是管理 Azure 服务授权的推荐方法。
生成应用程序
在生成应用程序时,代码主要与三种类型的资源交互:
存储帐户,它是 Azure 存储数据的唯一顶级命名空间。
容器,用于组织存储帐户中的 Blob 数据。
Blob,用于存储非结构化数据,例如文本和二进制数据。
以下图示显示了这些资源之间的关系。
每种类型的资源由一个或多个关联的 Java 类表示。 这些类是基本类: