question

dray avatar image
dray asked

How do I upload my server.zip using C#?

I'm trying to automate uploading my server to PlayFab as part of a C# program. I successfully call "GetAssetUploadUrlAsync" and get a URL. Then I use the following code to try and upload the zip file:

                    try
                    {
                        string url = GetURLTask.Result.Result.AssetUploadUrl;


                        var wwwRequest = (HttpWebRequest)WebRequest.Create(url);
                        wwwRequest.Method = "PUT";
                        wwwRequest.ContentType = "application/x-zip-compressed";
                        wwwRequest.Headers.Add("x-ms-blob-content-type: application/zip");
                        wwwRequest.Headers.Add("x-ms-blob-type: BlockBlob");
                        wwwRequest.ContentLength = contents.Length;
                        wwwRequest.Timeout = 1000000;
                        Stream dataStream = wwwRequest.GetRequestStream();


                        dataStream.Write(contents, 0, contents.Length);


                        dataStream.Close();
                        HttpWebResponse wwwResponse = (HttpWebResponse)wwwRequest.GetResponse();


                        if (wwwResponse.StatusCode == HttpStatusCode.Created || wwwResponse.StatusCode == HttpStatusCode.OK)
                        {
                            Console.WriteLine("...HTTP PUT Successful");
                        }
                        else
                        {
                            Console.WriteLine(string.Format("ERROR: [{0}] -- {1}", wwwResponse.StatusCode, wwwResponse.StatusDescription));
                        }
                    }
                    catch (Exception err)
                    {
                        System.Console.WriteLine(err.Message);
                        if (err.InnerException != null)
                        {
                            System.Console.WriteLine(err.InnerException.Message);
                        }
                    }

If I upload a small zip file, this works fine. But if I actually upload our server which is a little over 1GB, I get:

The remote server returned an error: (413) The request body is too large and exceeds the maximum permissible limit..

I can upload this same zip file either using the web interface or the PowerShell interface. What do I need to do to upload this file in C#?

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 answered

After the discussion and researching, our team have figured out a workaround solution for you. Basically using the URL returned from PlayFab GetAssetUploadUrl API will be like using Azure put blob method to submit a blob file, which has a limit of 256MB for single file. Meanwhile, if you can implement the Azure SDK on your project, the upload process can be handled well. Please note that you will need to install the v11 version of NuGet library on: https://www.nuget.org/packages/Microsoft.Azure.Storage.Blob/

Then refer to the following code:

var uri = ""; // the URL returned from GetAssetUploadUrl 
var blockBlob = new CloudBlockBlob(uri);
var file = @"D:\xxx\xxxx.zip";  //your file location
await blockBlob.UploadFromFileAsync(file);
3 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.

dray avatar image dray commented ·

That worked for me, thank you.

0 Likes 0 ·
Bob Sabiston avatar image Bob Sabiston dray commented ·

Are you still using this? I am trying to do the same thing. My build seems to be uploading OK. But it doesn't appear in the server list. I'm wondering what else needs to be done to upload a server? For example, do I need to set the Metadata in the CustomTags field of GetAssetUploadUrlRequest?

0 Likes 0 ·
Seth Du avatar image Seth Du ♦ Bob Sabiston commented ·

Is this "server list" or "assets list"? To be clear, this process only uploads the assets to PlayFab, while you will need to create a new build and select this zip file in "Assets" field.

0 Likes 0 ·
Seth Du avatar image
Seth Du answered

The maximum limit exists for the current implementation. Files larger than 256MB is not supported.

You will need to implement you own workaround via put blob list and divided the .zip into pieces and upload separately. Please refer to: https://stackoverflow.com/questions/58724878/upload-large-files-1-gb-to-azure-blob-storage-through-web-api.

We will also try to realize this workaround and it will take some time for us to do this work.

Please also feel free to send a thread on the feature request forum.

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.