<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: [Python V2] How to do batch upload? in Dropbox API Support &amp; Feedback</title>
    <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Python-V2-How-to-do-batch-upload/m-p/520226#M25386</link>
    <description>&lt;P&gt;Hey Greg. Would you mind going over an approach assuming the files being batch uploaded aren't small? As in "&lt;SPAN&gt;files_upload_session_append_v2"&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;would be best to read in each file by a chunk size? Thank you.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 13 May 2021 21:22:14 GMT</pubDate>
    <dc:creator>9krausec</dc:creator>
    <dc:date>2021-05-13T21:22:14Z</dc:date>
    <item>
      <title>[Python V2] How to do batch upload?</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Python-V2-How-to-do-batch-upload/m-p/329517#M19267</link>
      <description>&lt;P&gt;Hi!&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;I am trying to upload several files using the sessions and batch operators in the Dropbox SDK for Python. I'm trying to do something like this:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;dbx = dropbox.Dropbox(&amp;lt;API KEY&amp;gt;)

commit_info = []
for df in list_pandas_df: 
    df_raw_str = df.to_csv(index=False)
    upload_session = dbx.upload_session_start(df_raw_str.encode())
    commit_info.append(&lt;BR /&gt;            dbx.files.CommitInfo(path=/path/to/db/folder.csv&lt;BR /&gt;    )

dbx.files_upload_finish_batch(commit_info)&lt;/PRE&gt;&lt;P&gt;But, I do not completely understand from the documentation, how should I pass the commit data and the session info to the &lt;EM&gt;"files_upload_session_finish_batch"&lt;/EM&gt; function. The "&lt;EM&gt;files_upload_session_finish"&lt;/EM&gt; function does allow a commit and a cursor, while the documentation states that the batch option only takes a list of commits.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;My files are not particularly big, but they are numerous. That's why I'm not using any of the appending options. Should I use any cursor? I'm a little bit lost here &lt;img class="lia-deferred-image lia-image-emoji" src="https://www.dropboxforum.com/html/@B0F70D28791EB05FA3EA0C3BDDF08EE3/emoticons/1f61e.png" alt=":disappointed_face:" title=":disappointed_face:" /&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 May 2019 09:07:56 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Python-V2-How-to-do-batch-upload/m-p/329517#M19267</guid>
      <dc:creator>ivanhigueram</dc:creator>
      <dc:date>2019-05-29T09:07:56Z</dc:date>
    </item>
    <item>
      <title>Re: [Python V2] How to do batch upload?</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Python-V2-How-to-do-batch-upload/m-p/329632#M19281</link>
      <description>&lt;P&gt;[Cross-linking for reference: &lt;A href="https://stackoverflow.com/questions/54758978/dropbox-python-api-upload-multiple-files" target="_self"&gt;https://stackoverflow.com/questions/54758978/dropbox-python-api-upload-multiple-files&lt;/A&gt;&amp;nbsp;]&lt;/P&gt;
&lt;P&gt;Apologies for the confusion! The Python SDK documentation unfortunately doesn't do a good job identifying the types expected in certain parameters like this; I'll ask the team to work on improving that in the future.&lt;/P&gt;
&lt;P&gt;The `files_upload_session_finish_batch` method does work differently than the `files_upload_session_finish` method. The `files_upload_session_finish_batch` method expects a list of `UploadSessionFinishArg`, where each one encapsulates the cursor and commit info together.&lt;/P&gt;
&lt;P&gt;Here's a basic working example that shows how to do this:&lt;/P&gt;
&lt;PRE&gt;import dropbox

ACCESS_TOKEN = "..."

dbx = dropbox.Dropbox(ACCESS_TOKEN)

local_file_path = "..."

upload_entry_list = []

for i in range(5):
    f = open(local_file_path)
    upload_session_start_result = dbx.files_upload_session_start(f.read(), close=True) # assuming small files
    cursor = dropbox.files.UploadSessionCursor(session_id=upload_session_start_result.session_id,
                                               offset=f.tell())
    commit = dropbox.files.CommitInfo(path="/test_329517/%s" % i)
    upload_entry_list.append(dropbox.files.UploadSessionFinishArg(cursor=cursor, commit=commit))

print(dbx.files_upload_session_finish_batch(upload_entry_list))

# then use files_upload_session_finish_batch_check to check on the job&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Feb 2019 16:39:35 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Python-V2-How-to-do-batch-upload/m-p/329632#M19281</guid>
      <dc:creator>Greg-DB</dc:creator>
      <dc:date>2019-02-19T16:39:35Z</dc:date>
    </item>
    <item>
      <title>Re: [Python V2] How to do batch upload?</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Python-V2-How-to-do-batch-upload/m-p/329675#M19286</link>
      <description>&lt;P&gt;Hi Greg!&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Again thanks for replying back so promptly. I have some follow up questions. If I am looping through different Python objects, not files, that's why I'm first converting the pd.DataFrame to a string, and then pointing it to the dbx.files_upload_session_start() function.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Since my file is complete, and I am not passing a files in a context manager or a StringIO, I did not specify any offset in the cursor. Now that I'm trying to run the loop, I received the following error:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;Traceback (most recent call last):
  File "/Users/ivan/.pyenv/versions/weather_data/lib/python3.6/site-packages/dropbox/stone_serializers.py", line 337, in encode_struct
    field_value = getattr(value, field_name)
  File "/Users/ivan/.pyenv/versions/weather_data/lib/python3.6/site-packages/dropbox/files.py", line 10278, in offset
    raise AttributeError("missing required field 'offset'")
AttributeError: missing required field 'offset'

&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What are the real advantages of using the batch operations to upload files? Seems a convulted use case for objects in memory, rather than files.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Thanks again for your help.&lt;/P&gt;</description>
      <pubDate>Tue, 19 Feb 2019 20:34:41 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Python-V2-How-to-do-batch-upload/m-p/329675#M19286</guid>
      <dc:creator>ivanhigueram</dc:creator>
      <dc:date>2019-02-19T20:34:41Z</dc:date>
    </item>
    <item>
      <title>Re: [Python V2] How to do batch upload?</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Python-V2-How-to-do-batch-upload/m-p/329679#M19287</link>
      <description>&lt;P&gt;Regardless of where the data is coming from, the `&lt;A href="https://dropbox-sdk-python.readthedocs.io/en/latest/api/files.html#dropbox.files.UploadSessionCursor" target="_blank"&gt;UploadSessionCursor&lt;/A&gt;` object does require an `offset`, in order "to make sure upload data isn’t lost or duplicated in the event of a network error". It sounds like in your case the `offset` value would be the length of the string.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The main advantage of using `files_upload_session_finish_batch` is to minimize the number of "locks" needed when uploading multiple files.&amp;nbsp;&lt;A href="https://www.dropbox.com/developers/reference/data-ingress-guide" target="_blank"&gt;The Data Ingress Guide&lt;/A&gt; covers this in more detail. This applies to uploading from memory or files.&lt;/P&gt;
&lt;P&gt;The main advantage of using "upload sessions" to begin with is to enable apps to upload large files. If you're just uploading small files, you can certainly do so just using `&lt;A href="https://dropbox-sdk-python.readthedocs.io/en/latest/api/dropbox.html#dropbox.dropbox.Dropbox.files_upload" target="_blank"&gt;files_upload&lt;/A&gt;`, but you'd need to do so serially to avoid lock contention. Based on the code you provided, you're already uploading serially though, so it may not make much of a difference if you wish to switch to `&lt;A href="https://dropbox-sdk-python.readthedocs.io/en/latest/api/dropbox.html#dropbox.dropbox.Dropbox.files_upload" target="_blank"&gt;files_upload&lt;/A&gt;` for simplicity.&lt;/P&gt;</description>
      <pubDate>Tue, 19 Feb 2019 20:57:35 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Python-V2-How-to-do-batch-upload/m-p/329679#M19287</guid>
      <dc:creator>Greg-DB</dc:creator>
      <dc:date>2019-02-19T20:57:35Z</dc:date>
    </item>
    <item>
      <title>Re: [Python V2] How to do batch upload?</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Python-V2-How-to-do-batch-upload/m-p/520226#M25386</link>
      <description>&lt;P&gt;Hey Greg. Would you mind going over an approach assuming the files being batch uploaded aren't small? As in "&lt;SPAN&gt;files_upload_session_append_v2"&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;would be best to read in each file by a chunk size? Thank you.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 13 May 2021 21:22:14 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Python-V2-How-to-do-batch-upload/m-p/520226#M25386</guid>
      <dc:creator>9krausec</dc:creator>
      <dc:date>2021-05-13T21:22:14Z</dc:date>
    </item>
    <item>
      <title>Re: [Python V2] How to do batch upload?</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Python-V2-How-to-do-batch-upload/m-p/520411#M25390</link>
      <description>&lt;P&gt;&lt;a href="https://www.dropboxforum.com/t5/user/viewprofilepage/user-id/1441823"&gt;@9krausec&lt;/a&gt; I &lt;A href="https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/How-to-upload-files-in-batch/m-p/435181/highlight/true#M22976" target="_self"&gt;posted a sample here&lt;/A&gt; that may be useful. Hope this helps! &lt;/P&gt;</description>
      <pubDate>Fri, 14 May 2021 14:28:56 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Python-V2-How-to-do-batch-upload/m-p/520411#M25390</guid>
      <dc:creator>Greg-DB</dc:creator>
      <dc:date>2021-05-14T14:28:56Z</dc:date>
    </item>
    <item>
      <title>Re: [Python V2] How to do batch upload?</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Python-V2-How-to-do-batch-upload/m-p/521616#M25418</link>
      <description>&lt;P&gt;Thank you for the reply Greg. I did stumble on that post.&lt;BR /&gt;&lt;BR /&gt;I should of been more specific with my question -&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;If I have 20 files that I want uploaded in a single session, using "dbx.&lt;SPAN&gt;files_upload_session_finish_batch&lt;/SPAN&gt;()" and "dbx.files_upload_session_append_v2()" for files exceeding the set chunk size, how would I go about doing so?&lt;BR /&gt;&lt;BR /&gt;The example you posted here works great without using "files_upload_session_append_v2" as the files are assumed to be small.&lt;BR /&gt;&lt;BR /&gt;The example you linked works great, if uploading a single large file and wanted to chunk it out.&lt;BR /&gt;&lt;BR /&gt;I'm having difficulties figuring out how to approach combining the two examples with successful results.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Thank you for any help.&lt;/P&gt;</description>
      <pubDate>Thu, 20 May 2021 15:27:51 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Python-V2-How-to-do-batch-upload/m-p/521616#M25418</guid>
      <dc:creator>9krausec</dc:creator>
      <dc:date>2021-05-20T15:27:51Z</dc:date>
    </item>
    <item>
      <title>Re: [Python V2] How to do batch upload?</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Python-V2-How-to-do-batch-upload/m-p/521617#M25419</link>
      <description>&lt;P&gt;&lt;a href="https://www.dropboxforum.com/t5/user/viewprofilepage/user-id/10"&gt;@Greg-DB&lt;/a&gt;&amp;nbsp; - Sorry, forgot to mention you in reply.&lt;/P&gt;</description>
      <pubDate>Thu, 20 May 2021 15:28:23 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Python-V2-How-to-do-batch-upload/m-p/521617#M25419</guid>
      <dc:creator>9krausec</dc:creator>
      <dc:date>2021-05-20T15:28:23Z</dc:date>
    </item>
    <item>
      <title>Re: [Python V2] How to do batch upload?</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Python-V2-How-to-do-batch-upload/m-p/521660#M25421</link>
      <description>&lt;P&gt;&lt;a href="https://www.dropboxforum.com/t5/user/viewprofilepage/user-id/1441823"&gt;@9krausec&lt;/a&gt; Thanks for following up with the clarification. First, note that one "upload session" is required per file to be uploaded. So, you'd need to call files_upload_session_start once per file (so, 20 total in this case), and files_upload_session_append_v2 zero or more times per file (depending on each file's size).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then, to commit them together to avoid contention, you'd call files_upload_session_finish_batch once with the information for all 20 upload sessions. (That's instead of calling files_upload_session_finish once per file.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, you will need to combine portions of each of the examples for your use case. The example in this thread shows how to start multiple upload sessions and then commit them all together, while the example in the other thread shows how to append to any given upload session repeatedly as needed.&lt;/P&gt;</description>
      <pubDate>Thu, 20 May 2021 18:16:52 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Python-V2-How-to-do-batch-upload/m-p/521660#M25421</guid>
      <dc:creator>Greg-DB</dc:creator>
      <dc:date>2021-05-20T18:16:52Z</dc:date>
    </item>
    <item>
      <title>Re: [Python V2] How to do batch upload?</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Python-V2-How-to-do-batch-upload/m-p/521877#M25422</link>
      <description>&lt;P&gt;&lt;a href="https://www.dropboxforum.com/t5/user/viewprofilepage/user-id/10"&gt;@Greg-DB&lt;/a&gt;&amp;nbsp; - Thank you for following this and providing input. I think I'm close to a solution and will continue tinkering. In the meantime, please review my method below and provide any further suggestions.&lt;BR /&gt;&lt;BR /&gt;For output, I am getting an "UploadSessionFinishBatchLaunch" return at the end with an async_job_id. However, nothing is uploaded to dropbox after completion. Thank you.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from os import listdir
from os.path import isfile, join
import dropbox

def batchUpload(srcFiles, dstDir, dbx=None):
    if not dbx: dbx = DropboxTeam(DROPBOX_TOKEN).as_admin(ADMIN_ID)
    chunkSize = 2 * 1024 * 1024
    uploadEntryList = []
    for src in srcFiles:
        
        baseName = os.path.basename(src)
        dstPath  = dstDir + "/" + baseName
        fileSize = os.path.getsize(src)

        print("{0} ==&amp;gt; {1}".format(src, dstPath))

        with open(src, "rb") as fp:
            if fileSize &amp;lt;= chunkSize: # file smaller than chunkSize, no files_upload_session_append_v2
                uploadSessionStartResult = dbx.files_upload_session_start(fp.read(), dstPath, close=True)
                cursor = dropbox.files.UploadSessionCursor(session_id=uploadSessionStartResult.session_id,
                                            offset=fp.tell())
                commit = dropbox.files.CommitInfo(path=dstPath)
                
            else: # file larger than chunkSize. Use files_upload_session_append_v2
                uploadSessionStartResult = dbx.files_upload_session_start(fp.read(chunkSize))
                cursor = dropbox.files.UploadSessionCursor(session_id=uploadSessionStartResult.session_id,
                                                        offset=fp.tell())
                commit = dropbox.files.CommitInfo(path=dstPath)

                while fp.tell() &amp;lt;= fileSize:
                    if ((fileSize - fp.tell()) &amp;lt;= chunkSize): # file chunk append complete
                        print("COMPLETE CHUNKS")
                        uploadEntryList.append(files.UploadSessionFinishArg(cursor=cursor, commit=commit))
                        break
                    else:
                        print(fp.tell())
                        dbx.files_upload_session_append_v2(fp.read(chunkSize),
                                cursor)
                        cursor.offset = fp.tell()
            uploadEntryList.append(files.UploadSessionFinishArg(cursor=cursor, commit=commit))

    print( dbx.files_upload_session_finish_batch(uploadEntryList) )


srcDir = "C:/Users/alpha/Documents/scratch/LIGHTS - Copy"
srcFiles = [join(srcDir, f).replace('\\', '/') for f in listdir(srcDir) if isfile(join(srcDir, f))]
dstDir = "/scratch/poopOne"
dbx = DropboxTeam(DROPBOX_TOKEN).as_admin(ADMIN_ID)
batchUpload(srcFiles, dstDir, dbx)&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 21 May 2021 17:02:43 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Python-V2-How-to-do-batch-upload/m-p/521877#M25422</guid>
      <dc:creator>9krausec</dc:creator>
      <dc:date>2021-05-21T17:02:43Z</dc:date>
    </item>
    <item>
      <title>Re: [Python V2] How to do batch upload?</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Python-V2-How-to-do-batch-upload/m-p/521906#M25423</link>
      <description>&lt;P&gt;&lt;a href="https://www.dropboxforum.com/t5/user/viewprofilepage/user-id/1441823"&gt;@9krausec&lt;/a&gt; You'll need to poll files_upload_session_finish_batch_check to check on the result of the job. If you're not seeing anything uploaded, something must have failed, and the result from files_upload_session_finish_batch_check should tell you what it was.&lt;/P&gt;</description>
      <pubDate>Fri, 21 May 2021 19:40:41 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Python-V2-How-to-do-batch-upload/m-p/521906#M25423</guid>
      <dc:creator>Greg-DB</dc:creator>
      <dc:date>2021-05-21T19:40:41Z</dc:date>
    </item>
    <item>
      <title>Re: [Python V2] How to do batch upload?</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Python-V2-How-to-do-batch-upload/m-p/521932#M25425</link>
      <description>&lt;P&gt;&lt;a href="https://www.dropboxforum.com/t5/user/viewprofilepage/user-id/50413"&gt;@Greg&lt;/a&gt;&amp;nbsp;- Thank you again. No errors were returned. Just states that status is "in-progress"&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;UploadSessionFinishBatchJobStatus('in_progress', None)&lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;I'll tell ya what - You follow me through here to the end and I bet this is going to be an excellent reference for everyone else using Python. I'm actually surprised I wasn't able to find a working example in this forum, or in the examples on git.&lt;BR /&gt;&lt;BR /&gt;In the meantime I'll keep at it.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 21 May 2021 21:36:12 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Python-V2-How-to-do-batch-upload/m-p/521932#M25425</guid>
      <dc:creator>9krausec</dc:creator>
      <dc:date>2021-05-21T21:36:12Z</dc:date>
    </item>
    <item>
      <title>Re: [Python V2] How to do batch upload?</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Python-V2-How-to-do-batch-upload/m-p/521946#M25426</link>
      <description>&lt;P&gt;&lt;a href="https://www.dropboxforum.com/t5/user/viewprofilepage/user-id/1441823"&gt;@9krausec&lt;/a&gt; That indicates that the job is not done yet. You'll need to poll it until it's complete. That is, call it once every few seconds until it returns the result.&lt;/P&gt;</description>
      <pubDate>Fri, 21 May 2021 22:36:39 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Python-V2-How-to-do-batch-upload/m-p/521946#M25426</guid>
      <dc:creator>Greg-DB</dc:creator>
      <dc:date>2021-05-21T22:36:39Z</dc:date>
    </item>
  </channel>
</rss>

