RestSharp,Forge API-出现错误:文件上传时出现重叠范围

时间:2018-08-04 12:50:07

标签: c# autodesk-forge

我正在尝试使用伪造.NET SDK将文件上传到存储桶。它在大多数情况下都有效,但偶尔会出现{错误:范围重叠}。这是代码段。

private string uploadFileToBucket(Configuration configuration, string bucketKey, string filePath)
{
    ObjectsApi objectsApi = new ObjectsApi(configuration);
    string fileName = Path.GetFileName(filePath);
    string base64EncodedUrn, objectKey;
    using (FileStream fileStream = File.Open(filePath, FileMode.Open))
    {
        long contentLength = fileStream.Length;
        string content_range = "bytes 0-" + (contentLength - 1) + "/" + contentLength;
        dynamic result = objectsApi.UploadChunk(bucketKey, fileName, (int)fileStream.Length, content_range,
            "12313", fileStream);
        DynamicJsonResponse dynamicJsonResponse = (DynamicJsonResponse)result;
        JObject json = dynamicJsonResponse.ToJson();

        JToken urn = json.GetValue("objectId");
        string urnStr = urn.ToString();
        base64EncodedUrn = ApiClient.encodeToSafeBase64(urnStr);
        objectKey = fileName;
    }
    return base64EncodedUrn;
}

1 个答案:

答案 0 :(得分:0)

在上传之前,文件内容必须必须读入计算机内存,否则,代码段中的FileStream对象为空。

但是,如果您只想上载整个文件,我建议您使用PUT buckets/:bucketKey/objects/:objectName。这是我的测试代码。希望对您有帮助〜

    private static TwoLeggedApi oauth2TwoLegged;
    private static dynamic twoLeggedCredentials;
    private static Random random = new Random();

    public static string RandomString(int length)
    {
        const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        return new string(Enumerable.Repeat(chars, length)
          .Select(s => s[random.Next(s.Length)]).ToArray());
    }

     // Initialize the 2-legged OAuth 2.0 client, and optionally set specific scopes.
    private static void initializeOAuth()
    {
        // You must provide at least one valid scope
        Scope[] scopes = new Scope[] { Scope.DataRead, Scope.DataWrite, Scope.BucketCreate, Scope.BucketRead };

        oauth2TwoLegged = new TwoLeggedApi();
        twoLeggedCredentials = oauth2TwoLegged.Authenticate(FORGE_CLIENT_ID, FORGE_CLIENT_SECRET, oAuthConstants.CLIENT_CREDENTIALS, scopes);
        objectsApi.Configuration.AccessToken = twoLeggedCredentials.access_token;
    }

    private static void uploadFileToBucket(string bucketKey, string filePath)
    {
        Console.WriteLine("*****Start uploading file to the OSS");
        string path = filePath;

        //File Total size
        var info = new System.IO.FileInfo(path);
        long fileSize = info.Length;

        using (FileStream fileStream = File.Open(filePath, FileMode.Open))
        {
            string sessionId = RandomString(12);
            Console.WriteLine(string.Format("sessionId: {0}", sessionId));

            long contentLength = fileSize;
            string content_range = "bytes 0-" + (contentLength - 1) + "/" + contentLength;
            Console.WriteLine("Uploading range: " + content_range);


            byte[] buffer = new byte[contentLength];
            MemoryStream memoryStream = new MemoryStream(buffer);

            int nb = fileStream.Read(buffer, 0, (int)contentLength);
            memoryStream.Write(buffer, 0, nb);
            memoryStream.Position = 0;

            dynamic response = objectsApi.UploadChunk(bucketKey, info.Name, (int)contentLength, content_range,
                sessionId, memoryStream);

            Console.WriteLine(response);
        }
    }

    static void Main(string[] args)
    {
        initializeOAuth();
        uploadFileToBucket(BUCKET_KEY, FILE_PATH);
    }