question

Roshaan avatar image
Roshaan asked

How to download content from cdn?

Hi, I have acquired content url using GetContentDownloadUrl but when I try to download using UnityWebRequest Get method it gives an error "Http 403"

private IEnumerator WaitForFileDownloading(string URL)

        {

            if (string.IsNullOrEmpty(URL))

                yield break;



            using (UnityWebRequest unityWebRequest = UnityWebRequest.Get(URL))

            {

                unityWebRequest.method = UnityWebRequest.kHttpVerbGET;



                StartCoroutine(DownloadingBar(unityWebRequest));

                yield return unityWebRequest.SendWebRequest();



                if (unityWebRequest.isHttpError || unityWebRequest.isNetworkError)

                    Debug.LogError("Error: " + unityWebRequest.error);



                else

                {

                    if (unityWebRequest.isDone)

                    {

                        Debug.Log("Successfully downloaded book from PlayFab");



                        byte[] dataInBytes = unityWebRequest.downloadHandler.data;

                        File.WriteAllBytes(filePath, dataInBytes);

                    }

                }

            }

        }
apisunity3dsdksContent
8 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Seth Du avatar image Seth Du ♦ commented ·

May I ask how this error occurs? I assume that you have uploaded files in the File Management page of Game manager, and then the GetContentDownloadUrl API call has been used to retrieve an URL. The callback result is successful ,but later when URL is used to download files, you get 403 forbidden error?

0 Likes 0 ·
Roshaan avatar image Roshaan Seth Du ♦ commented ·

Yes you are correct. That's how I get an error

0 Likes 0 ·
Seth Du avatar image Seth Du ♦ Roshaan commented ·

I think there is a high possibility that the URL goes wrong. GetContentDownloadUrl simply returns a URL that contains a token with an expiration date, along with the directory(which is the key in the request) even though the file doesn't exist. So you may double check the file path in the game manager. You can share the request of GetContentDownloadUrl and the file management page of your game manager(Please hide sensitive information)

0 Likes 0 ·
Show more comments
Seth Du avatar image Seth Du ♦ commented ·

Hi, I have changed my previous comment. Please check it again. Your code seems good.

0 Likes 0 ·
Roshaan avatar image Roshaan Seth Du ♦ commented ·

Can you please tell me how can I know the key of a file in my directory on Game Manager.

/Root/Books/KJV_JSON.json

This is how my Game Manager directory looks like. What is the Key here for this KJV book?
Because GetContentDownloadUrl requires a key too in it's parameter. Right now my key is

Key = "KJV_JSON"

below is my complete code

internal void DownloadBook()
{
            PlayFab.ClientModels.GetContentDownloadUrlRequest contentDownloadUrlRequest = new PlayFab.ClientModels.GetContentDownloadUrlRequest
	{Key = "KJV_JSON",ThruCDN = true};


	PlayFabClientAPI.GetContentDownloadUrl(contentDownloadUrlRequest, OnSuccessfulResponse, OnFailedResponse);
}
0 Likes 0 ·
Seth Du avatar image Seth Du ♦ Roshaan commented ·

you may try

Key = "Books/KJV_JSON.json"
0 Likes 0 ·
Show more comments

1 Answer

·
Roshaan avatar image
Roshaan answered

@SethDu so the issue was wrong key format which I was following while requesting playfab file management. The format I'm following now and is working:

Key = "Books/KJV_JSON.json"

// "Books/" is folder name under Root directory
// KJV_JSON is file name
//.json is file extension
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.