0

I'm having trouble with the autodesk forge authorization. Occasionally I receive a 401 when calling oss/v2/buckets/{key}/objects/{object}. This only occurs infrequently, but worth mentioning is that one way I've been able to replicate this was when trying to upload two identical files concurrently from two different clients.

This scenario usually works, or to quote Brian Fantana -

60% of the time it works every time.

How do I solve this issue? Some guidance would be very helpful.

Thanks in advance.

6
  • How much time does this API call take for your files? If it takes more than 30 minutes, token you used should be refreshed before expiring. Commented May 24, 2017 at 2:19
  • Thanks for you reply. The file size was 155mb and took aprox. 2 minutes to upload. Could there be a limit on Autodesk's end when uploading concurrently? Commented May 24, 2017 at 7:38
  • Could you check the total size of the uploaded files in your Forge bucket for me? According to my experience, it is going to be failed to upload file with size larger than 20MB with the api oss/v2/buckets/{key}/objects/{object}. Commented May 24, 2017 at 8:20
  • An additional remind, just check size of a single file caused this issue. It's my fault, wrong typing... Commented May 24, 2017 at 8:30
  • Thanks for your help. On a side note - could it be that the viewer only supports some versions of Revit? Commented May 29, 2017 at 12:30

2 Answers 2

1

It's good to hear that you solve this issue by yourself. Although refreshing token on every upload could resolve this issue now, it's recommended to use buckets/:bucketKey/objects/:objectName/resumable to upload large files in chunks.

For large files, it's recommended to be separated into several small parts called the chunks in the official document, and uploaded by the buckets/:bucketKey/objects/:objectName/resumable API. Here is a C# sample of the Forge C# SDK for this API from my colleague:

private static dynamic resumableUploadFile()
{
  Console.WriteLine("*****Start uploading file to the OSS");
  string path = FILE_PATH;
  if (!File.Exists(path))
       path = @"..\..\..\" + FILE_PATH;

  //File Total size        
  long fileSize = new System.IO.FileInfo(path).Length;
  //Chunk size for separting file into several parts.
  //2MB chuck size is used in this sample.
  long chunkSize = 2 * 1024 * 1024 ;
  //Total amounts of chunks in 2MB size.
  long nbChunks = (long)Math.Round(0.5 + (double)fileSize / (double)chunkSize);

  ApiResponse<dynamic> finalRes = null ;
  using (FileStream streamReader = new FileStream(path, FileMode.Open))
  {
    //Unique id for resumable uploading.
    string sessionId = RandomString(12);
    for (int i = 0; i < nbChunks; i++)
    {
        //Start position in bytes of a chunk
        long start = i * chunkSize;
        //End position in bytes of a chunk
        //(End posistion of the latest chuck is the total file size in bytes)
        long end = Math.Min(fileSize, (i + 1) * chunkSize) - 1;

        //Identify chunk info. to the Forge
        string range = "bytes " + start + "-" + end + "/" + fileSize;
        //Steam size for this chunk
        long length = end - start + 1;

        Console.WriteLine("Uploading range: " + range);

        //Read content stream into a meomery stream for this chunk
        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 file to the Forge OSS Bucket
        ApiResponse<dynamic> response = objectsApi.UploadChunk(
                                            BUCKET_KEY,
                                            FILE_NAME,
                                            (int)length,
                                            range,
                                            sessionId,
                                            memoryStream
                                        );

        finalRes = response;

        if (response.StatusCode == 202) {
            Console.WriteLine("One chunk uploaded successfully");
            continue;
        }
        else if (response.StatusCode == 200)
        {
            Console.WriteLine("Final chunk uploaded successfully");
        }
        else
        {
            //Some error occurred here
            Console.WriteLine(response.StatusCode);
            break;
        } 
    } 

  }

  return (finalRes);
}

Hope this help.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for providing this solution. It's implemented and seems to be working fine. I'll add this as the correct answer.
0

To solve this issue I had to change the expiration time for the token to always be refreshed on every upload.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.