def test_client_passes_through_arguments(self): boto3_wasabi.DEFAULT_SESSION = self.Session() boto3_wasabi.client('sqs', region_name='us-west-2', verify=False) boto3_wasabi.DEFAULT_SESSION.client.assert_called_with( 'sqs', region_name='us-west-2', verify=False)
def test_client_uses_existing_session(self, setup_session): boto3_wasabi.DEFAULT_SESSION = self.Session() boto3_wasabi.client('sqs') self.assertFalse(setup_session.called, 'setup_default_session should not have been called') self.assertTrue(boto3_wasabi.DEFAULT_SESSION.client.called, 'Default session client method not called')
def test_client_creates_default_session(self, setup_session): boto3_wasabi.DEFAULT_SESSION = None boto3_wasabi.client('sqs') self.assertTrue(setup_session.called, 'setup_default_session not called') self.assertTrue(boto3_wasabi.DEFAULT_SESSION.client.called, 'Default session client method not called')
def __init__(self, *args, **kwargs): # Always work on a copy of meta, otherwise we would affect other # instances of the same subclass. self.meta = self.meta.copy() # Create a default client if none was passed if kwargs.get('client') is not None: self.meta.client = kwargs.get('client') else: self.meta.client = boto3_wasabi.client(self.meta.service_name) # Allow setting identifiers as positional arguments in the order # in which they were defined in the ResourceJSON. for i, value in enumerate(args): setattr(self, '_' + self.meta.identifiers[i], value) # Allow setting identifiers via keyword arguments. Here we need # extra logic to ignore other keyword arguments like ``client``. for name, value in kwargs.items(): if name == 'client': continue if name not in self.meta.identifiers: raise ValueError('Unknown keyword argument: {0}'.format(name)) setattr(self, '_' + name, value) # Validate that all identifiers have been set. for identifier in self.meta.identifiers: if getattr(self, identifier) is None: raise ValueError( 'Required parameter {0} not set'.format(identifier))
def upload_to_wasabi(f, file_key): WASABI_ACCESS_KEY = '' WASABI_SECRET_KEY = '' WASABI_BUCKET = 'nithya.bl' # Open the file as readable body = open(f, 'rb') # Start the boto3 client that points to Wasabi's S3 endpoints. s3 = boto3_wasabi.client('s3', region_name='us-east-1', aws_access_key_id=WASABI_ACCESS_KEY, aws_secret_access_key=WASABI_SECRET_KEY) # Upload your file upload_data = s3.put_object(Bucket=WASABI_BUCKET, Key=file_key, Body=body, ContentType='image/jpg') # Close the file; just a good practice. body.close() tmp = upload_data["ResponseMetadata"] sc = tmp["HTTPStatusCode"] if sc == 200: return True else: return False
def get_client(): WASABI_ACCESS_KEY = '' WASABI_SECRET_KEY = '' WASABI_BUCKET = 'nithya.bl' s3 = boto3_wasabi.client('s3', region_name='us-east-1', aws_access_key_id=WASABI_ACCESS_KEY, aws_secret_access_key=WASABI_SECRET_KEY) return s3
def file_write(self, filename=EXAMPLE_TIFF): """ Takes in a filename from the working directory as an argument and uploads it to wasabi :param filename: :return: """ # Start the boto3 client that points to Wasabi's S3 endpoints. s3 = boto3_wasabi.client('s3', aws_access_key_id=self._accessKey, aws_secret_access_key=self._privateKey) # Open the file as readable with open(filename, 'rb') as body: # Upload your file upload_data = s3.put_object(Bucket=self._bucketName, Key=filename, Body=body, ContentType='text/plain') # Print the uploaded data return upload_data
def file_read(self, filetoretrieve=EXAMPLE_TIFF, newfilename='Downloadedfile.tif'): """ Retrieves a file from wasabi and puts it in the current working directory :param filetoretrieve: :param newfilename: :return: """ # Start the boto3 client that points to Wasabi's S3 endpoints. s3 = boto3_wasabi.client('s3', aws_access_key_id=self._accessKey, aws_secret_access_key=self._privateKey) # Open the file as write-able with open(newfilename, 'wb') as body: output = s3.download_fileobj(self._bucketName, filetoretrieve, body) # Print to signify that script ran print('...Downloaded') return output
def test_docs_generated(): """Verify we can generate the appropriate docs for all services""" botocore_session = botocore.session.get_session() session = boto3_wasabi.Session(region_name='us-east-1') for service_name in session.get_available_services(): generated_docs = ServiceDocumenter(service_name, session=session).document_service() generated_docs = generated_docs.decode('utf-8') client = boto3_wasabi.client(service_name, 'us-east-1') # Check that all of the services have the appropriate title yield (_assert_has_title, generated_docs, client) # Check that all services have the client documented. yield (_assert_has_client_documentation, generated_docs, service_name, client) # If the client can paginate, make sure the paginators are documented. try: paginator_model = botocore_session.get_paginator_model( service_name) yield (_assert_has_paginator_documentation, generated_docs, service_name, client, sorted(paginator_model._paginator_config)) except DataNotFoundError: pass # If the client has waiters, make sure the waiters are documented if client.waiter_names: waiter_model = botocore_session.get_waiter_model(service_name) yield (_assert_has_waiter_documentation, generated_docs, service_name, client, waiter_model) # If the service has resources, make sure the service resource # is at least documented. if service_name in session.get_available_resources(): resource = boto3_wasabi.resource(service_name, 'us-east-1') yield (_assert_has_resource_documentation, generated_docs, service_name, resource)
def __init__(self, access_key_id=None, secret_access_key=None): self.s3 = boto3_wasabi.client( 's3', region_name='us-east-1', aws_access_key_id=os.environ["KEY_ID"], aws_secret_access_key=os.environ["SECRET_KEY"])
def file_delete(self, filetoretdelete=EXAMPLE_TIFF): s3 = boto3_wasabi.client('s3', aws_access_key_id=self._accessKey, aws_secret_access_key=self._privateKey) output = s3.delete_object(Bucket=self._bucketName, Key=filetoretdelete) print(output)