cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Share your feedback on the Document Scanning Experience in the Dropbox App right here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Re: Upload session api creating empty files

Upload session api creating empty files

ankit_goyal
Explorer | Level 3
Go to solution
import requests
import json

class DropboxUtil:
_token = None
_logger = None
UPLOAD_SIZE = 149 * 1024 * 1024

def __init__(self, token):
DropboxUtil._token = "Bearer " + token

def upload(self, data = None):
"""
Start a new upload session and upload the requested content to desired directory on Dropbox.
"""
try:
upload_url = "https://content.dropboxapi.com/2/files/upload_session/start"

api_arguments = {
"close" : False
}

upload_response = requests.post(upload_url, headers = {
"Authorization" : self._token,
"Dropbox-API-Arg" : json.dumps(api_arguments),
"Content-Type" : "application/octet-stream"
})

if "session_id" in upload_response.json():
return upload_response.json()['session_id']
else:
raise Exception("Unable to upload file to Dropbox")
except Exception as e:
raise e

def _append_to_upload(self, session_id, offset, data):
try:
append_uri = "https://content.dropboxapi.com/2/files/upload_session/append_v2"

api_arguments = {
"cursor": {
"session_id": session_id,
"offset": offset
},
"close": False
}

requests.post(append_uri, data, headers={
"Authorization" : self._token,
"Content-Type" : "application/octet-stream",
"Dropbox-API-Arg" : json.dumps(api_arguments)
})
except Exception as e:
raise e

def _finish_upload(self, filepath, offset, session_id):
"""
Ends current upload session and creates the file with given filename. Offset is the total size if file in bytes.
"""
try:
finish_upload_url = "https://content.dropboxapi.com/2/files/upload_session/finish"

api_arguments = {
"cursor": {
"session_id": session_id,
"offset": offset
},
"commit": {
"path": filepath,
"mode": "add",
"autorename": True,
"mute": False,
"strict_conflict": False
}
}

finish_response = requests.post(finish_upload_url, headers = {
"Authorization" : self._token,
"Content-Type" : "application/octet-stream",
"Dropbox-API-Arg" : json.dumps(api_arguments)
})
print(finish_response.content)
except Exception as e:
raise e

if __name__ == '__main__':
db = DropboxUtil('<token>')
session_id = db.upload()
data = b'test_data'
db._append_to_upload(session_id, len(data), data)
db._finish_upload('/test11.txt', 0, session_id)  

 

Why does test11.txt is always created with no content ?

1 Accepted Solution

Accepted Solutions

Greg-DB
Dropbox Staff
Go to solution

I see from your code that you're only sending data with the append call. You can actually send data on any/all of the start, append, and finish calls, though it's not required. (Also, for small uploads like this, you don't need to use upload sessions at all, but I presume this is just for the sake of testing the code during development.)

Anyway, you are sending data with the append call, but you aren't checking if that call is succeeding. You should check the result and add some error handling around the append call. You can check success via the returned status code, and check the response body for more information.

I suspect the underlying issue is due to your "offset" handling, and is being obscured by the lack of response/error handling for the append call. Specifically, the "offset" you supply should be "The amount of data that has been uploaded so far" (for the upload session overall), not how much you're sending in the current request, as you appear to have in your code with `len(data)`.

View solution in original post

3 Replies 3

Greg-DB
Dropbox Staff
Go to solution

I see from your code that you're only sending data with the append call. You can actually send data on any/all of the start, append, and finish calls, though it's not required. (Also, for small uploads like this, you don't need to use upload sessions at all, but I presume this is just for the sake of testing the code during development.)

Anyway, you are sending data with the append call, but you aren't checking if that call is succeeding. You should check the result and add some error handling around the append call. You can check success via the returned status code, and check the response body for more information.

I suspect the underlying issue is due to your "offset" handling, and is being obscured by the lack of response/error handling for the append call. Specifically, the "offset" you supply should be "The amount of data that has been uploaded so far" (for the upload session overall), not how much you're sending in the current request, as you appear to have in your code with `len(data)`.

ankit_goyal
Explorer | Level 3
Go to solution

Hi Greg 

Thanks for your response. 

Yes you are right, the issue is with offset only. It is working now. But as per the documentation https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-append , the append method does not have any return value.

 

Regards

 

Greg-DB
Dropbox Staff
Go to solution

It doesn't have a successful return value, but it can raise a number of different errors. You should still check the response for an error. You can check the status code to determine success/failure at a high level, and check the response body for specific error information. With the incorrect code you had, for instance, the body would have contained error information about the incorrect offset.

Need more support?