Skip to content

Instantly share code, notes, and snippets.

@sammachin
Created January 27, 2016 10:45
Show Gist options
  • Save sammachin/78f0f38c1555aae9e9f7 to your computer and use it in GitHub Desktop.
Save sammachin/78f0f38c1555aae9e9f7 to your computer and use it in GitHub Desktop.
Dropbox to S3 image upload
import dropbox
import tinys3
import tempfile
from string import Template
import random
import string
S3_ACCESS_KEY = ""
S3_SECRET_KEY= ""
S3_BUCKET = "s3.example.com"
db_key = ''
db_secret = ''
db_folder = '/S3Archive/'
#Dropbox Auth
flow = dropbox.client.DropboxOAuth2FlowNoRedirect(db_key, db_secret)
authorize_url = flow.start()
access_token, user_id = flow.finish(code)
#Upload to S3
client = dropbox.client.DropboxClient(access_token)
folder_metadata = client.metadata(db_folder)
conn = tinys3.Connection(S3_ACCESS_KEY, S3_SECRET_KEY, tls=True, endpoint='s3-eu-west-1.amazonaws.com')
for file in folder_metadata['contents']:
if file['mime_type'].split("/")[0] == 'image':
name = file['path'].split("/")[-1]
f = client.get_file(file['path'])
tf = tempfile.NamedTemporaryFile()
tf.write(f.read())
conn.upload(name,tf,S3_BUCKET)
tf.close()
client.file_delete(file['path'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment