0

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);
    }
Aleksandar
  • 13
  • 2

2 Answers2

0
  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.
Paxton.Huynh
  • 172
  • 10
  • Yes, I get that, but I have a problem because that is a PUT request and I'm only sending bucketKey and objectName(filename). So how can I get to the file via just fileName and upload it to my server? Thank you for answering. – Aleksandar Nov 18 '21 at 08:20
0

The code you pasted from this article is more about uploading from a server when the file is already stored there - either for caching purposes or the server is using/modifying those files.

As Paxton.Huynh said, FILE_PATH there contains the location on the server where the file is stored.

If you just want to upload the chunks to Forge through your server (to keep credentials and internal access token secret), like a proxy, then it's probably better to just pass on those chunks to Forge instead of storing the file on the server first and then passing it on to Forge - what the sample code you referred to is doing.

See e.g. this, though it's in NodeJS: https://github.com/Autodesk-Forge/forge-buckets-tools/blob/master/server/data.management.js#L171

Adam Nagy
  • 1,700
  • 1
  • 9
  • 14
  • Thank you Adam for answering and for a sample code. This was helpful because I was stuck between that two methods of uploading... I think I'm going to try to upload chunk by chunk directly to Forge instead of storing the file on the server first. – Aleksandar Nov 19 '21 at 15:53
  • Sorry to bother you Adam, but I have just one more question. Does Forge team have a plan to maybe, in the near future, implement these code samples (https://forge.autodesk.com/en/docs/viewer/v2/tutorials/basic-viewer/) in Angular and React frameworks, or maybe to introduce some wrapper components (libs) for them? Do you guys use only jQuery for frontend side on your internal projects with Forge or? – Aleksandar Nov 22 '21 at 12:41
  • It's better to look at v7 resources rather than v2: https://forge.autodesk.com/en/docs/viewer/v7/developers_guide/overview/ I don't see jQuery in those samples - did you mean the samples here https://learnforge.autodesk.io/#/ We are actually planning to make that fully vanilla JS so that no matter what you use it should be easy to add it to your project. One partner created an Angular package: https://github.com/theNBS/ng2-adsk-forge-viewer We have a few posts on React (and TypeScript as well), e.g. https://forge.autodesk.com/blog/how-use-react-forge-viewer-dockingpanel – Adam Nagy Nov 22 '21 at 20:04