2016-10-18 90 views

回答

2

您可以快速上傳使用files/upload_session/startfiles/upload_session/append_v2files/upload_session/finish API端點文件塊。這裏是一個使用我的小型保管箱v2 api包裝的例子(dropbox-v2-api):

const CHUNK_LENGTH = 100; 
//create read streams, which generates set of 100 (CHUNK_LENGTH) characters of values: 1 and 2 
const firstUploadChunkStream =() => utils.createMockedReadStream('1', CHUNK_LENGTH); 
const secondUploadChunkStream =() => utils.createMockedReadStream('2', CHUNK_LENGTH); 

sessionStart((sessionId) => { 
    sessionAppend(sessionId,() => { 
     sessionFinish(sessionId); 
    }); 
}); 

function sessionStart(cb) { 
    dropbox({ 
     resource: 'files/upload_session/start', 
     parameters: { 
      close: false 
     }, 
     readStream: firstUploadChunkStream() 
    }, (err, response) => { 
     if (err) { return console.log('sessionStart error: ', err) } 
     console.log('sessionStart response:', response); 
     cb(response.session_id); 
    }); 
} 


function sessionAppend(sessionId, cb) { 
    dropbox({ 
     resource: 'files/upload_session/append_v2', 
     parameters: { 
      cursor: { 
       session_id: sessionId, 
       offset: CHUNK_LENGTH 
      }, 
      close: false, 
     }, 
     readStream: secondUploadChunkStream() 
    }, (err, response) => { 
     if(err){ return console.log('sessionAppend error: ', err) } 
     console.log('sessionAppend response:', response); 
     cb(); 
    }); 
} 

function sessionFinish(sessionId) { 
    dropbox({ 
     resource: 'files/upload_session/finish', 
     parameters: { 
      cursor: { 
       session_id: sessionId, 
       offset: CHUNK_LENGTH * 2 
      }, 
      commit: { 
       path: "/result.txt", 
       mode: "add", 
       autorename: true, 
       mute: false 
      } 
     } 
    }, (err, response) => { 
     if (err) { return console.log('sessionFinish error: ', err) } 
     console.log('sessionFinish response:', response); 
    }); 
} 
1

雖然任何單個請求不應該大於150 MB(並且通常應該使用明顯更小的塊大小),但您可以通過使用多個請求來上傳比這更大的文件。

下面有一個使用上傳會話的例子。該示例使用Python SDK,但JavaScript SDK,但它應該作爲有用的參考,因爲邏輯是相同的。 (它們都使用相同的底層API。)

這使用Dropbox Python SDK將文件上傳到Dropbox API,從file_path指定的本地文件到dest_path指定的遠程路徑。它還選擇是否使用基於文件大小的上傳會話:

f = open(file_path) 
file_size = os.path.getsize(file_path) 

CHUNK_SIZE = 4 * 1024 * 1024 

if file_size <= CHUNK_SIZE: 

    print dbx.files_upload(f.read(), dest_path) 

else: 

    upload_session_start_result = dbx.files_upload_session_start(f.read(CHUNK_SIZE)) 
    cursor = dropbox.files.UploadSessionCursor(session_id=upload_session_start_result.session_id, 
               offset=f.tell()) 
    commit = dropbox.files.CommitInfo(path=dest_path) 

    while f.tell() < file_size: 
     if ((file_size - f.tell()) <= CHUNK_SIZE): 
      print dbx.files_upload_session_finish(f.read(CHUNK_SIZE), 
              cursor, 
              commit) 
     else: 
      dbx.files_upload_session_append(f.read(CHUNK_SIZE), 
              cursor.session_id, 
              cursor.offset) 
      cursor.offset = f.tell() 

f.close() 
0

我舉個例子!

testFile1Data = "test file data 1"; 
dbx.filesUploadSessionStart({ 
    contents: testFile1Data, 
    close: true, 
}) 
.then(function (response) { 
    file1Start = response; 
}) 
.catch(function (err) { 
    console.log(err); 
}); 

testFile2Data = "test file data 2"; 
dbx.filesUploadSessionStart({ 
    contents: testFile2Data, 
    close: true, 
}) 
.then(function (response) { 
    file2Start = response; 
}) 
.catch(function (err) { 
    console.log(err); 
}); 

dbx.filesUploadSessionFinishBatch({entries: [ 
    {cursor: {session_id: file1Start.session_id, offset: testFile1Data.length}, commit: {path: "/testFile1.txt"}}, 
    {cursor: {session_id: file2Start.session_id, offset: testFile2Data.length}, commit: {path: "/testFile2.txt"}}, 
    ]}) 
.then(function (response) { 
    finishBatch = response; 
}) 
.catch(function (err) { 
    console.log(err); 
}); 

dbx.filesUploadSessionFinishBatchCheck({async_job_id: finishBatch.async_job_id}) 
.then(function (response) { 
    finishBatch = response 
}) 
.catch(function (err) { 
    console.log(err); 
}); 

我接到了一個問題線程在github的例子 - https://github.com/dropbox/dropbox-sdk-js/issues/80#issuecomment-283189888