Share via


將檔案上傳至 Azure Blob 儲存體

本教學課程說明如何從 Spring Boot 應用程式上傳和讀取 Azure Blob 儲存體 帳戶中的容器 Blob。

Azure Blob 儲存體 是 Microsoft 的雲端物件儲存解決方案。 Blob 記憶體已針對儲存大量的非結構化數據進行優化,例如文字或二進位數據。

必要條件

  • Azure 訂用帳戶 - 建立免費帳戶
  • Java Development Kit (JDK) 第 8 版或更高版本。
  • Apache Maven 3.0 版或更高版本。
  • cURL 或類似的 HTTP 公用程式來測試功能。
  • Azure 記憶體帳戶和容器。 如果您沒有記憶體帳戶, 請建立記憶體帳戶
  • Spring Boot 應用程式。 如果您沒有這個應用程式,請使用 Spring Initializr 來建立 Maven 專案。 請務必選取 [Maven專案],然後在 [相依性] 下新增 Spring Web 相依性,然後選取 Java 版本 8 或更高版本。

注意

若要授與帳戶對資源的存取權,請在新建立的 Azure 儲存體 帳戶中,將角色指派Storage Blob Data Contributor給您目前使用的 Microsoft Entra 帳戶。 如需詳細資訊,請參閱使用 Azure 入口網站指派 Azure 角色

重要

需要 Spring Boot 2.5 版或更高版本,才能完成本教學課程中的步驟。

建立容器

首先,依照快速入門中的指示建立名為 testcontainer 的容器:使用 Azure 入口網站 上傳、下載及列出 Blob。

從 Azure 儲存體 帳戶容器上傳和讀取 Blob

現在您已擁有 Azure 儲存體 帳戶和容器,您可以使用 Spring Cloud Azure 從 Blob 上傳和讀取檔案。

若要安裝 Spring Cloud Azure 儲存體 Blob 入門模組,請將下列相依性新增至您的 pom.xml 檔案:

  • Spring Cloud Azure 材料帳單(BOM):

    <dependencyManagement>
      <dependencies>
        <dependency>
          <groupId>com.azure.spring</groupId>
          <artifactId>spring-cloud-azure-dependencies</artifactId>
          <version>5.12.0</version>
          <type>pom</type>
          <scope>import</scope>
        </dependency>
      </dependencies>
    </dependencyManagement>
    

    注意

    如果您使用 Spring Boot 2.x,請務必將 spring-cloud-azure-dependencies 版本設定為 4.18.0。 此材料帳單 (BOM) 應該在<dependencyManagement>pom.xml檔案的 區段中設定。 這可確保所有 Spring Cloud Azure 相依性都使用相同的版本。 如需此 BOM 所用版本的詳細資訊,請參閱 應該使用哪個版本的 Spring Cloud Azure。

  • Spring Cloud Azure 儲存體 Blob 入門成品:

    <dependency>
      <groupId>com.azure.spring</groupId>
      <artifactId>spring-cloud-azure-starter-storage-blob</artifactId>
    </dependency>
    

編碼應用程式

若要使用 Spring Cloud Azure 儲存體 Blob 入門版從 Blob 上傳和讀取檔案,請使用下列步驟來設定應用程式。

  1. 在 application.properties 組態檔中設定 儲存體 帳戶名稱和端點,如下列範例所示。

    spring.cloud.azure.storage.blob.account-name=${AZURE_STORAGE_ACCOUNT_NAME}
    spring.cloud.azure.storage.blob.endpoint=${AZURE_STORAGE_ACCOUNT_ENDPOINT}
    
  2. 建立新的 BlobController Java 類別,如下列範例所示。 這個類別可用來從 Azure 儲存體 帳戶中的容器 Blob 上傳和讀取檔案。

    package com.example.demo;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.core.io.Resource;
    import org.springframework.core.io.WritableResource;
    import org.springframework.util.StreamUtils;
    import org.springframework.web.bind.annotation.*;
    
    import java.io.IOException;
    import java.io.OutputStream;
    import java.nio.charset.Charset;
    
    @RestController
    @RequestMapping("blob")
    public class BlobController {
    
        @Value("azure-blob://testcontainer/test.txt")
        private Resource blobFile;
    
        @GetMapping("/readBlobFile")
        public String readBlobFile() throws IOException {
            return StreamUtils.copyToString(
                    this.blobFile.getInputStream(),
                    Charset.defaultCharset());
        }
    
        @PostMapping("/writeBlobFile")
        public String writeBlobFile(@RequestBody String data) throws IOException {
            try (OutputStream os = ((WritableResource) this.blobFile).getOutputStream()) {
                os.write(data.getBytes());
            }
            return "file was updated";
        }
    }
    

    提示

    在本教學課程中,組態或程式代碼中沒有任何驗證作業。 不過,連線到 Azure 服務需要驗證。 若要完成驗證,您需要使用 Azure Identity。 Spring Cloud Azure 使用 DefaultAzureCredential,Azure 身分識別連結庫會提供它來協助您取得認證,而不需要變更任何程序代碼。

    DefaultAzureCredential 支援多種驗證方法,並在執行階段判斷應使用的方法。 這種方法可讓您的應用程式在不同的環境中使用不同的驗證方法(例如本機和生產環境),而不需要實作環境特定的程序代碼。 如需詳細資訊,請參閱 DefaultAzureCredential

    若要在本機開發環境中完成驗證,您可以使用 Azure CLI、Visual Studio Code、PowerShell 或其他方法。 如需詳細資訊,請參閱 Java 開發環境中的 Azure 驗證。 若要在 Azure 裝載環境中完成驗證,建議您使用使用者指派的受控識別。 如需詳細資訊,請參閱什麼是 Azure 資源受控識別?

  3. 執行應用程式之後,請 curl 遵循下列步驟來測試您的應用程式。

    1. 使用下列命令傳送 POST 要求以更新檔案的內容:

      curl http://localhost:8080/blob/writeBlobFile -d "new message" -H "Content-Type: text/plain"
      

      您應該會看到一個回應,指出 file was updated

    2. 使用下列命令傳送 GET 要求來驗證檔案的內容:

      curl -X GET http://localhost:8080/blob/readBlobFile
      

      您應該會看到您張貼的「新訊息」文字。

部署至 Azure Spring Apps

現在您已在本機執行 Spring Boot 應用程式,現在可以將其移至生產環境。 Azure Spring Apps 可讓您輕鬆地將 Spring Boot 應用程式部署至 Azure,而不需要變更任何程式代碼。 服務會管理 Spring 應用程式的基礎結構,讓開發人員可以專注於處理程式碼。 Azure Spring 應用程式提供生命週期管理,使用全方位的監視和診斷、組態管理、服務探索、持續整合與持續傳遞的整合、藍綠部署等等。 若要將應用程式部署至 Azure Spring Apps,請參閱 將第一個應用程式部署至 Azure Spring Apps

下一步

若要深入了解 Spring 和 Azure,請繼續閱讀「Azure 上的 Spring」文件中心中的資訊。

另請參閱

如需適用於 Microsoft Azure 的其他 Spring Boot Starters 詳細資訊,請參閱 什麼是 Spring Cloud Azure?

如需您可以從 Spring Boot 應用程式呼叫的其他 Azure 記憶體 API 詳細資訊,請參閱下列文章: