def mainFunc(): #Bluemix Credentials auth_url = '' + '/v3' project_name = '' password = '' user_domain_name = '' project_id = '' user_id = '' region_name = '' # Get a Swift client connection object conn = swiftclient.Connection(key=password, authurl=auth_url, auth_version='3', os_options={ "project_id": project_id, "user_id": user_id, "region_name": region_name }) ## plainDir = r'C:\Users\Vivek\Desktop\cloud\plain' ## if not os.path.exists(plainDir): ## os.makedirs(plainDir) ## ## enDenDir = r'C:\Users\Vivek\Desktop\cloud\encrypted' ## if not os.path.exists(enDenDir): ## os.makedirs(enDenDir) #enKey = input('Give your passphrase: ') #print('Your key is : ', enKey) container_name1 = 'plain' container_name2 = 'encrypted' # File name for testing file_name = 'quiz1.txt' # Create a new container conn.put_container(container_name1) print "\nContainer %s created successfully." % container_name1 conn.put_container(container_name2) print "\nContainer %s created successfully." % container_name2 # List your containers print("\nContainer List:") for container in conn.get_account()[1]: print container['name'] selection = raw_input('Do you want to encrypt the file (Y/N): ') #print selection if selection is 'N': with open(file_name, 'w') as quiz1: conn.put_object(container_name1, file_name, contents="This is sample file for Task 1.", content_type='text/plain') if selection is 'Y': enfile = 'EnFileQuiz1.pgp' gpg_home = 'C:\Program Files (x86)\GnuPG' gpg = gnupg.GPG(gnupghome=gpg_home) #gpgobj = gnupg.GPG(gnupghome='/home/testgpguser/gpghome') data = gpg.gen_key_input(key_type="RSA", key_length=1024, passphrase="qwerty") key_gen = gpg.gen_key(data) #print key_gen with open(enfile, 'w') as f: encrypting = gpg.encrypt_file(f) conn.put_object(container_name2, enfile, contents=str(encrypting), content_type='text/plain') print("\nObject List:") for container in conn.get_account()[1]: for data in conn.get_container(container['name'])[1]: print 'object: {0}\t size: {1}\t date: {2}'.format( data['name'], data['bytes'], data['last_modified']) obj = conn.get_object(container_name1, file_name) with open(file_name, 'w') as my_example: my_example.write(obj[1]) print "\nObject %s downloaded successfully." % file_name ## obj = conn.get_object(container_name2, enfile) ## with open(enfile, 'w') as dec: ## dec.write(obj[1]) ## decrypted_data=gpg.decrypt(enfile, passphrase="qwerty") ## print '\nDecrypted File Downloaded.' # Delete an object conn.delete_object(container_name1, file_name) print "\nObject %s deleted successfully." % file_name # To delete a container. Note: The container must be empty! conn.delete_container(container_name1) print "\nContainer %s deleted successfully.\n" % container_name
def test_reset_stream(self): class LocalContents(object): def __init__(self, tell_value=0): self.already_read = False self.seeks = [] self.tell_value = tell_value def tell(self): return self.tell_value def seek(self, position): self.seeks.append(position) self.already_read = False def read(self, size=-1): if self.already_read: return '' else: self.already_read = True return 'abcdef' class LocalConnection(object): def __init__(self, parsed_url=None): self.reason = "" if parsed_url: self.host = parsed_url.netloc self.port = parsed_url.netloc def putrequest(self, *args, **kwargs): return def putheader(self, *args, **kwargs): return def endheaders(self, *args, **kwargs): return def send(self, *args, **kwargs): raise socket.error('oops') def request(self, *args, **kwargs): return def getresponse(self, *args, **kwargs): self.status = 200 return self def getheader(self, *args, **kwargs): return 'header' def read(self, *args, **kwargs): return '' def local_http_connection(url, proxy=None): parsed = urlparse(url) return parsed, LocalConnection() orig_conn = c.http_connection try: c.http_connection = local_http_connection conn = c.Connection('http://www.example.com', 'asdf', 'asdf', retries=1, starting_backoff=.0001) contents = LocalContents() exc = None try: conn.put_object('c', 'o', contents) except socket.error, err: exc = err self.assertEquals(contents.seeks, [0]) self.assertEquals(str(exc), 'oops') contents = LocalContents(tell_value=123) exc = None try: conn.put_object('c', 'o', contents) except socket.error, err: exc = err
def doRegularSwiftAuth(self): log.debug("Connecting to regular swift at: {}".format(self.swift_url)) self.conn = client.Connection(authurl=self.swift_url, user=self.swift_user, key=self.swift_pw)
def sapi(self): if not self._sapi: self._sapi = swiftclient.Connection(session=self.session) return self._sapi
def __init__(self): super(SwiftClientStub, self).__init__() self._connection = swift_client.Connection() self._containers = {} self._containers_list = [] self._objects = {}
def connect_to_swift(): return swift_client.Connection(authurl=cfg.common.keystone_url, user=cfg.common.user, key=cfg.common.password, tenant_name=cfg.common.tenant, auth_version=2)
def __init__(self, token): self.conn = client.Connection(preauthtoken=token, preauthurl=appConfig.swift_store_url)
decryptfilename = 'decryptfile' Passphrase = 'CloudComputing' project_id = 'projectid' user_id = 'userid' region_name = 'dallas' password = '******' auth_url = 'auth_url' + '/v3' user = '******' container_name = 'container_name' # Get a Swift client connection object conn = swiftclient.Connection(user=user, key=password, authurl=auth_url, auth_version='3', os_options={ "project_id": project_id, "user_id": user_id, "region_name": region_name }) #Class Implemented to check to see if new files are present in uploads folder class FileCreateEventHandler(FileSystemEventHandler): def __init__(self, observer): self.observer = observer def on_created(self, event): if not event.is_directory: uploadFile(event.src_path)
else: ### Authenticate user: OS_USERNAME = getpass.getpass("Username: "******"User info:", OS_USERNAME, sess.get_user_id() if 'OS_PROJECT_ID' in os.environ: # The token we've got from the environment is already scoped OS_PROJECT_ID = os.environ['OS_PROJECT_ID'] else: ### List user's projects: from keystoneclient.v3 import client ks = client.Client(session=sess, interface=OS_INTERFACE) projects = ks.projects.list(user=sess.get_user_id()) print "Available projects:", [t.name for t in projects] OS_PROJECT_ID = projects[0].id print "Selected project: ", OS_PROJECT_ID # (Re-)Scope the session auth = v3.Token(auth_url=OS_AUTH_URL, token=sess.get_token(), project_id=OS_PROJECT_ID) sess = session.Session(auth=auth) ### To list project's Swift containers (needs an scoped token, or re-scope the previous one): import swiftclient.client as swiftclient conn = swiftclient.Connection(session=sess) resp_headers, containers = conn.get_account() print "Containers:", [container['name'] for container in containers]
def _archive_swift(self, endpoint, token, basepath, lbid, proto): """ Archive HAProxy log files into swift. endpoint - Object store endpoint token - Authorization token basepath - Container base path lbid - Load balancer ID proto - Protocol of the load balancer we are archiving Note: It should be acceptable for exceptions to be thrown here as the controller should wrap these up nicely in a message back to the API server. """ proto = proto.lower() if not os.path.exists(self.haproxy_log): raise Exception('No HAProxy logs found') # We need a copy we can read reallog_copy = '/tmp/haproxy.log' self.ossvc.sudo_copy(self.haproxy_log, reallog_copy) self.ossvc.sudo_chown(reallog_copy, self.user, self.group) # Extract contents from the log based on protocol. This is # because each protocol (tcp or http) represents a separate # load balancer in Libra. See _config_to_string() for the # frontend and backend names we search for below. filtered_log = '/tmp/haproxy-' + proto + '.log' fh = open(filtered_log, 'wb') for line in open(reallog_copy, 'rb'): if re.search(proto + '-in', line): fh.write(line) elif re.search(proto + '-servers', line): fh.write(line) fh.close() os.remove(reallog_copy) # Compress the filtered log and generate the MD5 checksum value. # We generate object name using UTC timestamp. The MD5 checksum of # the compressed file is used to guarantee Swift properly receives # the file contents. ts = datetime.utcnow().strftime('%Y%m%d-%H%M%S') objname = 'haproxy-' + ts + '.log.gz' compressed_file = '/tmp/' + objname gzip_in = open(filtered_log, 'rb') gzip_out = gzip.open(compressed_file, 'wb') gzip_out.writelines(gzip_in) gzip_out.close() gzip_in.close() os.remove(filtered_log) etag = hashlib.md5(open(compressed_file, 'rb').read()).hexdigest() # We now have a file to send to Swift for storage. We'll connect # using the pre-authorized token passed to use for the given endpoint. # Then make sure that we have a proper container name for this load # balancer, and place the compressed file in that container. Creating # containers is idempotent so no need to check if it already exists. object_path = '/'.join([lbid, objname]) logfh = open(compressed_file, 'rb') try: conn = sc.Connection(preauthurl=endpoint, preauthtoken=token) conn.put_container(basepath) conn.put_object(container=basepath, obj=object_path, etag=etag, contents=logfh) except Exception as e: logfh.close() os.remove(compressed_file) errmsg = "Failure during Swift operations. Swift enabled?" errmsg = errmsg + "\nException was: %s" % e raise Exception(errmsg) logfh.close() os.remove(compressed_file)
# openstack example python-let: swift-list.py # # Author: JuanJo Ciarlante <*****@*****.**> # License: http://www.apache.org/licenses/LICENSE-2.0 # vim: si et sw=4 ts=4 import os from swiftclient import client as swift_client def get_creds(): # create a dictionary as e.g.: {'username': env['OS_USERNAME'], ... return { key: os.environ.get('OS_{}'.format(key.upper())) for key in ('auth_url', 'username', 'password', 'tenant_name', 'region_name') } creds = get_creds() swift_cli = swift_client.Connection( authurl=creds['auth_url'], user=creds['username'], key=creds['password'], auth_version='2.0', tenant_name=creds['tenant_name'], os_options={'region_name': creds['region_name']}) # For each swift container, print its name, objects count and bytes for container in swift_cli.get_account()[1]: print "{name:32} {count:8} {bytes:16}".format(**container)
def run(self): if os.path.isfile(self.args.pidfile): self.log.error("%s found: is the server already running?" % self.args.pidfile) return 1 stores = dict() for container, values in self.conf.items(): auth = dict( authurl=self.args.authurl, user=values['username'], key=values['password'], ) if self.args.keystone: try: from keystoneclient.v2_0 import client as _check_for_ksclient except ImportError: sys.exit( "auth 2.0 (keystone) requires python-keystoneclient") else: self.log.debug("using auth 2.0 (keystone)") if self.args.keystone_separator not in values['username']: self.log.error("%s: separator not found in %r, skipping" % (container, values['username'])) continue keystone_auth = values['username'].split( self.args.keystone_separator, 1) auth['tenant_name'], auth['user'] = keystone_auth auth['auth_version'] = '2.0' auth['os_options'] = dict( service_type=self.args.keystone_service, endpoint_type=self.args.keystone_endpoint, region_name=self.args.keystone_region, ) self.log.debug("os_options: %r" % auth['os_options']) cli = client.Connection(**auth) try: headers, _ = cli.get_container(container) except (socket.error, client.ClientException) as ex: if getattr(ex, 'http_status', None) == 404: self.log.warning("%s doesn't exist, skipping" % container) continue else: self.log.error("%s: %r, skipping" % (container, ex.msg)) continue self.log.debug(headers) meta = getMeta(headers) if not meta: self.log.warning("%s doesn't appear to be setup, skipping" % container) continue self.log.debug("Meta: %s" % meta) try: object_size = int(meta['object-size']) objects = int(meta['objects']) except ValueError as ex: self.log.error("%s doesn't appear to be correct: %s" % (container, ex)) return 1 if meta['version'] != disk_version: self.log.warning("Version mismatch %s != %s in %s" % (meta['version'], disk_version, container)) stores[container] = SwiftStorage( auth, container, object_size, objects, Cache(int(self.args.cache_limit * 1024**2 / object_size)), values['read-only'].lower() in ('1', 'yes', 'true', 'on'), ) addr = (self.args.bind_address, self.args.bind_port) server = Server(addr, stores) if not self.args.foreground: try: if os.fork() != 0: os._exit(0) except OSError as ex: self.log.error("Failed to daemonize: %s" % ex) return 1 os.setsid() fd = os.open(os.devnull, os.O_RDWR) os.dup2(fd, sys.stdin.fileno()) os.dup2(fd, sys.stdout.fileno()) os.dup2(fd, sys.stderr.fileno()) self.log.info("Starting to serve on %s:%s" % (addr[0], addr[1])) try: fd = os.open(self.args.pidfile, (os.O_CREAT | os.O_EXCL | os.O_WRONLY), 0o644) except OSError as ex: self.log.error("Failed to create the pidfile: %s" % ex) return 1 with os.fdopen(fd, "w") as pidfile_handle: pidfile_handle.write("%s\n" % os.getpid()) pidfile_handle.flush() server.serve_forever() os.remove(self.args.pidfile) # unlock the storages before exit server.unlock_all() self.log.info("Exiting...") return 0
def test_07_object_two_passes(self): """Objects modified two passes.""" index = {} index_container = {} # Create account self.created = filler.create_swift_account(self.o_ks_client, self.pile, 1, 1, index) for account, account_id, username in (self.extract_created_a_u_iter( self.created)): tenant_cnx = sclient.Connection(self.o_st, "%s:%s" % (account, username), self.default_user_password, auth_version=2) acc = (account, account_id) filler.create_containers(tenant_cnx, acc, 1, index_container) filler.create_objects(tenant_cnx, acc, 3, 2048, index_container) # Start sync process self.swsync.process() # Modify objects in containers for account, account_id, username in (self.extract_created_a_u_iter( self.created)): tenant_cnx = sclient.Connection(self.o_st, "%s:%s" % (account, username), self.default_user_password, auth_version=2) token = tenant_cnx.get_auth()[1] c_o = self.list_objects_in_containers(account_id, token, 'orig') for cont, objs in c_o.iteritems(): for obj in objs: # Modify object meta obj_d, data = self.get_object_detail( account_id, token, 'orig', cont, obj['name']) meta = { k: v for k, v in obj_d.iteritems() if k.startswith('x-object-meta') } meta_k_names = [k.split('-')[-1] for k in meta] headers = {} headers['X-Object-Meta-a1'] = 'b1' headers["X-Remove-Object-Meta-%s" % meta_k_names[0]] = 'x' headers["X-Object-Meta-%s" % meta_k_names[1]] = 'b2' self.post_object(account_id, token, 'orig', headers=headers, container=cont, name=obj['name']) # Create an object self.put_object(account_id, token, 'orig', cont, 'foofoo', 'barbarbar') self.put_object(account_id, token, 'orig', cont, 'foofoo1', 'barbarbar') self.put_object(account_id, token, 'orig', cont, 'foofoo2', 'barbarbar') o_names = [o['name'] for o in objs] # Delete an object name = o_names[0] self.delete_object(account_id, token, 'orig', cont, name) # Start sync process self.swsync.process() # Now verify dest for account, account_id, username in (self.extract_created_a_u_iter( self.created)): # Verify container listing olo = self.list_objects_in_containers(account_id, self.o_admin_token, 'orig') old = self.list_objects_in_containers(account_id, self.d_admin_token, 'dest') # Verify we have the same amount of container self.assertListEqual(olo.keys(), old.keys()) # For each container for c, objs in olo.items(): for obj in objs: # Verify first object detail returned by container server match = [od for od in old[c] if od['name'] == obj['name']] self.assertEqual(len(match), 1) obj_d = match[0] a = obj.copy() b = obj_d.copy() del a['last_modified'] del b['last_modified'] self.assertDictEqual(a, b) # Verify object details from object server obj_names = [d['name'] for d in olo[c]] for obj_name in obj_names: objd_o = self.get_object_detail(account_id, self.o_admin_token, 'orig', c, obj_name) objd_d = self.get_object_detail(account_id, self.d_admin_token, 'dest', c, obj_name) self.verify_aco_diff(objd_o, objd_d) # Verify content self.assertEqual(objd_o[1], objd_d[1])
def test_06_container_two_passes(self): """Containers modified two sync passes.""" index = {} index_container = {} # Create account self.created = filler.create_swift_account(self.o_ks_client, self.pile, 3, 1, index) for account, account_id, username in (self.extract_created_a_u_iter( self.created)): tenant_cnx = sclient.Connection(self.o_st, "%s:%s" % (account, username), self.default_user_password, auth_version=2) acc = (account, account_id) filler.create_containers(tenant_cnx, acc, 3, index_container) # Start sync process self.swsync.process() # Modify container in account for account, account_id, username in (self.extract_created_a_u_iter( self.created)): tenant_cnx = sclient.Connection(self.o_st, "%s:%s" % (account, username), self.default_user_password, auth_version=2) token = tenant_cnx.get_auth()[1] # Modify an existing container meta clo = self.list_containers(account_id, token, 'orig') co_name = clo[0]['name'] c_meta = self.get_container_meta(account_id, token, 'orig', co_name) c_meta_k_names = [k.split('-')[-1] for k in c_meta] headers = {} headers['X-Container-Meta-a1'] = 'b1' headers["X-Remove-Container-Meta-%s" % c_meta_k_names[0]] = 'x' headers["X-Container-Meta-%s" % c_meta_k_names[1]] = 'b2' self.post_container(account_id, token, 'orig', headers=headers, container=co_name) # Add a some more container self.put_container(account_id, token, 'orig', 'foobar') self.put_container(account_id, token, 'orig', 'foobar1') self.put_container(account_id, token, 'orig', 'foobar2') # Delete one container co_name = clo[1]['name'] self.delete_container(account_id, token, 'orig', co_name) # Re - Start sync process self.swsync.process() # Now verify dest for account, account_id, username in (self.extract_created_a_u_iter( self.created)): # Verify container listing clo = self.list_containers(account_id, self.o_admin_token, 'orig') cld = self.list_containers(account_id, self.d_admin_token, 'dest') self.assertEqual(len(clo), len(cld)) for do in clo: match = [dd for dd in cld if dd['name'] == do['name']] self.assertEqual(len(match), 1) self.assertDictEqual(do, match[0]) # Verify container details clo_c_names = [d['name'] for d in clo] for c_name in clo_c_names: cdo = self.get_container_detail(account_id, self.o_admin_token, 'orig', c_name) cdd = self.get_container_detail(account_id, self.d_admin_token, 'dest', c_name) self.verify_aco_diff(cdo, cdd)
def test_close_none(self): c.http_connection = self.fake_http_connection(200) conn = c.Connection('http://www.test.com', 'asdf', 'asdf') self.assertEqual(conn.http_conn, None) conn.close() self.assertEqual(conn.http_conn, None)
app = Flask(__name__) # Bluemix VCAP services PORT = int(os.getenv('VCAP_APP_PORT', 8080)) # Bluemix object storage name objectstore_container_name = 'cfstorage' # Bluemix credentials for connecting to object storage service bluemix_credential = json.loads( os.environ['VCAP_SERVICES'])['Object-Storage'][0] objectstore = bluemix_credential['credentials'] conn = swiftclient.Connection(key=objectstore['password'], authurl=objectstore['auth_url'] + '/v3', auth_version='3', os_options={ "project_id": objectstore['projectId'], "user_id": objectstore['userId'], "region_name": objectstore['region'] }) # create a default container def containercheck(): for container in conn.get_account()[1]: print container['name'] return False conn.put_container(objectstore_container_name) return True # Returns the lis of files in the specific container
def __init__(self): self._connection = swift_client.Connection() self._containers = {} self._containers_list = [] self._objects = {}
def __init__(self, *args, **kwargs): self.swift_client = swift_client.Connection(*args, **kwargs)
def __init__(self, session): super(SwiftResources, self).__init__(session) self.endpoint = self.session.get_endpoint("object-store") self.token = self.session.token self.conn = swift_client.Connection(session=session.keystone_session)
def _setup_client(self, create=False, container=None): """ Setup a client connection. If create is True and the container doesn't exist, it is created. Sets auth dictionary to create connections. Returns a client connection, metadata tuple or (None, None) on error. """ if container is None: container = self.args.container try: values = self.conf.get_container(container) except ValueError as ex: self.log.error(ex) return (None, None) auth = dict( authurl=self.args.authurl, user=values['username'], key=values['password'], ) if self.args.keystone: try: from keystoneclient.v2_0 import client as _check_for_ksclient except ImportError: sys.exit("auth 2.0 (keystone) requires python-keystoneclient") else: self.log.debug("using auth 2.0 (keystone)") if self.args.keystone_separator not in values['username']: self.log.error("%s: separator not found in %r" % (container, values['username'])) return (None, None) keystone_auth = values['username'].split( self.args.keystone_separator, 1) auth['tenant_name'], auth['user'] = keystone_auth auth['auth_version'] = '2.0' auth['os_options'] = dict( service_type=self.args.keystone_service, endpoint_type=self.args.keystone_endpoint, region_name=self.args.keystone_region, ) self.log.debug("os_options: %r" % auth['os_options']) self.auth = auth cli = client.Connection(**auth) try: headers, _ = cli.get_container(container) except (socket.error, client.ClientException) as ex: if getattr(ex, 'http_status', None) == 404: if create: self.log.warning("%s doesn't exist, will be created" % container) return (cli, dict()) else: self.log.error("%s doesn't exist" % container) else: self.log.error(ex) return (None, None) self.log.debug(headers) meta = getMeta(headers) self.log.debug("Meta: %s" % meta) if not meta: self.log.error("%s hasn't been setup to be used with swiftnbd" % container) return (None, None) return (cli, meta)
def __init__(self, *args, **kwargs): self.swift_client = swift_client.Connection(auth_version='2.0', *args, **kwargs)
log.debug( "create_test_files.py: Hit exception (%s) %s when opening S3 bucket: %s" % (e.error_code, e, args.bucket)) if e.error_code == "NoSuchBucket": s3bucket = s3connection.create_bucket(args.bucket) else: raise # Prepend s3 to the name format so we can differentiate s3 from NFS, etc. if not args.format.startswith('s3'): args.format = 's3_' + args.format log.debug( "create_test_files.py: Opened S3 connection for bucket: %s" % s3bucket.name) elif args.swift_password is not None: swift_client = swift.Connection( SWIFT_AUTH_URL_FMT.format(args.datanode, args.swift_port), args.user, args.swift_password) try: swift_account = swift_client.get_account() if args.bucket in [x['name'] for x in swift_account[-1]]: log.info("Using Swift container: %s" % args.bucket) else: raise ValueError("Invalid Swift container: %s" % args.bucket) except swift.ClientException as e: if e.http_reason == 'Not Found': # A current bug forces adding a default group in order for a user to access the NFS mounted # container. For now simply raise an exception so that the user can create the container # separate from this interface raise ValueError("Invalid Swift container: %s" % args.bucket)
def swift_constructor(version, session, region_name): return swiftclient.Connection(os_options={'region_name': region_name}, session=session)
import os,pyDes import keystoneclient.v3 as keystoneclient import swiftclient.client as swiftclient # User credentials authenticationurl = 'https://identity.open.softlayer.com' + '/v3' projectname = 'object_storage_9d3f0880_aeda_4c31_8763_49bfbdb81262' password = '******' userdomain = '1370727' projectid = 'f375b1a0507e4576a24e5e5780497a7d' userid = 'e32f0a6c156b4ffa88f018812cf0ad46' region = 'dallas' k = pyDes.des("DESCRYPT", pyDes.CBC, "\0\0\0\0\0\0\0\0", pad=None, padmode=pyDes.PAD_PKCS5) # Swift client connection connectionurl = swiftclient.Connection(key=password, authurl=authenticationurl, auth_version='3', os_options={"project_id": projectid, "user_id": userid, "region_name": region}) # Inititate variables container_name = 'Saipriya_Test' file_name = 'Sample_Upload.txt' encodedfile = 'sample_test_saipriya.txt' downloadedfile = 'sample_download.txt' temporaryfile = "tmp.txt" #Upload a file def upload(): verify_status = check_size() if verify_status == 1: connectionurl.put_container(container_name) print("\nContainer %s created successfully." % container_name) #Containers list
def __init__(self, context, db_driver=None): chunk_size_bytes = CONF.backup_swift_object_size sha_block_size_bytes = CONF.backup_swift_block_size backup_default_container = CONF.backup_swift_container enable_progress_timer = CONF.backup_swift_enable_progress_timer super(SwiftBackupDriver, self).__init__(context, chunk_size_bytes, sha_block_size_bytes, backup_default_container, enable_progress_timer, db_driver) if CONF.backup_swift_url is None: self.swift_url = None info = CONF.swift_catalog_info try: service_type, service_name, endpoint_type = info.split(':') except ValueError: raise exception.BackupDriverException(_( "Failed to parse the configuration option " "'swift_catalog_info', must be in the form " "<service_type>:<service_name>:<endpoint_type>")) for entry in context.service_catalog: if entry.get('type') == service_type: # It is assumed that service_types are unique within # the service catalog, so once the correct one is found # it is safe to break out of the loop self.swift_url = entry.get( 'endpoints')[0].get(endpoint_type) break else: self.swift_url = '%s%s' % (CONF.backup_swift_url, context.project_id) if self.swift_url is None: raise exception.BackupDriverException(_( "Could not determine which Swift endpoint to use. This can " "either be set in the service catalog or with the " "cinder.conf config option 'backup_swift_url'.")) if CONF.backup_swift_auth_url is None: self.auth_url = None info = CONF.keystone_catalog_info try: service_type, service_name, endpoint_type = info.split(':') except ValueError: raise exception.BackupDriverException(_( "Failed to parse the configuration option " "'keystone_catalog_info', must be in the form " "<service_type>:<service_name>:<endpoint_type>")) for entry in context.service_catalog: if entry.get('type') == service_type: # It is assumed that service_types are unique within # the service catalog, so once the correct one is found # it is safe to break out of the loop self.auth_url = entry.get( 'endpoints')[0].get(endpoint_type) break else: self.auth_url = CONF.backup_swift_auth_url if self.auth_url is None: raise exception.BackupDriverException(_( "Could not determine which Keystone endpoint to use. This can " "either be set in the service catalog or with the " "cinder.conf config option 'backup_swift_auth_url'.")) LOG.debug("Using swift URL %s", self.swift_url) LOG.debug("Using auth URL %s", self.auth_url) self.swift_attempts = CONF.backup_swift_retry_attempts self.swift_backoff = CONF.backup_swift_retry_backoff LOG.debug('Connect to %s in "%s" mode', CONF.backup_swift_url, CONF.backup_swift_auth) self.backup_swift_auth_insecure = CONF.backup_swift_auth_insecure if CONF.backup_swift_auth == 'single_user': if CONF.backup_swift_user is None: LOG.error(_LE("single_user auth mode enabled, " "but %(param)s not set"), {'param': 'backup_swift_user'}) raise exception.ParameterNotFound(param='backup_swift_user') os_options = {} if CONF.backup_swift_user_domain is not None: os_options['user_domain_name'] = CONF.backup_swift_user_domain if CONF.backup_swift_project_domain is not None: os_options['project_domain_name'] = ( CONF.backup_swift_project_domain ) if CONF.backup_swift_project is not None: os_options['project_name'] = CONF.backup_swift_project self.conn = swift.Connection( authurl=self.auth_url, auth_version=CONF.backup_swift_auth_version, tenant_name=CONF.backup_swift_tenant, user=CONF.backup_swift_user, key=CONF.backup_swift_key, os_options=os_options, retries=self.swift_attempts, starting_backoff=self.swift_backoff, insecure=self.backup_swift_auth_insecure, cacert=CONF.backup_swift_ca_cert_file) else: self.conn = swift.Connection(retries=self.swift_attempts, preauthurl=self.swift_url, preauthtoken=self.context.auth_token, starting_backoff=self.swift_backoff, insecure= ( self.backup_swift_auth_insecure), cacert=CONF.backup_swift_ca_cert_file)
def _connect_to_swift(*args, **kwargs): """ """ swift = swift_client.Connection(*args, **kwargs) return swift
import keystoneclient.v3 as keystoneclient PORT = int(os.getenv('PORT', 80)) app = flask.Flask(__name__) app.debug = True password = '' authurl = '' projectId = '' userId = '' region = '' conn = swiftclient.Connection(key=password, authurl=authurl + '/v3', auth_version='3', os_options={ "project_id": projectId, "user_id": userId, "region_name": region }) @app.route('/', methods=['GET', 'POST']) def root(): return flask.render_template("index.html") @app.route('/createnewcont', methods=['GET', 'POST']) def createnewcont(): container_name = flask.request.form['contname'] conn.put_container(container_name) return flask.render_template("index.html",
def test_reset_stream(self): class LocalContents(object): def __init__(self, tell_value=0): self.already_read = False self.seeks = [] self.tell_value = tell_value def tell(self): return self.tell_value def seek(self, position): self.seeks.append(position) self.already_read = False def read(self, size=-1): if self.already_read: return '' else: self.already_read = True return 'abcdef' class LocalConnection(object): def __init__(self, parsed_url=None): self.reason = "" if parsed_url: self.host = parsed_url.netloc self.port = parsed_url.netloc def putrequest(self, *args, **kwargs): self.send() def putheader(self, *args, **kwargs): return def endheaders(self, *args, **kwargs): return def send(self, *args, **kwargs): raise socket.error('oops') def request(self, *args, **kwargs): return def getresponse(self, *args, **kwargs): self.status = 200 return self def getheader(self, *args, **kwargs): return 'header' def getheaders(self): return {"key1": "value1", "key2": "value2"} def read(self, *args, **kwargs): return '' def local_http_connection(url, proxy=None, cacert=None, insecure=False, ssl_compression=True): parsed = urlparse(url) return parsed, LocalConnection() orig_conn = c.http_connection try: c.http_connection = local_http_connection conn = c.Connection('http://www.example.com', 'asdf', 'asdf', retries=1, starting_backoff=.0001) contents = LocalContents() exc = None try: conn.put_object('c', 'o', contents) except socket.error as err: exc = err self.assertEqual(contents.seeks, [0]) self.assertEqual(str(exc), 'oops') contents = LocalContents(tell_value=123) exc = None try: conn.put_object('c', 'o', contents) except socket.error as err: exc = err self.assertEqual(contents.seeks, [123]) self.assertEqual(str(exc), 'oops') contents = LocalContents() contents.tell = None exc = None try: conn.put_object('c', 'o', contents) except c.ClientException as err: exc = err self.assertEqual(contents.seeks, []) self.assertEqual( str(exc), "put_object('c', 'o', ...) failure " "and no ability to reset contents for reupload.") finally: c.http_connection = orig_conn
def test_instance(self): conn = c.Connection('http://www.test.com', 'asdf', 'asdf') self.assertEquals(conn.retries, 5)
def __init__(self): self.swift_client = swift.Connection(authurl=AUTH_URL, user=USER, key=KEY, auth_version=AUTH_VERION, tenant_name=USER)