開発入門 > その他 > Azure CDNのコンテンツをPHPからRest API経由でパージする

Azure CDNのコンテンツをPHPからRest API経由でパージする

AzureをRest APIで操作してCDNにキャッシュされたコンテンツをPurgeする方法がうまくまとまったサイトがなかったので、関数を作ってみた。

まず、リソース作成者の取得方法(Rest API編)を参考に、各種キーを取得します。

一点上記のサイトと違うところは、今回はCDNを操作するため、IAMでアカウントのRoleにはCDN Profile Contributerを設定しておきましょう。

<?php


$cdnurl = "https://xxxxxxx.azureedge.net/Thumbnails/d7d7d572-c299-449a-a8af-5da785867310.png";

//パージ実行
purgeCdnURL($cdnurl, $cdn_purge_subscriptionId, $cdn_purge_resourceGroupName, $cdn_purge_profileName, $cdn_purge_directory_id, $cdn_purge_client_id, $cdn_purge_client_secret);


#設定情報
$cdn_purge_directory_id = "XXXXXX-fef8-46d0-9824-XXXXXXX";
$cdn_purge_client_id = "XXXXXX-XXXXX-456e-9836-XXXXXXXX";
$cdn_purge_client_secret = "XXXXXXXXXXXXXXXX1ZK78fuKC6mcmg=";

$cdn_purge_subscriptionId = "XXXXXX-XXXXX-XXXXX-XXXXX-4654b3f6ffe1";
$cdn_purge_resourceGroupName = "XXXXXXXXX";
$cdn_purge_profileName = "XXXXXXXXXXX";


$cdnurl = $_POST["cdnurl"];
if($cdnurl==""){print "cdnurlを設定してください";die();}

//CDNのURLを指定してパージする
function purgeCdnURL($CdnURL, $subscriptionId, $resourceGroupName, $profileName, $directory_id, $client_id, $client_secret){
	$contentPath = getContentPath($CdnURL);
	$endpointName = getEndpointName($CdnURL);
	$access_token = getBearerAccessToken($directory_id, $client_id, $client_secret);
	$ret = purgeCdn($subscriptionId, $resourceGroupName, $profileName, $endpointName, $contentPath, $access_token);
	if($ret == true){
		print "Purge succeeded.";
	}else{
		print "Purge failed.";
	}
}

//Authorization: Bearer用のAccess Token取得
//参考:https://tech-lab.sios.jp/archives/7239
function getBearerAccessToken($directory_id, $client_id, $client_secret){
	$ch = curl_init();
	curl_setopt_array($ch, [
	    CURLOPT_URL => 'https://login.windows.net/' . $directory_id . '/oauth2/token?api-version=1.0',
	    CURLOPT_RETURNTRANSFER => true,
	    CURLOPT_POST => true,
	    CURLOPT_POSTFIELDS => http_build_query(
	    	array(
		    'grant_type' => 'client_credentials',
		    'resource' => 'https://management.azure.com/',
		    'client_id' => $client_id,
		    'client_secret' => $client_secret,
	    	)
	    ),
	]);
	$response = curl_exec($ch);
	curl_close($ch);
	$response_decoded = json_decode($response);
	$access_token = @$response_decoded->{"access_token"};
	if($access_token==""){
		print_r("Error at getBearerAccessToken():" . $response);die();
	}
	return $access_token;
}


#CDNコンテンツをパージ
#参考:https://docs.microsoft.com/en-us/rest/api/cdn/endpoints/endpoints_purgecontent
function purgeCdn($subscriptionId, $resourceGroupName, $profileName, $endpointName, $contentPath, $access_token){
	
	$url = "https://management.azure.com/subscriptions/{$subscriptionId}/resourceGroups/{$resourceGroupName}/providers/Microsoft.Cdn/profiles/{$profileName}/endpoints/{$endpointName}/purge?api-version=2017-10-12";
	$ch = curl_init();
	
	curl_setopt($ch, CURLOPT_HTTPHEADER, 
		array(
			"Content-Type: application/json",
			"Authorization: Bearer " . $access_token
		)
	);
	curl_setopt_array($ch, [
	    CURLOPT_URL => $url,
	    CURLOPT_RETURNTRANSFER => true,
	    CURLOPT_POST => true,
	    CURLOPT_POSTFIELDS => '{contentPaths: ["' . $contentPath . '"]}',
	    
	]);
	$response = curl_exec($ch);
	curl_close($ch);
	
	if($response!=""){
		print_r("Error at purgeCdn():" . $response);die();
	}
	return true;
}


function getContentPath($cdnurl){
	preg_match('/^(http|https):\/\/(.*).azureedge.net/',$cdnurl,$matched);
	$tmp = @$matched[0];
	if($tmp==""){print "Error: This is not CDN URL";die();}
	return str_replace($tmp, "", $cdnurl);
}

function getEndpointName($cdnurl){
	preg_match('/^(http|https):\/\/(.*).azureedge.net/',$cdnurl,$matched);
	if(count($matched)==0){print "Error: This is not CDN URL";die();}
	return $matched[2];
}



?>

 

 

 

 

カテゴリー: その他

HOME / Coporate Site/ Careers

© Copyright 2018 STYLY..