1. 程式人生 > >封裝微軟(Microsoft) Azure 獲得container 實現增加、查詢、刪除功能

封裝微軟(Microsoft) Azure 獲得container 實現增加、查詢、刪除功能

1. 所需 jar 包:

<!--microsoft azure-->
<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>azure-storage</artifactId>
    <version>7.0.0</version>
</dependency>

2. 程式碼:

package com.dmap.base.units.azure.blob;

import com.microsoft.azure.storage.CloudStorageAccount;
import com.microsoft.azure.storage.OperationContext; import com.microsoft.azure.storage.StorageException; import com.microsoft.azure.storage.blob.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.*; import java.net.URISyntaxException; import java.security.InvalidKeyException;
public class AzureApp { private AzureApp() { } private static final Logger logger = LoggerFactory.getLogger(AzureApp.class); /** * 1步:獲取要使用的容器 * * @param accountName 使用 Azure 門戶中列出的儲存帳戶的名稱 * @param acountKey 儲存帳戶的主訪問金鑰 * @param containerName 1.容器名稱必須以字母或數字開頭,並且只能包含字母、數字和短劃線
(-) 字元。 * 2.每個短劃線 (-) 字元的前面和後面都必須是一個字母或數字;在容器名稱中不允許連續的短劃線 (-) * 3.容器名稱中的所有字母都必須為小寫。 * 4.容器名稱必須介於 3 63 個字元。 * @return */ public static CloudBlobContainer getCloudBlobContainer(String accountName, String acountKey, String containerName) { //獲取儲存連線字串 String storageConnectionString = "DefaultEndpointsProtocol=https;AccountName=" + accountName + ";AccountKey=" + acountKey + ";EndpointSuffix=core.chinacloudapi.cn"; CloudStorageAccount storageAccount; CloudBlobClient blobClient = null; CloudBlobContainer container = null; try { storageAccount = CloudStorageAccount.parse(storageConnectionString); blobClient = storageAccount.createCloudBlobClient(); container = blobClient.getContainerReference(containerName); container.createIfNotExists(BlobContainerPublicAccessType.CONTAINER, new BlobRequestOptions(), new OperationContext()); } catch (URISyntaxException | InvalidKeyException | StorageException e) { logger.error("1步:獲取要使用的容器 error from Azure server. ", e); } catch (Exception e) { logger.error("1步:獲取要使用的容器 error from Azure server. ", e); } return container; } /** * 上傳Blob * 建立 Blob或更新Blob(如果該 Blob 已存在) * * @param container 要上傳的容器 * @param filePath 檔案路徑 eg: "/home/edmond/Pictures/古天樂.jpg" * @return blob url eg: "https://testpmpd.blob.core.chinacloudapi.cn/pmpd-container/古天樂.jpg" */ public static String uploadBlob(CloudBlobContainer container, String filePath) { FileInputStream fileInputStream = null; CloudBlockBlob blob = null; try { File source = new File(filePath); blob = container.getBlockBlobReference(source.getName()); fileInputStream = new FileInputStream(source); blob.upload(fileInputStream, source.length()); } catch (URISyntaxException | StorageException | FileNotFoundException e) { logger.error("上傳Blob error from Azure server. ", e); } catch (IOException e) { logger.error("上傳Blob error from Azure server. ", e); } catch (Exception e) { logger.error("上傳Blob error from Azure server. ", e); } finally { try { if (fileInputStream != null) { fileInputStream.close(); } } catch (IOException e) { logger.error("上傳Blob error from Azure server. ", e); } } String returnValue = null; if (blob != null) { returnValue = blob.getUri().toString(); } return returnValue; } /** * 下載 Blob * * @param container 要下載的容器 * @param filePath 下載檔案要儲存的資料夾路徑 eg: "/home/edmond/Pictures/" * @param blobName 要下載的blobeg: "古天樂.jpg" */ public static void downloadBlob(CloudBlobContainer container, String filePath, String blobName) { FileOutputStream fileOutputStream = null; try { for (ListBlobItem blobItem : container.listBlobs()) { if (blobItem instanceof CloudBlob) { CloudBlob blob = (CloudBlob) blobItem; if (blob.getName().equals(blobName)) { fileOutputStream = new FileOutputStream(filePath + blob.getName()); blob.download(fileOutputStream); } } } } catch (FileNotFoundException | StorageException e) { logger.error("下載 Blob error from Azure server. ", e); } catch (IOException e) { logger.error("下載 Blob error from Azure server. ", e); } catch (Exception e) { logger.error("下載 Blob error from Azure server. ", e); } finally { try { if (fileOutputStream != null) { fileOutputStream.close(); } } catch (IOException e) { logger.error("下載 Blob error from Azure server. ", e); } } } /** * 刪除 Blob * * @param container 檔案所在的容器 * @param blobName 要刪除的檔名 eg: "古天樂.jpg" * @return true or false */ public static boolean deleteBlob(CloudBlobContainer container, String blobName) { boolean success = false; try { CloudBlockBlob blob = container.getBlockBlobReference(blobName); success = blob.deleteIfExists(); } catch (URISyntaxException | StorageException e) { logger.error("刪除 Blob error from Azure server. ", e); } catch (Exception e) { logger.error("刪除 Blob error from Azure server. ", e); } return success; } }