從Azure儲存體擷取資料

本文說明如何以Azure儲存空間中儲存的資料填入最適化表單。
假設您已將最適化表單資料儲存在Azure儲存體,且現在想要將該資料預先填入最適化表單。

NOTE
本文中的程式碼無法用於以核心元件為基礎的最適化表單。此處提供核心元件型最適化表單的同等文章

建立GET請求

下一步是撰寫程式碼,以使用blobID從Azure儲存體擷取資料。 已寫入下列程式碼以擷取資料。 此URL是使用OSGi設定中的sasToken和storageURI值以及傳遞至getBlobData函式的blobID所建構

 @Override
public String getBlobData(String blobID) {
    String sasToken = azurePortalConfigurationService.getSASToken();
    String storageURI = azurePortalConfigurationService.getStorageURI();
    log.debug("The SAS Token is " + sasToken);
    log.debug("The Storage URL is " + storageURI);
    String httpGetURL = storageURI + blobID;
    httpGetURL = httpGetURL + sasToken;
    HttpGet httpGet = new HttpGet(httpGetURL);

    org.apache.http.impl.client.CloseableHttpClient httpClient = HttpClientBuilder.create().build();
    CloseableHttpResponse httpResponse = null;
    try {
        httpResponse = httpClient.execute(httpGet);
        HttpEntity httpEntity = httpResponse.getEntity();
        String blobData = EntityUtils.toString(httpEntity);
        log.debug("The blob data I got was " + blobData);
        return blobData;

    } catch (ClientProtocolException e) {

        log.debug("Got Client Protocol Exception " + e.getMessage());
    } catch (IOException e) {

        log.debug("Got IOEXception " + e.getMessage());
    }

    return null;
}

當最適化表單在URL中以guid引數轉譯時,與範本相關聯的自訂頁面元件會擷取並填入最適化表單中來自Azure儲存體的資料。
以下是和範本相關之頁面元件的jsp中的程式碼

com.aemforms.saveandfetchfromazure.StoreAndFetchDataFromAzureStorage azureStorage = sling.getService(com.aemforms.saveandfetchfromazure.StoreAndFetchDataFromAzureStorage.class);


String guid = request.getParameter("guid");

if(guid!=null&&!guid.isEmpty())
{
    String dataXml = azureStorage.getBlobData(guid);
    slingRequest.setAttribute("data",dataXml);

}

測試解決方案

recommendation-more-help
8de24117-1378-413c-a581-01e660b7163e