0

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

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

60% 的时间它每次都能正常工作。

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

提前致谢。

4

2 回答 2

1

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

对于大文件,建议在官方文档中分成若干个小部分,称为 chunks,通过buckets/:bucketKey/objects/:objectName/resumableAPI上传 。这是我的同事为这个 API 提供的 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);
}

希望这有帮助。

于 2017-05-30T05:53:19.920 回答
0

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

于 2017-05-29T12:29:32.430 回答