0

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?

1 Answers1

1

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.

Brando Zhang
  • 22,586
  • 6
  • 37
  • 65
  • Hey Brando, Thanks a lot man. Seems like my issue got resolved after I pass an additional parameter OriginAuthHeader. I am marking this as an answer. Although, Can you please give me a favor? can you please provide me source of this information that where they have mentioned that this parameter is must be while creating Endpoint? Thanks again. – kartik ghodasara Apr 19 '17 at 09:02
  • I'm so sorry I couldn't give you the right information. I write this parameter according to the portal's add enpoit dialog. It needs the OriginHostHeader parameter, so I test it on my side. – Brando Zhang Apr 19 '17 at 09:14