2017-05-23 27 views
0

我遇到了Autodesk僞造授權問題。偶爾我會撥打oss/v2/buckets/{key}/objects/{object}來收到401。 這只是偶爾發生,但值得一提的是,我已經能夠複製這一點的一種方式是,當試圖從兩個不同的客戶端併發上傳兩個相同的文件時。同時上傳文件時與autodesk僞造的授權問題

這種情況通常工作,或引用布萊恩Fantana - 它屢試不爽的時間

60%。

我該如何解決這個問題?一些指導將非常有幫助。

在此先感謝。

+0

此API調用需要多長時間處理文件?如果超過30分鐘,您使用的令牌應該在到期前刷新。 –

+0

謝謝你的回覆。文件大小爲155mb,並採取aprox。 2分鐘上傳。同時上傳時,Autodesk可能會有限制嗎? –

+0

您能否檢查Forge存儲桶中已上傳文件的總大小?根據我的經驗,使用api'oss/v2/buckets/{key}/objects/{object}'上傳大小超過20MB的文件將會失敗。 –

回答

1

很高興聽到您自己解決這個問題。雖然每次上傳刷新令牌現在都可以解決此問題,但建議使用buckets/:bucketKey/objects/:objectName/resumable以塊的形式上傳大文件。

對於大文件,建議將其分成幾個小部分,稱爲官方document中的塊,並由 buckets/:bucketKey/objects/:objectName/resumable API上傳。下面是the Forge C# SDK從我的同事此API的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); 
} 

希望這有助於。

+0

感謝您提供此解決方案。它的實施和似乎工作正常。我會添加這個作爲正確的答案。 –

0

要解決此問題,我必須更改標記的到期時間,以便在每次上傳時始終刷新標記。