天天看點

java azure blobs sas_用于 Java 的 Azure 存儲庫

您現在通路的是微軟AZURE全球版技術文檔網站,若需要通路由世紀互聯營運的MICROSOFT AZURE中國區技術文檔網站,請通路 https://docs.azure.cn.

用于 Java 的 Azure 存儲庫Azure Storage libraries for Java

10/19/2018

本文内容

概述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:

com.microsoft.azure

azure-storage-blob

10.1.0

隊列服務的依賴項:Dependency for Queue service:

com.microsoft.azure

azure-storage-queue

10.0.0-Preview

示例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.

com.microsoft.azure

azure-mgmt-storage

1.3.0

示例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 storageAccountKeys = storageAccount.getKeys();

for(StorageAccountKey key : storageAccountKeys) {

System.out.println("Key name: " + key.keyName() + " with value "+ key.value());

}

示例Samples