Search code examples
azureazure-blob-storageazure-cdnazure-container-service

Create azure cdn endpoint for azure container


I need to create Azure CDN Endpoint for Azure Container. I am using below code to do so.

 Endpoint endpoint = new Endpoint() {
                IsHttpAllowed = true,
                IsHttpsAllowed = true,
                Location = this.config.ResourceLocation,
                Origins = new List<DeepCreatedOrigin> { new DeepCreatedOrigin(containerName, string.Format(STORAGE_URL, storageAccountName)) },
                OriginPath = "/" + containerName,                    
            };
            await this.cdnManagementClient.Endpoints.CreateAsync(this.config.ResourceGroupName, storageAccountName, containerName, endpoint);

All the information I provide is correct and Endpoint is getting created successfully. But when I try to access any blob inside it. It is giving an InvalidUrl error.

However the weird thing is If I create the same endpoint using same values through portal, I am able to access and download blobs.

Anyone please let me know what am I doing wrong in my code? Do I need to pass any extra parameters?


Solution

  • As far as I know, if you want to create a storage CDN in code, you need set the OriginHostHeader value as your storage account URL.

    More details, you could refer to below codes:

       // Create CDN client
                CdnManagementClient cdn = new CdnManagementClient(new TokenCredentials(token))
                { SubscriptionId = subscriptionId };
                //ListProfilesAndEndpoints(cdn);
                Endpoint e1 = new Endpoint()
                {
                   // OptimizationType = "storage",
                    Origins = new List<DeepCreatedOrigin>() { new DeepCreatedOrigin("{yourstoragename}-blob-core-windows-net", "{yourstoragename}.blob.core.windows.net") },
                    OriginHostHeader = "{yourstoragename}.blob.core.windows.net",
                    IsHttpAllowed = true,
                    IsHttpsAllowed = true,
                    OriginPath=@"/foo2",
                    Location = "EastAsia"
            };
    
            cdn.Endpoints.Create(resourcegroup, profilename, enpointname, e1);
    

    Besides, I suggest you could generate SAS token to directly access the blob file by URL.