我遇到了autodesk forge授权问题。有时我会在致电oss/v2/buckets/{key}/objects/{object}
时收到401。
这种情况很少发生,但值得一提的是我能够复制的一种方法是尝试从两个不同的客户端同时上传两个相同的文件。
此方案通常有效,或引用 Brian Fantana -
每次都有60%的时间。
如何解决此问题?一些指导会非常有用。
提前致谢。
答案 0 :(得分:1)
很高兴听到您自己解决这个问题。虽然每次上传时刷新令牌现在都可以解决此问题,但建议您使用buckets/:bucketKey/objects/:objectName/resumable
以块的形式上传大型文件。
对于大型文件,建议将其分成几个小块,称为官方document中的块,并由
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);
}
希望得到这个帮助。
答案 1 :(得分:0)
要解决此问题,我必须更改令牌的到期时间,以便在每次上传时始终刷新。