我有大sql备份,我想将它们保存在Dropbox中,但我只是想将副本发送到Dropbox并将文件移动到外部硬盘因为我的服务器硬盘空间。
我正在尝试使用dropbox api的chunk上传,这是他们提供的示例代码。
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周。最后我想问一下这个问题。
如何使用此方法,使用发送文件路径,并从该大文件上传块?
谢谢你们。
答案 0 :(得分:2)
该示例使用随机字节。
评论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))
...