通过Forge SDK恢复上传文件,UploadChunkWithHttpInfo失败

时间:2018-11-08 12:27:29

标签: autodesk-forge autodesk forge autodesk-data-management autodesk-bim360

我正在尝试使用此示例使用适用于.Net的SDK上载文件: https://forge.autodesk.com/blog/c-resumable-upload-file-forge-sdk

如果直接使用REST API,则可以创建存储桶并上传文件而不会出现问题,可以使用无块的直接上传。 如果使用SDK方法,我总是得到

Message: "An error has occurred."
ExceptionMessage: "Error calling UploadChunk: {"developerMessage":"ACM check failed, user or calling service does not have access to perform this operation","userMessage":"","errorCode":"AUTH-012","more info":"http://developer.api.autodesk.com/documentation/v1/errors/AUTH-012"}"
ExceptionType: "Autodesk.Forge.Client.ApiException"
StackTrace: "   at Autodesk.Forge.ObjectsApi.UploadChunkWithHttpInfo(String bucketKey, String objectName, Nullable`1 contentLength, String contentRange, String sessionId, Stream body, String contentDisposition, String ifMatch)

我已确认存储桶键相同。 我正在使用SDK的1.4.0版本,有一个我不希望使用的1.5.1 Alpha版本。

1 个答案:

答案 0 :(得分:0)

因此,我尝试使用HttpInfo选项使用this code,如下所示,并且运行良好。根据您的消息,正如@Bryan Huang指出的那样,该错误很可能与身份验证(或临时故障)有关

long chunkSize = 2 * 1024 * 1024; // 2 Mb
long numberOfChunks = (long)Math.Round((double)(fileSize / chunkSize)) + 1;

progressBar.Maximum = (int)numberOfChunks;

long start = 0;
chunkSize = (numberOfChunks > 1 ? chunkSize : fileSize);
long end = chunkSize;
string sessionId = Guid.NewGuid().ToString();

// upload one chunk at a time
using (BinaryReader reader = new BinaryReader(new FileStream(filePath, FileMode.Open)))
{
    for (int chunkIndex = 0; chunkIndex < numberOfChunks; chunkIndex++)
    {
        string range = string.Format("bytes {0}-{1}/{2}", start, end, fileSize);

        long numberOfBytes = chunkSize + 1;
        byte[] fileBytes = new byte[numberOfBytes];
        MemoryStream memoryStream = new MemoryStream(fileBytes);
        reader.BaseStream.Seek((int)start, SeekOrigin.Begin);
        int count = reader.Read(fileBytes, 0, (int)numberOfBytes);
        memoryStream.Write(fileBytes, 0, (int)numberOfBytes);
        memoryStream.Position = 0;

        dynamic chunkUploadResponse = await objects.UploadChunkAsyncWithHttpInfo(bucketKey, objectKey, (int)numberOfBytes, range, sessionId, memoryStream);

        start = end + 1;
        chunkSize = ((start + chunkSize > fileSize) ? fileSize - start - 1 : chunkSize);
        end = start + chunkSize;

        progressBar.CustomText = string.Format("{0} Mb uploaded...", (chunkIndex * chunkSize) / 1024 / 1024);
        progressBar.Value = chunkIndex;
    }
}