Copying files between projects

Instead of copying individual files, which will make one API call per file, we recommend using a bulk API call. This way you can copy up to 100 files with a single API call.

Not optimized for rate limit

Copying individual files requires two API calls for each file: one to find the file by name, and another one to copy it. We recommend using the bulk API call instead.

for name in source_file_names:
    f = api.files.query(project=src_project, names=[name])[0]
    f.copy(project=target_project)

Optimized for rate limit

Using a bulk API call you can copy up to 100 files.

def bulk_copy_files(files_to_copy, target_project):
    "Copies files in batches of size 100"
     
    final_responses = {}
     
    for i in range(0, len(files_to_copy), 100):
         
        files = [f for f in files_to_copy[i:i + 100]]
        responses = api.actions.bulk_copy_files(files, target_project.id)
         
        for fileid, response in responses.items():
            if response['status'] != 'OK':
                raise Exception(
                    "Error copying {}: {}".format(fileid, response)
                )
                 
        final_responses.update(responses)
     
    return final_responses
     
     
files_to_copy = list(
    api.files.query(
        project=src_project,
        names=source_files,
        limit=100
    ).all()
)
 
responses = bulk_copy_files(files_to_copy, target_project)