同时上传文件时 autodesk forge 的授权问题

Authorization issues with autodesk forge when uploading files concurrently

我在使用 Autodesk Forge 授权时遇到问题。偶尔我在调用 oss/v2/buckets/{key}/objects/{object} 时会收到 401。 这种情况很少发生,但值得一提的是,我能够复制这种情况的一种方法是尝试从两个不同的客户端同时上传两个相同的文件。

这种情况通常有效,或者引用 Brian Fantana -

60% of the time it works every time.

如何解决这个问题?一些指导会很有帮助。

提前致谢。

为了解决这个问题,我不得不更改令牌的到期时间,以便在每次上传时始终刷新。

很高兴听到你自己解决了这个问题。虽然每次上传时刷新令牌现在可以解决此问题,但建议使用 buckets/:bucketKey/objects/:objectName/resumable 分块上传大文件。

对于大文件,建议分成几个小部分在官方document中称为chunks,由 buckets/:bucketKey/objects/:objectName/resumable API。这是来自我同事的此 API 的 the Forge C# SDK 的 C# 示例:

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);
}

希望对您有所帮助。