Project

General

Profile

1
import boto
2
import boto.s3.connection
3
import sys
4
import pymongo
5
from pymongo import MongoClient
6
import os.path
7
import time
8

    
9

    
10

    
11
def file_exist(bucket, file_path):
12
	c =bucket.get_key(file_path)
13
	if c is not None and c.size > 0:
14
		return True
15
	return False
16

    
17
def exportItemForMongoCollection(obsId, db, bucket, log_file):	
18
	destination_collection =db[ 's3_'+obsId[:36]]
19
	source_collection = db[ obsId[:36]]
20
	print 'Start to exporting objectStore :%s '%obsId
21
	i = 0
22
	skip = 0
23
	last_percentage = 0
24
	total = source_collection.estimated_document_count()
25
	for item in source_collection.find(no_cursor_timeout=True):
26
		fs_path = item['fsPath']
27
		objectId = item['id']
28
		dest_item = destination_collection.find_one({'id':objectId})
29
		if dest_item is None:
30
			if os.path.isfile(fs_path):
31
				i += 1
32
				if not file_exist(bucket, '%s/%s'%(obsId,objectId)):
33
					key = bucket.new_key('%s/%s'%(obsId,objectId))
34
					try:
35
						key.set_contents_from_filename(fs_path)	
36
					except Exception as e:
37
						time.sleep(10)
38
						key.set_contents_from_filename(fs_path)
39
					item.pop('_id', None)
40
					item.pop('fsPath')
41
					item['uri'] = 's3://%s/%s'%(bucket.name, key.name)
42
					destination_collection.insert_one(item)
43
				if i % 100 == 0:
44
					print "Exported %i/%i   (skipped)%i "%(i, total, skip)
45
			else:
46
				log_file.writeline('Missing file for objectStoreid: %s ObjectId:%s path: %s'%(obsId, objectId, fs_path))
47
		else:
48
			skip += 1
49
		
50

    
51
def start_import(metadataCollection, bucket, log_file, done_file, skip_store):
52
	client = MongoClient()
53
	db = client['objectStore']
54
	metadataCollection = db[metadataCollection]
55
	for item in metadataCollection.find(no_cursor_timeout=True):
56
		obsId = item['obsId']
57
		if obsId not in skip_store:
58
			exportItemForMongoCollection(obsId, db, bucket, log_file)
59
			destination_collection =db[ 's3_'+obsId[:36]]
60
			print "creating Index on ID"
61
			destination_collection.create_index([('id',pymongo.ASCENDING)])
62
			done_file.write('{}\n'.format(obsId))
63

    
64

    
65

    
66

    
67

    
68
if __name__=='__main__':
69
	args = sys.argv
70
	if not len(args) is not 3 :
71
		print "Error on applying script usage: python scriptName.py s3cfgPath objectstoreBucket"
72
	f = open(args[1])
73
	props = {}	
74
	for line in f:
75
		d =line.split('=')
76
		if len(d) == 2:
77
			props[d[0].strip()] = d[1].strip()
78
	skip_store =[]
79
	if os.path.isfile('store_done'):
80
		f = open('store_done')
81
		skip_store =[s.strip() for line in f if len(line) >0]
82

    
83
	bname = args[2]
84
	conn = boto.connect_s3(aws_access_key_id = props['access_key'], aws_secret_access_key = props['secret_key'],  host = props['host_base'], calling_format = boto.s3.connection.OrdinaryCallingFormat())
85
	bucket = conn.get_bucket(bname, validate=True)
86
	log_file = open('s3_migration.log', 'w')
87
	done_file = open('store_done', 'wb')
88
	start_import('metadataObjectStore',bucket, log_file, done_file, skip_store)
89
	log_file.close()
    (1-1/1)