Saturday, June 11, 2016

Sample program to take a local backup file, upload it to S3 storage and inform an api

import boto
import boto.s3
import sys
from boto.s3.key import Key

AWS_ACCESS_KEY_ID = 'an_access_key_for_the_bucket'
AWS_SECRET_ACCESS_KEY = 'an_access_secret_for_the_bucket'

bucket_name = 'usersbackup'
conn = boto.connect_s3(AWS_ACCESS_KEY_ID,
AWS_SECRET_ACCESS_KEY)

bucket = conn.get_bucket(bucket_name, validate=False)

if (len(sys.argv)<= 1):
print('Usage: python backup.py <filename>')
sys.exit(0)

testfile = sys.argv[1]
print 'Uploading %s to Amazon S3 bucket %s' % \
(testfile, bucket_name)

def percent_cb(complete, total):
sys.stdout.write('.')
sys.stdout.flush()

def genCode():
import random
import string
return ''.join([random.choice(string.digits + string.ascii_uppercase) for x in range(0, 12)])

k = Key(bucket)
k.key = genCode()
k.set_contents_from_filename(testfile,
cb=percent_cb, num_cb=10)
sys.stdout.write('DONE')
sys.stdout.write('\n')
sys.stdout.flush()
import pycurl
from StringIO import StringIO
buffer = StringIO()
c = pycurl.Curl()
c.setopt(pycurl.POST, 0)
post_data = {'key': k.key}
from urllib import urlencode
postfields = urlencode(post_data)
c.setopt(c.POSTFIELDS, postfields)
c.setopt(c.URL, 'http://myapi/backup/v1/addbentry')

Convert a sorted array into a binary
search tree

Node makeBST (List <int> nums, int start, int end)
{
If start > end return null;
Int mid =(start+end)/2;
Node root = new node (nums [mid]);
Root.left = makeBST (nums, start, mid);
Root.right = makeBST (nums, mid+1, end);
Return root;
}

No comments:

Post a Comment