def allowed_methods(self, bucket, methods=[]): client = self.state.session.client("s3") self.rb_logger.info(f"Retrieve Client Hanlder: {client}") try: response = client.get_bucket_cors(Bucket=bucket) obj = response['CORSRules'][0] aws_methods = obj['AllowedMethods'] assert set(aws_methods) == set(methods) except botocore.exceptions.ClientError as e: raise KeywordError(e)
def delete_file(self, bucket, key): """ Delete File Requires: @param ```bucket``` which is the bucket name: @param: ```key``` which is the bucket location/path name. Example: | Delete File | bucket | key | """ client = self.state.session.client('s3') try: response = client.delete_object(Bucket=bucket, Key=key) if response['ResponseMetadata']['HTTPStatusCode'] != 204: raise ContinuableError(f"Error {response['ResponseMetadata']}") except botocore.exceptions.ClientError as e: raise KeywordError(e)
def key_should_exist(self, bucket, key): """ Key Should Exist Description: Checks to see if the file on the s3 bucket exist. If so, the keyword will not fail. Requires: @param ```bucket``` which is the bucket name: @param: ```key``` which is the bucket location/path name. Example: | Key Should Exist | bucket | key | """ client = self.state.session.client("s3") res = client.head_object(Bucket=bucket, Key=key) try: if res['ResponseMetadata']['HTTPStatusCode'] == 404: raise KeywordError("Key: {}, does not exist".format(key)) except botocore.exceptions.ClientError as e: raise ContinuableError(e.response['Error']) return True
def download_file_from_s3(self, bucket, key, path): """ Downloads File from S3 Bucket Requires: @param ```bucket``` which is the bucket name: @param: ```key``` which is the bucket location/path name. @param: ```path``` which is the local path of file. Example: | Download File | bucket | path | key | """ client = self.state.session.client('s3') try: client.download_file(bucket, key, path) except botocore.exceptions.ClientError as e: if e.response['Error']['Code'] == '404': raise KeywordError(f"Keyword: {bucket} does not exist") else: self.rb_logger.debug(e)
def upload_file(self, bucket, key, path): """ Uploads File from S3 Bucket Requires: @param ```bucket``` which is the bucket name: @param: ```key``` which is the bucket location/path name. @param: ```path``` which is the local path of file. Example: | Upload File | bucket | path | key | """ client = self.state.session.client('s3') try: client.upload_file(path, bucket, key) response = client.head_object(Bucket=bucket, Key=key) self.rb_logger.info(response) except botocore.exceptions.S3 as e: if e.response['Error']: raise KeywordError(e) else: raise ContinuableError(e)
def key_should_not_exist(self, bucket, key): """ Verifies Key on S3 Bucket Does Not Exist Description: Checks to see if the file on the s3 bucket exist. If so, the keyword will fail. Requires: @param ```bucket``` which is the bucket name: @param: ```key``` which is the bucket location/path name. Example: | Key Should Not Exist | bucket | key | """ client = self.state.session.client('s3') self.rb_logger.info(f"Retrieve Client Hanlder: {client}") try: res = client.head_object(Bucket=bucket, Key=key) if res['ResponseMetadata']['HTTPStatusCode'] == 200: raise KeywordError("Key: {}, already exists".format(key)) except botocore.exceptions.ClientError as e: # noqa if e.response['ResponseMetadata']['HTTPStatusCode'] != 404: raise ContinuableError("Error at: {} with data {}".format( e.operation_name, e.response))
def list_objects(self, bucket, prefix=""): """ List Objects Requires: @param: ```bucket``` which is the bucket name Optional: @param: ```prefix``` which limits the response to keys that begin with the specified prefix. Example: | List Objects | bucket | key | """ client = self.state.session.client('s3') try: response = client.list_objects( Bucket=bucket, Prefix=prefix ) if 'Contents' in response: return [key['Key'] for key in response['Contents']] else: return [] except botocore.exceptions.ClientError as e: raise KeywordError(e)