Storing personal files in Dropbox is useful, but collaborating with others can make you even more effective. Whether for your business, school, or personal projects, Dropbox sharing can create more engaging work. There are a number of ways to incorporate shared files and folders with Dropbox. Each method can be accessed with the Dropbox API, allowing your app to automatically add the right people to your projects.
In this post, we’ll use the Dropbox API to implement sharing in three ways. The code samples use the Dropbox Python SDK, but you could use any SDK (or make direct API calls).
Prepare Your Environment
In order to get started, you’ll need the Dropbox Python SDK and an access token for your Dropbox App if you don’t already have one. You can create an app from the App Console and get your own access token in the app settings.
If you don’t have a Python environment setup, then you can follow along with the set up instructions in our Getting Started guide.
Now you have everything you need to begin. In a new file, import the Dropbox Python SDK, enter your access token, and create a variable with the Dropbox object:
import dropbox
token = 'your_access_token'
dbx = dropbox.Dropbox(token)
Imagine you’re a professor at a local university. It’s your department’s thesis presentation day and you’re recording all of the student’s talks so they have the opportunity to evaluate themselves later. Half hour videos are too large in size to send to them over email, but because you automatically backup your videos to Dropbox there is a way to share these videos to the students in a streamlined fashion:
Here’s how to create a shared link* with the Dropbox API:
def creating_shared_link_no_settings(path):
link = dbx.sharing_create_shared_link_with_settings(path)
print(link.url)
creating_shared_link_no_settings("/students/Amy/video.mp4")
*Note that the /sharing/create_shared_link_with_settings endpoint is used with no settings to create a basic shared link. The link is subject to a team’s sharing policy on Dropbox Business accounts.
Besides the inherent simplicity of a link, it also provides some extra flexibility in how the content is shared. Keeping with our educator example; let’s say you have a presentation video of a student in your research group. You can post the link on your research group’s website or your chat group, giving you the ability to share the video with undefined groups quickly.
There are many reasons to create a shared link, including sharing the same files with multiple people or wanting to give view-only access. Additionally, the recipient of the link does not need a Dropbox account in order to view the file.
Configure your Shared Link
Perhaps you want to post the link to the video on your faculty website so all the members of your department can view it, but you want to respect the student’s privacy and not just let anyone view it. By creating a password, you can ensure that if the video is shared more broadly, only the intended audience can access it.
def creating_shared_link_password(path, password):
link_settings = dropbox.sharing.SharedLinkSettings(
requested_visibility =
dropbox.sharing.RequestedVisibility.password,
link_password=password,
#expires=datetime.datetime.utcnow() + datetime.timedelta(hours=24)
)
link = dbx.sharing_create_shared_link_with_settings(path, settings=link_settings)
print(link.url)
creating_shared_link_password('/students/Amy/video.mp4','password')
The code is similar to the previous sample, but here you’re adding parameters to configure the shared link’s settings. The visibility is set to require a password and you supply the link password separately.
Optionally, you can also set an expiration (commented out in the code), but you’ll need to import Python’s datetime library.
You know the students in your class have Dropbox accounts because the university provides them during enrollment. For a simulated business negotiation exercise, you’d like to grant view-only access to specific files on a per student basis using their emails. You should take advantage of view-only file access.
Sharing a file for view-only access is more secure as the recipient must be authenticated with Dropbox to view the file. As the sharer, you’re able to verify whether the recipient, or student in our example, has viewed the file. Additionally, shared files make it easier to collaborate because the student is already logged in and can leave comments.
Give your students view-only file access using Dropbox’s sharing/add_file_member endpoint. Be sure to add a message so they know what the file is.
member = dropbox.sharing.MemberSelector.email('Amy@college.edu')
dbx.sharing_add_file_member('/2019/Students/Amy_Thesis.mp4', [member], "Here's your thesis video.")
Note: the recipient must have a Dropbox account in order to view the file.
Along with the video, perhaps you’d like to include feedback of your student’s presentation. Instead of sharing a file, you can share an entire folder.
While files can only have viewers, folders can have editing privileges for certain people–allowing others to add onto that feedback. For example, perhaps another professor wants to add their feedback alongside yours. You can share resources with them by creating a shared folder:
def creating_shared_folder(folder_path, access_level, email, message):
dbx.files_create_folder(folder_path)
# sharing_folder = dbx.sharing_share_folder(folder_path, force_async=True)
sharing_folder = dbx.sharing_share_folder(folder_path)
if sharing_folder.is_complete():
sharing_folder_data = sharing_folder.get_complete()
if sharing_folder.is_async_job_id():
async_job_id = sharing_folder.get_async_job_id()
# helper function will block until async sharing job completes
retry_sharing_job(async_job_id)
sharing_folder_job = dbx.sharing_check_share_job_status(async_job_id)
sharing_folder_data = sharing_folder_job.get_complete()
member = dropbox.sharing.MemberSelector.email(email)
add_member = dropbox.sharing.AddMember(member, access_level)
members = [add_member]
dbx.sharing_add_folder_member(sharing_folder_data.shared_folder_id, members, custom_message=message)
print(f"Folder successfully created and shared with {email}.")
creating_shared_folder(
'/students/Amy',
dropbox.sharing.AccessLevel.editor,
'TeacherFriend@college.edu',
'This is the message they will see'
)
You’ve created this functionality by stringing together a series of calls. First you create the folder, share the folder*, and add a member to the shared folder.
*Important note: the folder share may happen asynchronously, in which case it will return an async_job_id instead of complete. In our sample, we’re addressing that with some simple retry logic:
def retry_sharing_job(async_job_id):
sharing_job = dbx.sharing_check_share_job_status(async_job_id)
if sharing_job.is_complete():
print("Async sharing job completed...")
pass
else:
print("Async sharing job in progress")
print("....waiting 3 seconds...")
time.sleep(3)
retry_sharing_job(async_job_id)
Using a folder gives you the benefit of being able to add other files later (such as their grades or other assignments) without needing to go through the sharing process again.
There are four parameters you’ll want to set when sharing a folder with this function:
1. folder_path – The name and path to the folder. In this example, this could be ‘/students/Amy’. Make sure to start the path with a slash
2. access_level – The access level of the person you’re sharing the folder with. Perhaps you want the students to be able to turn in a reflection to their folder, so you set the access as editor.
3. email – The person’s email, such as ‘Amy@College.edu’
4. message – The message they will see when you share it with them. This way they aren’t confused why they suddenly see a folder with their name in it. Perhaps something along the lines of ‘Here’s the notes and video from your thesis presentation’
If you have a list of your students’ names and emails you can loop through all them as well and really speed up the process:
students = [('Amy', 'Amy@College.edu'), ('Bill', 'Bill@College.edu'), ('Chad', 'Chad@College.edu')]
for name, email in students:
creating_shared_folder(f'/students/{name}', dropbox.sharing.AccessLevel.editor, email, 'Here is your talk')
Automate Your Link, File, and Folder Sharing
We covered several different ways to share using the Dropbox API:
- A shared link is a great choice to provide view-only access to non-Dropbox users and can be configured with extra settings like password and expiration.
- If the recipient is a Dropbox user, then you can directly share access to the file—giving your collaborator the ability to view and comment on the file.
- Sharing a folder with a Dropbox user is a great way to have a collaborative space to work from shared resources.
Whether you’re a professor reviewing research, a manager organizing one-on-one notes, or a team member working on company announcements, you likely have collaborators. Use these sharing methods to improve how you collaborate and work with others.
See what is possible with the Dropbox API and find other ways to automate your work. Please keep an eye out for a followup article where we’ll explore an interesting set of features available for Dropbox Business users, Teams and Groups.
Build with Dropbox today at www.dropbox.com/developers