【问题标题】:RestSharp, Forge API - Getting error:overlapping ranges on file uploadRestSharp,Forge API - 出现错误:文件上传时范围重叠
【发布时间】:2019-01-12 02:55:58
【问题描述】:

我正在尝试使用 forge .NET SDK 将文件上传到存储桶。它大部分时间都有效,但偶尔会给出{错误:重叠范围}。这是代码sn-p。

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;
}

【问题讨论】:

标签: c# autodesk-forge


【解决方案1】:

在上传之前,文件内容必须要读到计算机内存中,否则你的代码sn-p中的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);
    }

【讨论】:

    猜你喜欢
    • 2020-10-15
    • 1970-01-01
    • 2018-10-13
    • 2020-11-20
    • 2021-06-06
    • 1970-01-01
    • 1970-01-01
    • 2017-01-12
    • 2016-09-28
    相关资源
    最近更新 更多