Beispiel #1
0
import os,sys,re
import utility

#The "set" content program for S3 using Python boto
#Mario Barrenechea - DSC Class, Fall 2012
connection = utility.get_s3_connection_from_env()
sysargs = len(sys.argv)
if sysargs >= 3 and sysargs <= 4:
	bucket_name = sys.argv[1]
	key_name = sys.argv[2]
	#If there does exist such a bucket instance...
	if utility.check_if_bucket_exists(connection, bucket_name) == True:
		bucket_instance = connection.get_bucket(bucket_name)
		bucket_key = bucket_instance.get_key(key_name)		
		#If there does exist such a bucket key...
		if utility.check_if_key_exists(bucket_instance, bucket_key.name) == True:
			#That means we should have <program name> <bucket> <key> 
			if sysargs == 3:
				print "[INFO]: We have <%s><%s><%s>" % (sys.argv[0], sys.argv[1], sys.argv[2])
				user_input = raw_input("Accepting input from stdin: ")
				bucket_key.set_contents_from_string(user_input)
				print "[OK]: The contents was successfully sent to %s:%s" % (bucket_instance, bucket_key)

			#That means we should have <program name> <bucket> <key> [file]
			elif sysargs == 4:
				print "[INFO]: We have <%s><%s><%s>[%s]" % (sys.argv[0], sys.argv[1], sys.argv[2], sys.argv[3])
				filename = sys.argv[3]
				if os.path.exists(filename) == True:
					bucket_key.set_contents_from_filename(filename)
				else:
					print"[ERROR]: Could not find file (%s)" % filename
Beispiel #2
0
    else:
        file_name = False
else:
    print "Usage:", sys.argv[0],"<bucket> <name> [<local-file>]"
    print "If local-file is omitted, output is to stdout"
    sys.exit(1)

connection = utility.get_s3_connection_from_env()

if file_name != False and not os.path.isfile(file_name):
    print "File ", file_name, "doesn't exist"
    sys.exit(2)

if not utility.check_if_bucket_exists(connection, bucket_name):
    print "Bucket", bucket_name,"doesn't exist"
    sys.exit(3)

bucket_instance = connection.get_bucket(bucket_name)
if not utility.check_if_key_exists(bucket_instance, key_name):
    print "Key", key_name, "not in bucket", bucket_name
    sys.exit(4)

k = boto.s3.key.Key(bucket_instance)
k.key = key_name
if file_name == False:
    sys.stdout.write(k.get_contents_as_string())
else:
    k.get_contents_from_file(open(file_name))
    print "Contents of file", file_name, " should be filled by bucket", bucket_name, "/", key_name