您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

用于 Java 的 Azure 存储库Azure Storage libraries for Java

概述Overview

使用 Azure 存储在 Java 应用程序中读取和写入 Blob(对象)数据、文件和消息。Read and write blob (object) data, files, and messages from your Java applications with Azure Storage.

若要开始使用 Azure 存储,请参阅如何通过 Java 使用 Blob 存储To get started with Azure Storage, see How to use Blob storage from Java.

客户端库Client library

使用 Azure Active Directory 中的共享密钥、SAS 令牌或 OAuth 令牌通过 Azure 存储服务进行授权。Use a Shared Key, SAS token or an OAuth token from the Azure Active Directory to authorize with Azure Storage services. 然后通过客户端库的类和方法来使用 Blob、文件或队列存储。Then use the client libraries' classes and methods to work with blob, file, or queue storage.

向 Maven pom.xml 文件中添加依赖项,以便在项目中使用客户端库。Add a dependency to your Maven pom.xml file to use the client library in your project.

Blob 服务的依赖项Dependency for Blob service:

<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>azure-storage-blob</artifactId>
    <version>10.1.0</version>
</dependency>

队列服务的依赖项Dependency for Queue service:

<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>azure-storage-queue</artifactId>
    <version>10.0.0-Preview</version>
</dependency>

示例Example

将本地文件系统中的映像文件写入现有 Azure 存储 Blob 容器中的新 Blob。Write an image file from the local file system into a new blob in an existing Azure Storage blob container.

// Retrieve the credentials and initialize SharedKeyCredentials
String accountName = System.getenv("AZURE_STORAGE_ACCOUNT");
String accountKey = System.getenv("AZURE_STORAGE_ACCESS_KEY");

// Create a BlockBlobURL to run operations on Block Blobs. Alternatively create a ServiceURL, or ContainerURL for operations on Blob service, and Blob containers
SharedKeyCredentials creds = new SharedKeyCredentials(accountName, accountKey);

// We are using a default pipeline here, you can learn more about it at https://github.com/Azure/azure-storage-java/wiki/Azure-Storage-Java-V10-Overview
final BlockBlobURL blobURL = new BlockBlobURL(
    new URL("https://" + accountName + ".blob.core.windows.net/mycontainer/myimage.jpg"), 
        StorageURL.createPipeline(creds, new PipelineOptions())
);

AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(Paths.get("myimage.jpg"));
TransferManager.uploadFileToBlockBlob(fileChannel, blobURL,0, null).blockingGet();

管理 APIManagement API

使用管理 API 创建和管理 Azure 存储帐户与连接密钥。Create and manage Azure Storage accounts and connection keys with the management API.

向 Maven pom.xml 文件中添加依赖项,以便在项目中使用管理 API。Add a dependency to your Maven pom.xml file to use the management API in your project.

<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>azure-mgmt-storage</artifactId>
    <version>1.3.0</version>
</dependency

示例Example

在订阅中创建新的 Azure 存储帐户并检索其访问密钥。Create a new Azure Storage account in your subscription and retrieve its access keys.

StorageAccount storageAccount = azure.storageAccounts().define(storageAccountName)
        .withRegion(Region.US_EAST)
        .withNewResourceGroup(rgName)
        .create();

// get a list of storage account keys related to the account
List<StorageAccountKey> storageAccountKeys = storageAccount.getKeys();
for(StorageAccountKey key : storageAccountKeys)    {
    System.out.println("Key name: " + key.keyName() + " with value "+ key.value());
}

示例Samples

用于 Java 的 Azure 存储 SDK 在 Blob 存储中读取和写入对象 Azure Storage SDK for Java Read and write objects to blob storage
使用队列读取和写入消息Read and write messages with queues