Python Cloud Synchronization Library
pip install cloudsync
# install provider support
pip install cloudsync-gdrivecloudsync sync --help
cloudsync sync file:c:/users/me/documents gdrive:/mydocs
# on linux you can pass -D for 'daemon mode', which will detatch and run in the backgroundimportcloudsync# Get a generic client_id and client_secret. Do not use this in production code.# For more information on getting your own client_id and client_secret, see README_OAUTH.mdoauth_config=cloudsync.command.utils.generic_oauth_config('gdrive')
# get an instance of the gdrive provider classprovider=cloudsync.create_provider('gdrive', oauth_config=oauth_config)
# Start the oauth process to login in to the cloud providercreds=provider.authenticate()
# Use the credentials to connect to the cloud providerprovider.connect(creds)
# Perform cloud operationsforentryinprovider.listdir_path("/"):
print(entry.path)importcloudsyncimporttempfileimporttime# a little setuplocal_root=tempfile.mkdtemp()
remote_root="/cloudsync_test/"+time.strftime("%Y%m%d%H%M")
provider_name='gdrive'print("syncronizing between %s locally and %s on %s"% (local_root, remote_root, provider_name))
# Get a generic client_id and client_secret. Do not use this in production code.# For more information on getting your own client_id and client_secret, see README_OAUTH.mdcloud_oauth_config=cloudsync.command.utils.generic_oauth_config(provider_name)
# get instances of the local file provider and cloud provider from the provider factorylocal=cloudsync.create_provider("filesystem")
remote=cloudsync.create_provider(provider_name, oauth_config=cloud_oauth_config)
# Authenticate with the remote provider using OAuthcreds=remote.authenticate()
# Connect with the credentials acquired by authenticating with the providerlocal.namespace=local_root# filesystem provider wants to know the root namespace before connectinglocal.connect(None)
remote.connect(creds)
# Create the folder on google drive to syncronize locallyprint("Creating folder %s on %s"% (remote_root, provider_name))
remote.mkdirs(remote_root) # provider.mkdirs() will automatically create any necessary parent folders# Specify which folders to syncronizesync_roots= (local_root, remote_root)
# instantiate a new sync engine and start syncingsync=cloudsync.CloudSync((local, remote), roots=sync_roots)
sync.start()
# should sync this file as soon as it's noticed by watchdoglocal_hello_path=local.join(local_root, "hello.txt")
print("Creating local file %s"%local_hello_path)
withopen(local_hello_path, "w") asf:
f.write("hello")
# note remote.join, NOT os.path.join... Gets the path separator correctremote_hello_path=remote.join(remote_root, "hello.txt")
# wait for sync to upload the new file to the cloudwhilenotremote.exists_path(remote_hello_path):
time.sleep(1)
remote_hello_info=remote.info_path(remote_hello_path)
# rename in the cloudlocal_goodbye_path=local.join(local_root, "goodbye.txt")
remote_goodbye_path=remote.join(remote_root, "goodbye.txt")
print("renaming %s to %s on %s"% (remote_hello_path, remote_goodbye_path, provider_name))
remote.rename(remote_hello_info.oid, remote_goodbye_path) # rename refers to the file to rename by oid# wait for sync to cause the file to get renamed locallywhilenotlocal.exists_path(local_goodbye_path):
time.sleep(1)
print("synced")
sync.stop(forever=True)
local.disconnect()
remote.disconnect()