【问题标题】:Read big files and chunk upload with Dropbox Api in c#在 c# 中使用 Dropbox Api 读取大文件和块上传
【发布时间】:2016-05-21 10:46:45
【问题描述】:

我有大量的 sql 备份,我想将它们保存在保管箱中,但我只想将副本发送到保管箱并将文件移动到外部硬盘,因为我的服务器硬盘空间。

我正在尝试使用 dropbox api 的块上传,这是他们提供的示例代码。

private async Task ChunkUpload(DropboxClient client, string folder, string fileName)
    {
        Console.WriteLine("Chunk upload file...");
        // Chunk size is 128KB.
        const int chunkSize = 128 * 1024;

        // Create a random file of 1MB in size.
        var fileContent = new byte[1024 * 1024];        
        new Random().NextBytes(fileContent);

        using (var stream = new MemoryStream(fileContent))
        {
            int numChunks = (int)Math.Ceiling((double)stream.Length / chunkSize);

            byte[] buffer = new byte[chunkSize];
            string sessionId = null;

            for (var idx = 0; idx < numChunks; idx++)
            {
                Console.WriteLine("Start uploading chunk {0}", idx);
                var byteRead = stream.Read(buffer, 0, chunkSize);

                using (MemoryStream memStream = new MemoryStream(buffer, 0, byteRead))
                {
                    if (idx == 0)
                    {
                        var result = await client.Files.UploadSessionStartAsync(memStream);
                        sessionId = result.SessionId;
                    }

                    else
                    {
                        UploadSessionCursor cursor = new UploadSessionCursor(sessionId, (ulong)(chunkSize * idx));

                        if (idx == numChunks - 1)
                        {
                            await client.Files.UploadSessionFinishAsync(cursor, new CommitInfo(folder + "/" + fileName), memStream);
                        }

                        else
                        {
                            await client.Files.UploadSessionAppendAsync(cursor, memStream);
                        }
                    }
                }
            }
        }
    }

我有文件路径,我想分块上传我的大文件,但无法让这个示例代码与发送文件路径一起工作。我对块读取的修改不起作用,我在谷歌上搜索了 2 周。最后我想问一下这个。

我怎样才能使这个方法与发送文件路径一起工作,并从那个大文件上传一个块?

从现在开始谢谢。

【问题讨论】:

    标签: c# api dropbox chunking


    【解决方案1】:

    示例使用随机字节。

    注释random 流的代码并在using 语句中包含您的文件流。在localContentFullPath 中包含带有文件名和扩展名的完整路径,并将filename 分开以在DropBox 中显示文件列表。

        public async Task ChunkUpload(DropboxClient client, string folder, string localContentFullPath)
        {
            Console.WriteLine("Chunk upload file...");
            // Chunk size is 128KB.
            const int chunkSize = 128 * 1024;
    
            // Create a random file of 1MB in size.
            // var fileContent = new byte[1024 * 1024];
            // new Random().NextBytes(fileContent);
    
            //using (var stream = new MemoryStream(fileContent))
    
            var filename = System.IO.Path.GetFileName(localContentFullPath.ToString());
            using (var stream = new FileStream(localContentFullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
    ...
    

    【讨论】:

    • @Marlo,你能告诉我使用后我可以使用什么吗?是文件夹指定了保管箱文件夹名称。这是我的代码.. using (var stream = new FileStream(content, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { var updated = await dbx.Files.UploadAsync("/" + folder + "/" +文件,WriteMode.Overwrite.Instance,正文:流); }
    • 真的很慢,比浏览器上传慢2倍以上。有什么办法可以优化吗?
    猜你喜欢
    • 1970-01-01
    • 2016-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-27
    • 1970-01-01
    相关资源
    最近更新 更多