Search code examples
autodesk-forge

Forge chunk upload .NET Core


I have question about uploading large objects in forge bucket. I know that I need to use /resumable api, but how can I get the file( when I have only filename). In this code what is exactly FILE_PATH? Generally, should I save file on server first and then do the upload on bucket?

private static dynamic resumableUploadFile()
{
       Console.WriteLine("*****begin uploading large file");
        string path = FILE_PATH;
        if (!File.Exists(path))`enter code here`
            path = @"..\..\..\" + FILE_PATH;

        //total size of file        
        long fileSize = new System.IO.FileInfo(path).Length;
        //size of piece, say 2M    
        long chunkSize = 2 * 1024 * 1024 ;
        //pieces count
        long nbChunks = (long)Math.Round(0.5 + (double)fileSize / (double)chunkSize);
        //record a global response for next function. 
        ApiResponse<dynamic> finalRes = null ;
        using (FileStream streamReader = new FileStream(path, FileMode.Open))
        {
            //unique id of this session
            string sessionId = RandomString(12);
            for (int i = 0; i < nbChunks; i++)
            {
                //start binary position of one certain piece 
                long start = i * chunkSize;
                //end binary position of one certain piece 
                //if the size of last piece is bigger than  total size of the file, end binary 
                // position will be the end binary position of the file 
                long end = Math.Min(fileSize, (i + 1) * chunkSize) - 1;

                //tell Forge about the info of this piece
                string range = "bytes " + start + "-" + end + "/" + fileSize;
                // length of this piece
                long length = end - start + 1; 

                //read the file stream of this piece
                byte[] buffer = new byte[length];
                MemoryStream memoryStream = new MemoryStream(buffer);

                int nb = streamReader.Read(buffer, 0, (int)length);
                memoryStream.Write(buffer, 0, nb);
                memoryStream.Position = 0;

                //upload the piece to Forge bucket
                ApiResponse<dynamic> response = objectsApi.UploadChunkWithHttpInfo(BUCKET_KEY,
                        FILE_NAME, (int)length, range, sessionId, memoryStream,
                        "application/octet-stream"); 

                finalRes = response;

                if (response.StatusCode == 202){
                    Console.WriteLine("one certain piece has been uploaded");
                    continue;
                }
                else if(response.StatusCode == 200){
                    Console.WriteLine("the last piece has been uploaded");
                }
                else{
                    //any error
                    Console.WriteLine(response.StatusCode);
                    break;

                } 
            } 
                   
        }
                        
        return (finalRes);
    }

Solution

    1. FILE_PATH: is the path where you stored file on your server.
    2. You should upload your file to server first. Why? Because when you upload your file to Autodesk Forge Server you need internal token, which should be kept secret (that why you keep it in your server), you dont want someone take that token and mess up your Forge Account.