def multi_part_upload_manual(bucket_name, item_name, file, length): try: # create client object cos_cli = ibm_boto3.client("s3", ibm_api_key_id=COS_API_KEY_ID, ibm_service_instance_id=COS_RESOURCE_CRN, ibm_auth_endpoint=COS_AUTH_ENDPOINT, config=Config(signature_version="oauth"), endpoint_url=COS_ENDPOINT ) print("Starting multi-part upload for {0} to bucket: {1}\n".format(item_name, bucket_name)) # initiate the multi-part upload mp = cos_cli.create_multipart_upload( Bucket=bucket_name, Key=item_name ) upload_id = mp["UploadId"] # min 5MB part size part_size = 1024 * 1024 * 5 file_size = length #print(file_size) part_count = int(math.ceil(file_size / float(part_size))) data_packs = [] position = 0 part_num = 0 ind = 0 # begin uploading the parts #with open(file_path, "rb") as f: for i in range(part_count): part_num = i + 1 part_size = min(part_size, (file_size - position)) #print("partsize", part_size) print("Uploading to {0} (part {1} of {2})".format(item_name, part_num, part_count)) file_data = file.read(part_size) #print(type(file_data)) #print(len(file_data)) mp_part = cos_cli.upload_part( Bucket=bucket_name, Key=item_name, PartNumber=part_num, Body=file_data, ContentLength=part_size, UploadId=upload_id ) data_packs.append({ "ETag":mp_part["ETag"], "PartNumber":part_num }) position += part_size # complete upload cos_cli.complete_multipart_upload( Bucket=bucket_name, Key=item_name, UploadId=upload_id, MultipartUpload={ "Parts": data_packs } ) print("Upload for {0} Complete!\n".format(item_name)) except ClientError as be: # abort the upload cos_cli.abort_multipart_upload( Bucket=bucket_name, Key=item_name, UploadId=upload_id ) print("Multi-part upload aborted for {0}\n".format(item_name)) print("CLIENT ERROR: {0}\n".format(be)) except Exception as e: print("Unable to complete multi-part upload: {0}".format(e))
def GetObjectStorage(container, filename): cos = ibm_boto3.client('s3',ibm_api_key_id=cos_credentials['apikey'],ibm_service_instance_id=cos_credentials['resource_instance_id'],ibm_auth_endpoint=auth_endpoint,config=Config(signature_version='oauth'),endpoint_url=service_endpoint) obj = cos.get_object(Bucket = container, Key = filename)['Body'] return Response(obj.read(), mimetype='text/plain', status=200)
def test2(container, filename): cos = ibm_boto3.client('s3',ibm_api_key_id=cos_credentials['apikey'],ibm_service_instance_id=cos_credentials['resource_instance_id'],ibm_auth_endpoint=auth_endpoint,config=Config(signature_version='oauth'),endpoint_url=service_endpoint) obj = cos.get_object(Bucket = container, Key = filename)['Body'] b = io.BytesIO(obj.read()) df = pd.read_csv(b) x = df.iloc[:,0].values/1000 y = df.iloc[:,1].values z = np.diff(y) z1 = np.divide(z,np.diff(x)) z = np.insert(z1, 0, 0, axis=0) tools_to_show = "wheel_zoom,pan,box_zoom,reset,save" plot1 = figure(title="Total Capacitance (Ch1)", x_axis_label='Time [s]', y_axis_label='Capacitance [pF]', tools= tools_to_show) plot1.line(x,y,line_width=1.5) plot1.add_tools(HoverTool(tooltips=[("Time", "$x"),("Capcitance", "$y"),])) plot2 = figure(title="Capacitance Change (Ch1)", x_axis_label='Time [s]', y_axis_label='Cap. Change [pF/s]', tools= tools_to_show) plot2.line(x,z1,line_width=1) plot2.add_tools(HoverTool(tooltips=[("Time", "$x"),("Capcitance", "$y"),])) if len(df.columns) == 2: p = gridplot([[plot1,plot2]], plot_width=800, plot_height=400, sizing_mode = "scale_width") else: y2 = df.iloc[:,2].values z2 = np.diff(y2) z3 = np.divide(z2,np.diff(x)) z2 = np.insert(z3, 0, 0, axis=0) plot3 = figure(title="Total Capacitance (Ch2)", x_axis_label='Time [s]', y_axis_label='Capacitance [pF]', tools= tools_to_show) plot3.line(x,y2,line_width=1.5) plot3.add_tools(HoverTool(tooltips=[("Time", "$x"),("Capcitance", "$y"),])) plot4 = figure(title="Capacitance Change (Ch2)", x_axis_label='Time [s]', y_axis_label='Cap. Change [pF/s]', tools= tools_to_show) plot4.line(x,z2,line_width=1) plot4.add_tools(HoverTool(tooltips=[("Time", "$x"),("Capcitance", "$y"),])) p = gridplot([[plot1, plot2], [plot3, plot4]], plot_width=800, plot_height=400, sizing_mode = "scale_width") script, div = components(p) return render_template('plot.html', script = script, div = div)
def main(): parser = argparse.ArgumentParser( epilog= "Note: Create a Service credential in the COS to get the apikey and the resource_instance_id" ) parser.add_argument("-b", "--bucket", help="Bucket name", required=True) parser.add_argument("-r", "--region", help="Bucket's region(e.g: us-south)", required=True) parser.add_argument("-f", "--file", help="Input file to be uploaded", required=True) parser.add_argument("-o", "--object", help="Target object name to be created in the bucket", required=True) parser.add_argument( "-a", "--accesskey", help="Storage Bucket's Access Key ID(not required if --aspera set") parser.add_argument( "-s", "--secret", help="Storage Bucket's Secret Access Key(not required if --aspera set)" ) parser.add_argument( "--aspera", help= "Use aspera client to upload an image(available only in x86 linux environment)", action="store_true", default=False) parser.add_argument("-k", "--apikey", help="IBM Cloud API Key(required if --aspera set)") parser.add_argument( "-i", "--instanceid", help="IBM COS resource instance ID(required if --aspera set)") args = parser.parse_args() if args.aspera: print("Aspera option is set..") if platform.machine() != "x86_64" or platform.system() != "Linux": print( "--aspera option is supported only on distro: Linux, architecture: x86_64" ) sys.exit(2) import ibm_boto3 from ibm_botocore.client import Config cos = ibm_boto3.client( service_name='s3', ibm_api_key_id=args.apikey, ibm_service_instance_id=args.instanceid, ibm_auth_endpoint='https://iam.cloud.ibm.com/identity/token', config=Config(signature_version='oauth'), endpoint_url='https://s3.' + args.region + '.cloud-object-storage.appdomain.cloud/', ) upload_object_aspera(cos, args.bucket, args.file, args.object) else: print( "Aspera option is not set, continuing with normal boto3 client..") import boto3 s3 = boto3.client('s3', endpoint_url='https://s3.' + args.region + '.cloud-object-storage.appdomain.cloud/', region_name=args.region, aws_access_key_id=args.accesskey, aws_secret_access_key=args.secret) with open(args.file, "rb") as f: s3.upload_fileobj(f, args.bucket, args.object)
def GetObjStoContainerInfo(container): cos = ibm_boto3.client('s3',ibm_api_key_id=cos_credentials['apikey'],ibm_service_instance_id=cos_credentials['resource_instance_id'],ibm_auth_endpoint=auth_endpoint,config=Config(signature_version='oauth'),endpoint_url=service_endpoint) bucs = [] for data in cos.list_objects(Bucket = container)['Contents']: data['downloadURL'] = thehost + "/" + container + "/" + data['Key'] bucs.append(data) return render_template('table.html', objs = bucs, container = container)
def main(args): audio = args['__ow_body'] headers = args["__ow_headers"] audio_extension = headers['content_type'] unique_filename = audio.split('=')[1] print("Unique Filename : ", unique_filename) cos_download = ibm_boto3.resource( "s3", ibm_api_key_id=COS_API_KEY_ID_DOWNLOAD, ibm_service_instance_id=COS_RESOURCE_CRN_DOWNLOAD, ibm_auth_endpoint=COS_AUTH_ENDPOINT, config=Config(signature_version="oauth"), endpoint_url=COS_ENDPOINT) try: file = cos_download.Object("imagecompressionuploads", unique_filename).get() except ClientError as be: print("CLIENT ERROR: {0}\n".format(be)) return {'statusCode': 400, 'body': be} except Exception as e1: print("Unable to retrieve file contents: {0}".format(e1)) return {'statusCode': 400, 'body': e1} audio_data = file["Body"].read() if audio_extension == 'mp3': with open('input.mp3', 'wb') as f: f.write(audio_data) sound = pydub.AudioSegment.from_mp3("input.mp3") sound.export("inputfile.wav", format="wav") else: with open('inputfile.wav', 'wb') as f: f.write(audio_data) with contextlib.closing(wave.open('inputfile.wav', 'rb')) as spf: print("TYPE : ", type(spf)) sampleRate = spf.getframerate() ampWidth = spf.getsampwidth() nChannels = spf.getnchannels() nFrames = spf.getnframes() # Extract Raw Audio from multi-channel Wav File signal = spf.readframes(nFrames * nChannels) spf.close() channels = interpret_wav(signal, nFrames, nChannels, ampWidth, True) # get window size fqRatio = (cutOffFrequency / sampleRate) N = int(math.sqrt(0.196196 + fqRatio**2) / fqRatio) # Use moviung average (only on first channel) filt = run_mean(channels[0], N).astype(channels.dtype) wav_file = wave.open("compressed.wav", "w") wav_file.setparams( (1, ampWidth, sampleRate, nFrames, spf.getcomptype(), spf.getcompname())) wav_file.writeframes(filt.tobytes('C')) wav_file.close() if audio_extension == 'mp3': sound1 = pydub.AudioSegment.from_wav("compressed.wav") sound1.export("compressed.mp3", format="mp3") try: cos_upload = ibm_boto3.resource( "s3", ibm_api_key_id=COS_API_KEY_ID_UPLOAD, ibm_service_instance_id=COS_RESOURCE_CRN_UPLOAD, ibm_auth_endpoint=COS_AUTH_ENDPOINT, config=Config(signature_version="oauth"), endpoint_url=COS_ENDPOINT) print("Starting file transfer for {0} to bucket: {1}\n".format( unique_filename, "imagecompressiondownloads")) part_size = 1024 * 1024 * 5 file_threshold = 1024 * 1024 * 40 transfer_config = ibm_boto3.s3.transfer.TransferConfig( multipart_threshold=file_threshold, multipart_chunksize=part_size) if audio_extension == 'mp3': with open("compressed.mp3", "rb") as file_data: cos_upload.Object("imagecompressiondownloads", unique_filename).upload_fileobj( Fileobj=file_data, Config=transfer_config) else: with open("compressed.wav", "rb") as file_data: cos_upload.Object("imagecompressiondownloads", unique_filename).upload_fileobj( Fileobj=file_data, Config=transfer_config) print("Transfer for {0} Complete!\n".format(unique_filename)) except ClientError as be: print("CLIENT ERROR: {0}\n".format(be)) return {'statusCode': 400, 'body': be} except Exception as e: print("Unable to complete multi-part upload: {0}".format(e)) return {'statusCode': 400, 'body': e} return {'statusCode': 200, 'body': "Compression Successfully"}
# on_connect we should subscribe to the topic to pull down the images def on_connect(client, userdata, flags, rc): print("connected to mqtt") client.subscribe(topic) # on_message save the payload to the cloud bucket def on_message(client, userdata, msg): print("message received") png_name = "face-" + str(round(time.time()))[:-2] + ".png" resource.Bucket(name=bucket).put_object(Key=png_name, Body=msg.payload) print("wrote to bucket") # setting up the connection to the bucket resource = ibm_boto3.resource( 's3', ibm_api_key_id=credentials['apikey'], ibm_service_instance_id=credentials['resource_instance_id'], ibm_auth_endpoint=auth_endpoint, config=Config(signature_version='oauth'), endpoint_url=service_endpoint) # setting up mqtt connection and defining what to do on_connect and on_message client = mqtt.Client() client.on_connect = on_connect client.connect(mqtt_host) client.on_message = on_message client.loop_forever()
def resource( self, service_name, region_name=None, api_version=None, use_ssl=True, verify=None, endpoint_url=None, aws_access_key_id=None, aws_secret_access_key=None, aws_session_token=None, ibm_api_key_id=None, ibm_service_instance_id=None, ibm_auth_endpoint=None, auth_function=None, token_manager=None, config=None, ): """ Create a resource service client by name. :type service_name: string :param service_name: The name of a service, e.g. 's3'. You can get a list of available services via :py:meth:`get_available_resources`. :type region_name: string :param region_name: The name of the region associated with the client. A client is associated with a single region. :type api_version: string :param api_version: The API version to use. By default, ibm_botocore will use the latest API version when creating a client. You only need to specify this parameter if you want to use a previous API version of the client. :type use_ssl: boolean :param use_ssl: Whether or not to use SSL. By default, SSL is used. Note that not all services support non-ssl connections. :type verify: boolean/string :param verify: Whether or not to verify SSL certificates. By default SSL certificates are verified. You can provide the following values: * False - do not validate SSL certificates. SSL will still be used (unless use_ssl is False), but SSL certificates will not be verified. * path/to/cert/bundle.pem - A filename of the CA cert bundle to uses. You can specify this argument if you want to use a different CA cert bundle than the one used by ibm_botocore. :type endpoint_url: string :param endpoint_url: The complete URL to use for the constructed client. Normally, ibm_botocore will automatically construct the appropriate URL to use when communicating with a service. You can specify a complete URL (including the "http/https" scheme) to override this behavior. If this value is provided, then ``use_ssl`` is ignored. :type aws_access_key_id: string :param aws_access_key_id: The access key to use when creating the client. This is entirely optional, and if not provided, the credentials configured for the session will automatically be used. You only need to provide this argument if you want to override the credentials used for this specific client. :type aws_secret_access_key: string :param aws_secret_access_key: The secret key to use when creating the client. Same semantics as aws_access_key_id above. :type aws_session_token: string :param aws_session_token: The session token to use when creating the client. Same semantics as aws_access_key_id above. :type ibm_api_key_id: string :param ibm_api_key_id: IBM api key used for IAM authentication. :type ibm_service_instance_id: string :param ibm_service_instance_id: Service Instance ID used for PUT bucket and GET service requests. :type ibm_auth_endpoint: string :param ibm_auth_endpoint: URL used for IAM authentication. :type token_manager: TokenManager :param token_manager: custom token manager to use. :type auth_function: function :param auth_function: function that does custom authentication and returns json with token, refresh token, expiry time and token type. :type config: ibm_botocore.client.Config :param config: Advanced client configuration options. If region_name is specified in the client config, its value will take precedence over environment variables and configuration values, but not over a region_name value passed explicitly to the method. If user_agent_extra is specified in the client config, it overrides the default user_agent_extra provided by the resource API. See `ibm_botocore config documentation <https://botocore.readthedocs.io/en/stable/reference/config.html>`_ for more details. :return: Subclass of :py:class:`~ibm_boto3.resources.base.ServiceResource` """ try: resource_model = self._loader.load_service_model( service_name, 'resources-1', api_version) except UnknownServiceError: available = self.get_available_resources() has_low_level_client = (service_name in self.get_available_services()) raise ResourceNotExistsError(service_name, available, has_low_level_client) except DataNotFoundError: # This is because we've provided an invalid API version. available_api_versions = self._loader.list_api_versions( service_name, 'resources-1') raise UnknownAPIVersionError(service_name, api_version, ', '.join(available_api_versions)) if api_version is None: # Even though ibm_botocore's load_service_model() can handle # using the latest api_version if not provided, we need # to track this api_version in ibm_boto3 in order to ensure # we're pairing a resource model with a client model # of the same API version. It's possible for the latest # API version of a resource model in ibm_boto3 to not be # the same API version as a service model in ibm_botocore. # So we need to look up the api_version if one is not # provided to ensure we load the same API version of the # client. # # Note: This is relying on the fact that # loader.load_service_model(..., api_version=None) # and loader.determine_latest_version(..., 'resources-1') # both load the same api version of the file. api_version = self._loader.determine_latest_version( service_name, 'resources-1') # Creating a new resource instance requires the low-level client # and service model, the resource version and resource JSON data. # We pass these to the factory and get back a class, which is # instantiated on top of the low-level client. if config is not None: if config.user_agent_extra is None: config = copy.deepcopy(config) config.user_agent_extra = 'Resource' else: config = Config(user_agent_extra='Resource') client = self.client( service_name, region_name=region_name, api_version=api_version, use_ssl=use_ssl, verify=verify, endpoint_url=endpoint_url, aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key, aws_session_token=aws_session_token, ibm_api_key_id=ibm_api_key_id, ibm_service_instance_id=ibm_service_instance_id, ibm_auth_endpoint=ibm_auth_endpoint, auth_function=auth_function, token_manager=token_manager, config=config, ) service_model = client.meta.service_model # Create a ServiceContext object to serve as a reference to # important read-only information about the general service. service_context = ibm_boto3.utils.ServiceContext( service_name=service_name, service_model=service_model, resource_json_definitions=resource_model['resources'], service_waiter_model=ibm_boto3.utils.LazyLoadedWaiterModel( self._session, service_name, api_version), ) # Create the service resource class. cls = self.resource_factory.load_from_definition( resource_name=service_name, single_resource_json_definition=resource_model['service'], service_context=service_context, ) return cls(client=client)
def sconnect_dashboard(): cur = mysql.connection.cursor() #global file_exist global COS_ENDPOINT if request.method == 'POST': #print('request received') # check if the post request has the file part try: cur.execute( "SELECT sr,pkname,version,server_type,uploaded_by,os,state,state_by,msil_comment,date_time FROM sconnect ORDER BY sr DESC" ) file_exist = cur.fetchall() aNG = 0 aGG = 0 iNG = 0 iGG = 0 for each in file_exist: if each[5] == 'android': if each[6] == 'GG': aGG += 1 elif each[6] == 'NG': aNG += 1 elif each[5] == 'iOS': if each[6] == 'GG': iGG += 1 elif each[6] == 'NG': iNG += 1 except Exception as e: return show_notification('/sconnectdashboard', e.message) if request.form['btn'] == 'Upload' and g.user: if 'file' not in request.files: flash('No file part') return show_notification('/sconnectdashboard', "oops, No file part") file = request.files['file'] file.seek(0, os.SEEK_END) size = file.tell() # seek to its beginning, so you might save it entirely file.seek(0) # if user does not select file, browser also # submit an empty part without filename if file.filename == '': flash('No selected file') return show_notification('/sconnectdashboard', "oops, No file selected!") if file and allowed_file(file.filename): filename = secure_filename(file.filename) #print(filename, [temp[1] for temp in file_exist]) if filename in [temp[1] for temp in file_exist]: return show_notification( '/sconnectdashboard', "oops, file name already exists!") elif request.form['version'] in [ temp[2] for temp in file_exist ]: return show_notification('/sconnectdashboard', "oops, version already exists!") elif request.form['version'].strip() == '': return show_notification( '/sconnectdashboard', "oops, version should not be empty!") #file_exist = file_exist+ ((3, filename,request.form['version'], request.form['server'], session['user'],request.form['os'],'pending','pending'),) #for i in file_exist: # print(i) else: try: #COS_ENDPOINT = 'https://' + 's3.jp-tok.cloud-object-storage.appdomain.cloud' multi_part_upload_manual("suzukiconnect", filename, file, size) try: timestamp = datetime.now().strftime( '%Y-%m-%d %H:%M:%S') cur.execute( "SELECT name FROM username WHERE id = '{}';". format(g.user)) name = cur.fetchall()[0][0] #print(name) cur.execute( "INSERT INTO sconnect (pkname, date_time, uploaded_by, os, version, state, state_by, msil_comment,server_type) VALUES('{}','{}','{}','{}','{}','pending','pending','Package not checked yet!','{}');" .format(filename, str(timestamp), name, request.form['os'], request.form['version'], request.form['server'])) mysql.connection.commit() return show_notification('/sconnectdashboard', 'Upload successful!!') except Exception as e: print(e) return show_notification( '/sconnectdashboard', 'Upload unsuccessful!!<br>' + str(e)) except Exception as e: #print(e) return show_notification( '/sconnectdashboard', 'Upload unsuccessful!!<br>' + str(e)) return show_notification('/sconnectdashboard', 'oops, unsupported file format!') elif request.form['btn'] == 'Feedback' and g.user: #print(request.form['feedback'].splitlines()) #print(request.form['pkname']) #return ('<script>alert("'+'\\n'.join(request.form['feedback'].splitlines())+'")</script>') #UPDATE sconnect SET state='NG', msil_comment='bad app', state_by='Probhakar' WHERE pkname='Sconnect_iOS_test_1_0_1_6.zip'; try: cur.execute( "SELECT name FROM username WHERE id = '{}';".format( g.user)) name = cur.fetchall()[0][0] cur.execute( "UPDATE sconnect SET state='{}', msil_comment='{}', state_by='{}' WHERE pkname='{}';" .format(request.form['status'], request.form['feedback'], name, request.form['pkname'])) mysql.connection.commit() return redirect(url_for('sconnect_dashboard')) except Exception as e: #print(e) return show_notification('/sconnectdashboard', 'Update unsuccessful!!<br>' + str(e)) elif request.form['btn'] == 'Download' and g.user: #print(request.form) #print(request.form['pdkname']) #return (request.form['pdkname']) try: #fobj = get_item("suzukiconnect",request.form['pdkname']) #return send_file(fobj.read(), attachment_filename=request.form['pdkname'], as_attachment=True) #return show_notification('ok') try: # create client object cos = ibm_boto3.resource( "s3", ibm_api_key_id=COS_API_KEY_ID, ibm_service_instance_id=COS_RESOURCE_CRN, ibm_auth_endpoint=COS_AUTH_ENDPOINT, config=Config(signature_version="oauth"), endpoint_url=COS_ENDPOINT) file = cos.Object("suzukiconnect", request.form['pdkname']).get() #file = cos.Bucket(bucket_name).Object('my-key') #print(file) #dd = file["Body"].read() #print(type(dd)) print(file["Body"]) #print(len(file["Body"].read())) #return send_file(dd, attachment_filename='sample.zip', as_attachment=True) '''with open(item_name+'.zip', "wb") as file_data: file_data.write(dd) file_data.close()''' #return send_file(file["Body"].read(), attachment_filename=item_name, as_attachment=True) #fileobj = file["Body"] #return fileobj w = FileWrapper(BytesIO(file['Body'].read())) #print('under w') try: return Response(w, mimetype="application/zip", direct_passthrough=True, headers={ "Content-Disposition": "attachment;filename={}".format( request.form['pdkname']) }) except Exception as e: print("upload excp: {0}".format(e)) except ClientError as be: print("CLIENT ERROR: {0}\n".format(be)) except Exception as e: print("Unable to retrieve file contents: {0}".format(e)) except Exception as e: #print(e) return show_notification( '/sconnectdashboard', 'Download unsuccessful!!<br>' + str(e)) return show_notification('/sconnectdashboard', 'ok') else: if g.user: #cur = mysql.connection.cursor() cur.execute( 'SELECT responsiblefor FROM username WHERE id = {};'.format( g.user)) permission = cur.fetchall() try: cur.execute( "SELECT sr,pkname,version,server_type,uploaded_by,os,state,state_by,msil_comment,date_time FROM sconnect ORDER BY sr DESC" ) file_exist = cur.fetchall() aNG = 0 aGG = 0 iNG = 0 iGG = 0 for each in file_exist: if each[5] == 'android': if each[6] == 'GG': aGG += 1 elif each[6] == 'NG': aNG += 1 elif each[5] == 'iOS': if each[6] == 'GG': iGG += 1 elif each[6] == 'NG': iNG += 1 except Exception as e: return show_notification('/sconnectdashboard', str(e)) if (permission[0][0] == 'all'): #print(timestamp) #try: #cur.execute("INSERT INTO sconnect (pkname, date_time, uploaded_by, os, version, state, state_by, msil_comment) VALUES('Suzuki_Connect_ios_1_6_1.zip','{}','Minal','iOS','1.6.1','NG','Probhakar','1.bad app\\n2.goodapp');".format(str(timestamp))) #mysql.connection.commit() #print(cur.fetchall()) #print(file_exist) #except Exception as e: #print('unable to insert\\n',e.message) return render_template('dasboard_sconnect_admin.html', file_exist=file_exist, android=[aGG, aNG], apple=[iGG, iNG]) elif (permission[0][0] == 'sconnect'): return render_template('dasboard_sconnect_ibm.html', file_exist=file_exist, android=[aGG, aNG], apple=[iGG, iNG]) elif (permission[0][0] == 'sps'): #return render_template('dasboard_sconnect_ibm.html', file_exist=file_exist, android=[10,8],apple=[8,7]) return redirect(url_for('sps_dashboard')) elif (permission[0][0] == 'spd'): #return render_template('dasboard_sconnect_ibm.html', file_exist=file_exist, android=[10,8],apple=[8,7]) return redirect(url_for('spd_dashboard')) else: return show_notification('/sconnectdashboard', 'not admin') return redirect(url_for('index'))
def __init__(self): """ Description : Constructor to class FeedbackLoop, initializes the connection to COS Parameters : 1. self """ constants = Constants() self.client = ibm_boto3.client(service_name='s3',ibm_api_key_id=constants.API_KEY,ibm_auth_endpoint=constants.AUTH_ENDPOINT,config=Config(signature_version='oauth'),endpoint_url=constants.COS_API_ENDPOINT) column_names = ['Timestamp','CPU','Instances'] util = COSUtils() self.bucketName = 'getcsvdata' self.data = {} self.score = {} self.modelSelector = {} self.response = {} for region in ['Dallas', 'London', 'Tokyo']: self.initialDataset = 'initial_'+region+'.csv' self.fetchedDataset = 'data_'+region+'.csv' self.data[region] = util.get_dataframe(column_names, self.bucketName, self.initialDataset) self.data[region].columns = column_names self.data[region] = self.convertTimestampToFloat(self.data[region]) self.modelSelector[region] = RegressionSelector() self.score[region] = self.getScore(self.data[region], region)['Best Score'] logging.info("Score:" + str(self.score[region])) logging.debug("Response:") logging.debug(self.response)
class Tools: cos_download = ibm_boto3.resource( "s3", ibm_api_key_id=settings.COS_API_KEY_ID, ibm_service_instance_id=settings.COS_INSTANCE_CRN, config=Config(signature_version="oauth"), endpoint_url=settings.COS_ENDPOINT) cos_upload = ibm_boto3.client( "s3", ibm_api_key_id=settings.COS_API_KEY_ID, ibm_service_instance_id=settings.COS_INSTANCE_CRN, config=Config(signature_version="oauth"), endpoint_url=settings.COS_ENDPOINT) data_ejercicio = { 'Cicla elíptica': 'clica_eliptica', #Listo 'Cicla estática': 'cicla_estatica', #Listo 'Caminadora': 'caminadora', #Listo 'Caminar': 'caminar', #Listo 'Cicla': 'cicla', #Listo 'Correr': 'correr', #Listo 'Patinar': 'patinar', #Listo 'Nadar': 'nadar', #Listo 'Saltar cuerda': 'cuerda', #Listo 'Baile aeróbico': 'baile_aerobico', #Listo 'Aeróbicos acuáticos': 'aerobico_acuatico', #Listo 'Yoga': 'yoga' #Yoga } @staticmethod def checkPassword(password): rgx = re.compile(r'(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?!.* ).{8,}') return rgx.fullmatch(password) and len(password) >= 8 @staticmethod def auditar(request, descripcion): try: audit = Auditoria() audit.fecha = Tools.getToday(time=True) audit.descripcion = descripcion audit.direccion_ip = Tools.get_client_ip(request) audit.correo_usuario = request.user.username audit.tipo_usuario = request.user.groups.first() audit.save() except Exception as e: print(e) pass @staticmethod def getAuditoria(): return Auditoria.objects.all().order_by('-fecha') @staticmethod def get_client_ip(request): x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR') if x_forwarded_for: ip = x_forwarded_for.split(',')[0] else: ip = request.META.get('REMOTE_ADDR') return ip @staticmethod def getEjercicioImg(ejercicio): try: return Tools.data_ejercicio[ejercicio] except: return 'defecto' @staticmethod def getUser(email): try: return User.objects.all().get(username=email) except: return None @staticmethod def recuperarClave(user): new_pass = Tools.get_random_string(10) user.set_password(new_pass) user.save() Tools.sendEmailUserPasswd(user, new_pass) @staticmethod def getPercentage(x, dec): return round(x * 100, dec) @staticmethod def getToday(time=False): date = datetime.datetime.now(pytz.timezone(settings.TIME_ZONE)) if time: return date else: return date.date() @staticmethod def getTodayStr(str_date): dat = datetime.datetime.strptime(str_date, '%Y-%m-%dT%H:%M:%S.%fZ') utc_now = pytz.utc.localize(dat) pst_now = utc_now.astimezone(pytz.timezone(settings.TIME_ZONE)) return pst_now @staticmethod def get_random_string(length): letters = string.ascii_lowercase result_str = ''.join(random.choice(letters) for i in range(length)) return result_str @staticmethod def sendEmail(user, subject, body): try: email = EmailMessage(subject, body, from_email=settings.DEFAULT_FROM_EMAIL, to=[user.username]) email.content_subtype = "html" email.send() return True except: return False @staticmethod def sendEmailRaw(email, subject, body): try: email = EmailMessage(subject, body, from_email=settings.DEFAULT_FROM_EMAIL, to=[email]) email.content_subtype = "html" email.send() return True except: return False @staticmethod def sendEmailUserAdded(user, password): subject = 'Bienvenido - Fynex' body = f''' <div style="width:100%;background-color:#D5D5D5;padding-top: 30px;padding-bottom: 30px;"> <div style="width: 500px;margin: auto auto;background-color:white;border-style: double;"> <img src="https://fynexapp.herokuapp.com/static/images/banner.jpg" style="width: 100%" align="left"> <br> <br><br><br><br> <h1><strong style="color:#1C54F7"><font font="verdana"><i><b><p align="center">Bienvenido {user.first_name}</b></i></font></strong></h1></p> <div style="width:100%;text-align:center"> <img src="https://i.pinimg.com/originals/7e/69/ec/7e69eca344ca1465da94d698ded08e8e.gif" width="200"> </div> <h3><p align="center"> Su correo es: <br> <font font="verdana"><i><b>{user.username}</b></i></font> </p></h3> <h3><p align="center"> La contraseña temporal que ha sido asignada es : <br> <strong style="color:#5178EC"><i>{password}</i></strong> </p></h3> <h3 align="center">Cuando inicie sesión podrá modificar la contraseña a la que desee.<h3> <p align="center"><a href="https://fynexapp.herokuapp.com"><button style="border-radius: 12px;font-size: 25px;background-color: #125570;color:white">Ingresar</button></a> </p> <br> </div> </div> ''' res = Tools.sendEmail(user, subject, body) return res @staticmethod def sendEmailUserPasswd(user, new_pass): subject = 'Recuperar contraseña' body = f''' <div style="width:100%;background-color:#D5D5D5;padding-top: 30px;padding-bottom: 30px;"> <div style="width: 500px;margin: auto auto;background-color:white;border-style: double;"> <img src="https://fynexapp.herokuapp.com/static/images/banner.jpg" style="width: 100%" align="left"> <br> <br><br><br><br> <h1><strong style="color:#1C54F7"><font font="verdana"><i><b><p align="center">Recuperar contraseña</b></i></font></strong></h1></p> <div style="width:100%;text-align:center"> <img src="https://i.pinimg.com/originals/7e/69/ec/7e69eca344ca1465da94d698ded08e8e.gif" width="200"> </div> <h3><p align="center"> Su correo es: <br> <font font="verdana"><i><b>{user.username}</b></i></font> </p></h3> <h3><p align="center"> La nueva contraseña de su cuenta es : <br> <strong style="color:#5178EC"><i>{new_pass}</i></strong> </p></h3> <p align="center"><a href="https://fynexapp.herokuapp.com"><button style="border-radius: 12px;font-size: 25px;background-color: #125570;color:white">Ingresar</button></a> </p> <br> </div> </div> ''' res = Tools.sendEmail(user, subject, body) return res @staticmethod def sendEmailUserMsg(paciente, paciente_sender): subject = 'Nuevo mensaje' url = 'https://fynexapp.herokuapp.com/Paciente/chat' tipo = f'médico <strong>{paciente.medico.user.first_name}</strong>' enviar_a = paciente.user if paciente_sender: tipo = f'paciente <strong>{paciente.user.first_name}</strong>' url = f'https://fynexapp.herokuapp.com/Medico/{paciente.id}/chat' enviar_a = paciente.medico.user body = f''' <div style="width:100%;background-color:#D5D5D5;padding-top: 30px;padding-bottom: 30px;"> <div style="width: 500px;margin: auto auto;background-color:white;border-style: double;"> <img src="https://fynexapp.herokuapp.com/static/images/banner.jpg" style="width: 100%" align="left"> <br> <br><br><br><br> <h1><strong style="color:#1C54F7"><font font="verdana"><i><b><p align="center">Tienes una nueva notificación</b></i></font></strong></h1></p> <div style="width:100%;text-align:center"> <img src="https://i.pinimg.com/originals/7e/69/ec/7e69eca344ca1465da94d698ded08e8e.gif" width="200"> </div> <h3><p align="center"> Tienes un nuevo mensaje de tu ''' + tipo + '''.</p></h3> <br> <p align="center"><a href="''' + url + '''"><button style="border-radius: 12px;font-size: 25px;background-color: #125570;color:white">Ir al chat</button></a></p> <br> </div> </div> ''' res = Tools.sendEmail(enviar_a, subject, body) return res @staticmethod def sendEmailNewRecommendationFood(paciente): subject = 'Solicitud de nueva recomendación nutricional' body = f''' <div style="width:100%;background-color:#D5D5D5;padding-top: 30px;padding-bottom: 30px;"> <div style="width: 500px;margin: auto auto;background-color:white;border-style: double;"> <img src="https://fynexapp.herokuapp.com/static/images/banner.jpg" style="width: 100%" align="left"> <br> <br><br><br><br> <h1><strong style="color:#1C54F7"><font font="verdana"><i><b><p align="center">Nueva recomendación</b></i></font></strong></h1></p> <div style="width:100%;text-align:center"> <img src="https://i.pinimg.com/originals/7e/69/ec/7e69eca344ca1465da94d698ded08e8e.gif" width="200"> </div> <h3><p align="center"> El paciente {paciente.user.first_name} desea una nueva recomendación nutricional </p></h3> <p align="center"><a href="https://fynexapp.herokuapp.com/Medico/{paciente.id}/nutrition_recommendations"><button style="border-radius: 12px;font-size: 25px;background-color: #125570;color:white">Ingresar</button></a></p> <br> </div> </div> ''' res = Tools.sendEmail(paciente.medico.user, subject, body) return res @staticmethod def sendEmailNewRecommendationExercise(paciente): subject = 'Solicitud de nueva recomendación de ejercicio' body = f''' <div style="width:100%;background-color:#D5D5D5;padding-top: 30px;padding-bottom: 30px;"> <div style="width: 500px;margin: auto auto;background-color:white;border-style: double;"> <img src="https://fynexapp.herokuapp.com/static/images/banner.jpg" style="width: 100%" align="left"> <br> <br><br><br><br> <h1><strong style="color:#1C54F7"><font font="verdana"><i><b><p align="center">Nueva recomendación</b></i></font></strong></h1></p> <div style="width:100%;text-align:center"> <img src="https://i.pinimg.com/originals/7e/69/ec/7e69eca344ca1465da94d698ded08e8e.gif" width="200"> </div> <h3><p align="center"> El paciente {paciente.user.first_name} desea una nueva recomendación de actividad física </p></h3> <p align="center"><a href="https://fynexapp.herokuapp.com/Medico/{paciente.id}/exercise_recommendations"><button style="border-radius: 12px;font-size: 25px;background-color: #125570;color:white">Ingresar</button></a></p> <br> </div> </div> ''' res = Tools.sendEmail(paciente.medico.user, subject, body) return res @staticmethod def sendInformacion(email): subject = 'Fynex' body = f''' <div style="width:100%;background-color:#D5D5D5;padding-top: 30px;padding-bottom: 30px;"> <div style="width: 500px;margin: auto auto;background-color:white;border-style: double;"> <img src="https://fynexapp.herokuapp.com/static/images/banner.jpg" style="width: 100%" align="left"> <br> <br><br><br><br> <h1><strong style="color:#1C54F7"><font font="verdana"><i><b><p align="center">Fynex</b></i></font></strong></h1></p> <div style="width:100%;text-align:center"> <img src="https://i.pinimg.com/originals/7e/69/ec/7e69eca344ca1465da94d698ded08e8e.gif" width="200"> </div> <p align="center"> Fynex es una aplicación que ayuda a médicos y pacientes que participan en un proceso de prevención y tratamiento de enfermedades basadas en trastornos alimenticios, ofreciendo planes nutricionales y planes de actividad física, generados a partir de inteligencia artificial y modelos estadísticos predictivos. Además, ofrece un seguimiento personalizado a los pacientes, al disponer de funcionalidades gráficas y de comunicación como un chat de mensajería.<br><br> Para mayor información, comunicarse con <a href="mailto:[email protected]">[email protected]</a> </p> <p align="center"><a href="https://fynexapp.herokuapp.com"><button style="border-radius: 12px;font-size: 25px;background-color: #125570;color:white">Ingresar</button></a></p> <br> </div> </div> ''' res = Tools.sendEmailRaw(email, subject, body) return res @staticmethod def sendEmailNuevoExamen(paciente, examen): subject = 'Nuevo resultado de examen' body = f''' <div style="width:100%;background-color:#D5D5D5;padding-top: 30px;padding-bottom: 30px;"> <div style="width: 500px;margin: auto auto;background-color:white;border-style: double;"> <img src="https://fynexapp.herokuapp.com/static/images/banner.jpg" style="width: 100%" align="left"> <br> <br><br><br><br> <h1><strong style="color:#1C54F7"><font font="verdana"><i><b><p align="center">Nuevo resultado</b></i></font></strong></h1></p> <div style="width:100%;text-align:center"> <img src="https://i.pinimg.com/originals/7e/69/ec/7e69eca344ca1465da94d698ded08e8e.gif" width="200"> </div> <h3><p align="center"> El paciente {paciente.user.first_name} ha subido el resultado del examen: <b>{examen.nombre}</b> </p></h3> <p align="center"><a href="https://fynexapp.herokuapp.com/Medico/{paciente.id}/examenes"><button style="border-radius: 12px;font-size: 25px;background-color: #125570;color:white">Ver examenes</button></a></p> <br> </div> </div> ''' res = Tools.sendEmail(paciente.medico.user, subject, body) return res
encr = Path(wrkdir + '/credentials.aes').read_text() key = base64.b64decode(key) iv = base64.b64decode(iv) encr = base64.b64decode(encr) cipher = AES.new(key, AES.MODE_CBC, iv) creds = json.loads(unpad(cipher.decrypt(encr), AES.block_size).decode('utf-8')) COS_ENDPOINT = creds['url_endpoint'] COS_BUCKET_LOCATION = creds['bucket_name'] COS_API_KEY_ID = creds['apikey'] COS_INSTANCE_CRN = creds['resource_instance_id'] IMAGE_FILE = sys.argv[1] cos = ibm_boto3.resource("s3", ibm_api_key_id=COS_API_KEY_ID, ibm_service_instance_id=COS_INSTANCE_CRN, config=Config(signature_version="oauth"), endpoint_url=COS_ENDPOINT) def upload_file(bucket_name, item_name, file_path): part_size = 1024 * 1024 * 5 file_threshold = 1024 * 1024 * 5 cos_cli = ibm_boto3.client("s3", ibm_api_key_id=COS_API_KEY_ID, ibm_service_instance_id=COS_INSTANCE_CRN, config=Config(signature_version="oauth"), endpoint_url=COS_ENDPOINT ) transfer_config = ibm_boto3.s3.transfer.TransferConfig( multipart_threshold=file_threshold, multipart_chunksize=part_size
def Up_to_storage(lname, file_name): with open('./credentials.json') as data_file: credentials = json.load(data_file) print("Service credential:") print(json.dumps(credentials, indent=2)) print("") print("Connecting to COS...") # Rquest detailed enpoint list endpoints = requests.get(credentials.get('endpoints')).json() #import pdb; pdb.set_trace() # Obtain iam and cos host from the the detailed endpoints iam_host = (endpoints['identity-endpoints']['iam-token']) cos_host = (endpoints['service-endpoints']['cross-region']['us']['public'] ['us-geo']) api_key = credentials.get('apikey') service_instance_id = credentials.get('resource_instance_id') # Constrict auth and cos endpoint auth_endpoint = "https://" + iam_host + "/oidc/token" service_endpoint = "https://" + cos_host print("Creating client...") # Get bucket list cos = ibm_boto3.client('s3', ibm_api_key_id=api_key, ibm_service_instance_id=service_instance_id, ibm_auth_endpoint=auth_endpoint, config=Config(signature_version='oauth'), endpoint_url=service_endpoint) # Call COS to list current buckets response = cos.list_buckets() # Get a list of all bucket names from the response buckets = [bucket['Name'] for bucket in response['Buckets']] # Print out the bucket list print("Current Bucket List:") print(json.dumps(buckets, indent=2)) print("---") result = database.get_bucket_name(lname) print "result = ", result print("Creating a new bucket and uploading an object...") print file_name if len(result) == 0: bucket_name = 'bucket' + str(random.randint(100, 99999999)) # Create a bucket cos.create_bucket(Bucket=bucket_name) # Upload a file cos.upload_file('./' + file_name, bucket_name, file_name) # Call COS to list current buckets response = cos.list_buckets() # Get a list of all bucket names from the response buckets = [bucket['Name'] for bucket in response['Buckets']] # Print out the bucket list print("New Bucket List:") print(json.dumps(buckets, indent=2)) print("---") else: bucket_name = result cos.upload_file('./' + file_name, bucket_name, file_name) os.remove('./' + file_name) # Call COS to list current objects response = cos.list_objects(Bucket=bucket_name) # Get a list of all object names from the response objects = [object['Key'] for object in response['Contents']] # Print out the object list print("Objects in %s:" % bucket_name) print(json.dumps(objects, indent=2)) return bucket_name
def help(): #reading voice command with open(join(dirname(__file__), './.', 'help.mp3'), 'rb') as audio_file: speech_recognition_results = speech_to_text.recognize( audio=audio_file, content_type='audio/mp3' ).get_result() x = speech_recognition_results["results"][0]["alternatives"][0]["transcript"] print('The received voice command is: %s\n' %x) # Create resource cos = ibm_boto3.resource("s3", ibm_api_key_id=COS_API_KEY_ID, ibm_service_instance_id=COS_RESOURCE_CRN, ibm_auth_endpoint=COS_AUTH_ENDPOINT, config=Config(signature_version="oauth"), endpoint_url=COS_ENDPOINT ) print("Connection to object storage is successful.\n") #Provide CloudantDB credentials such as username,password and url client = Cloudant("", ", url="") client.connect() #Provide your database name database_name = "sample" my_database = client.create_database(database_name) if my_database.exists(): print("Database creation is successful.\n") def multi_part_upload(bucket_name, item_name, file_path): try: print("Starting file transfer for {0} to cloud bucket: {1}\n".format(item_name, bucket_name)) # set 5 MB chunks part_size = 1024 * 1024 * 5 # set threadhold to 15 MB file_threshold = 1024 * 1024 * 15 # set the transfer threshold and chunk size transfer_config = ibm_boto3.s3.transfer.TransferConfig( multipart_threshold=file_threshold, multipart_chunksize=part_size ) # the upload_fileobj method will automatically execute a multi-part upload # in 5 MB chunks for all files over 15 MB with open(file_path, "rb") as file_data: cos.Object(bucket_name, item_name).upload_fileobj( Fileobj=file_data, Config=transfer_config ) print("Transfer of the image file {0} is complete!\n".format(item_name)) except ClientError as be: print("CLIENT ERROR: {0}\n".format(be)) except Exception as e: print("Unable to complete multi-part upload: {0}\n".format(e)) #Reading the first frame/image of the video. video=cv2.VideoCapture(0) #capturing the first frame check,frame=video.read() gray=cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) picname=datetime.datetime.now().strftime("%y-%m-%d-%H-%M-%S") cv2.imwrite(picname+".jpg",frame) print("Image captured and saved in local storage\n") multi_part_upload("bishwa-bucket-2020", picname+".jpg", picname+".jpg") json_document={"link":COS_ENDPOINT+"/"+"bishwa-bucket-2020"+"/"+picname+".jpg"} new_document = my_database.create_document(json_document) # Checking that the document exists in the database. if new_document.exists(): print("Document creation is successful.\n") #Sending alert message to mobile r = requests.get('') #print(r.status_code) print("Sending alert message to mobile is successful.\n") #Sending Buzzer Alert print("Buzzer alert sent.\n") if platform == "linux" or platform == "linux2" or platform == "darwin": duration = 1 # Set duration to 1 second freq = 2500 # Set frequency To 2500 He os.system('play -nq -t alsa synth {} sine {}'.format(duration, freq)) elif platform == "win32": frequency = 2500 # Set frequency To 2500 Hertz duration = 1000 # Set duration To 1000 ms == 1 second winsound.Beep(frequency, duration) #release the camera video.release() print("camera released")
def main(args): audio = args['__ow_body'] headers = args["__ow_headers"] audio_extension = headers['content_type'] unique_filename = audio.split('=')[1] print("Unique Filename : ",unique_filename) cos_download = ibm_boto3.resource("s3", ibm_api_key_id=COS_API_KEY_ID_DOWNLOAD, ibm_service_instance_id=COS_RESOURCE_CRN_DOWNLOAD, ibm_auth_endpoint=COS_AUTH_ENDPOINT, config=Config(signature_version="oauth"), endpoint_url=COS_ENDPOINT) try: file = cos_download.Object("imagecompressionuploads",unique_filename).get() except ClientError as be: print("CLIENT ERROR: {0}\n".format(be)) return { 'statusCode': 400, 'body': be } except Exception as e1: print("Unable to retrieve file contents: {0}".format(e1)) return { 'statusCode': 400, 'body': e1 } audio_data = file["Body"].read() if audio_extension == 'mp3': with open('input.mp3','wb') as f: f.write(audio_data) sound = pydub.AudioSegment.from_mp3("input.mp3") sound.export("inputfile.wav", format="wav") else: with open('inputfile.wav','wb') as f: f.write(audio_data) samplerate, tabulasa = wavfile.read('inputfile.wav') start = samplerate * 14 # 10 seconds in end = start + samplerate * 10 # 5 second duration tabulasa_left = tabulasa[:,0] _, _, reconstructed = pca_reduce(tabulasa_left, 64, 256) scaled = np.int16(reconstructed/np.max(np.abs(reconstructed)) * 32767) write('compressed.wav', 44100, scaled) if audio_extension == 'mp3': sound1 = pydub.AudioSegment.from_wav("compressed.wav") sound1.export("compressed.mp3", format="mp3") try: cos_upload = ibm_boto3.resource("s3",ibm_api_key_id=COS_API_KEY_ID_UPLOAD,ibm_service_instance_id=COS_RESOURCE_CRN_UPLOAD,ibm_auth_endpoint=COS_AUTH_ENDPOINT,config=Config(signature_version="oauth"),endpoint_url=COS_ENDPOINT) print("Starting file transfer for {0} to bucket: {1}\n".format(unique_filename, "imagecompressiondownloads")) part_size = 1024 * 1024 * 5 file_threshold = 1024 * 1024 * 40 transfer_config = ibm_boto3.s3.transfer.TransferConfig( multipart_threshold=file_threshold, multipart_chunksize=part_size ) if audio_extension == 'mp3': with open("compressed.mp3", "rb") as file_data: cos_upload.Object("imagecompressiondownloads", unique_filename).upload_fileobj( Fileobj=file_data, Config=transfer_config ) else: with open("compressed.wav", "rb") as file_data: cos_upload.Object("imagecompressiondownloads", unique_filename).upload_fileobj( Fileobj=file_data, Config=transfer_config ) print("Transfer for {0} Complete!\n".format(unique_filename)) except ClientError as be: print("CLIENT ERROR: {0}\n".format(be)) return { 'statusCode': 400, 'body': be } except Exception as e: print("Unable to complete multi-part upload: {0}".format(e)) return { 'statusCode': 400, 'body': e } return { 'statusCode': 200, 'body': "Compression Successfully" }
def query_tiles_with_spark(spark, cos_api_key, cos_instance_crn, cos_endpoint_url, bucket, image_meta_prefix, layers, start_date, end_date, bbox, download_dir, search_buffer_in_degrees=2, selected_dates=None, merge_tiles=True, merge_by_date=True, force_bbox=True): if isinstance(layers, str): layers = [layers] if isinstance(selected_dates, str) or isinstance(selected_dates, int): selected_dates = [selected_dates] # Get COS client for downloading tiles import ibm_boto3 from ibm_botocore.client import Config cos_client = ibm_boto3.client("s3", ibm_api_key_id=cos_api_key, ibm_service_instance_id=cos_instance_crn, endpoint_url=cos_endpoint_url, config=Config(signature_version="oauth")) # Config Spark for reading meta data sc = spark.sparkContext sc._jsc.hadoopConfiguration().set('fs.cos.myCos.iam.api.key', cos_api_key) sc._jsc.hadoopConfiguration().set('fs.cos.myCos.iam.service.id', cos_instance_crn) sc._jsc.hadoopConfiguration().set('fs.cos.myCos.endpoint', cos_endpoint_url) # Get meta data spark.read.parquet('cos://{}.myCos/{}'.format( bucket, image_meta_prefix)).createOrReplaceTempView('temp_view') expanded_bbox = expand_bbox(bbox, search_buffer_in_degrees) if selected_dates is None: date_query_part = 'date between {} AND {}'.format(start_date, end_date) else: date_query_part = 'date in {}'.format(tuple(selected_dates)) if expanded_bbox[2] >= expanded_bbox[0]: lon_query_part = 'lon between {} AND {}'.format( expanded_bbox[0], expanded_bbox[2]) else: lon_query_part = '(lon between {} AND 180 OR lon between -180 AND {})'.format( expanded_bbox[0], expanded_bbox[2]) df = spark.sql(""" SELECT * FROM temp_view WHERE layer in {0} AND {1} AND lat between {2[1]} AND {2[3]} AND {3} """.format(tuple(layers), date_query_part, expanded_bbox, lon_query_part)).toPandas() if not os.path.exists(download_dir): os.makedirs(download_dir) if merge_tiles: from pyrip.image import merge merged_images = [] for layer in layers: tmp_df = df[df['layer'] == layer].sort_values( ['date', 'lat', 'lon']) if merge_by_date: dates = tmp_df['date'].unique() for date in dates: tiles = [] with tempfile.TemporaryDirectory() as tmpdir: for url in tmp_df[tmp_df['date'] == date]['url']: bucket, key = url.replace('cos://', '').split('/', 1) tile = download(cos_client, bucket, key, tmpdir) tiles.append(tile) if bbox[2] > bbox[0]: merged_image = os.path.join( download_dir, layer, str(date), 'merged.tif') if force_bbox: merge(tiles, merged_image, bbox) else: merge(tiles, merged_image) merged_images.append(merged_image) # If the given bbox spans over anti-meridian, we have to merge into two parts. Note that this # will also overwrite the value of force_bbox and set it to true. else: merged_image_1 = os.path.join( download_dir, layer, str(date), 'merged_1.tif') merged_image_2 = os.path.join( download_dir, layer, str(date), 'merged_2.tif') merge(tiles, merged_image_1, (bbox[0], bbox[1], 180, bbox[3])) merge(tiles, merged_image_2, (-180, bbox[1], bbox[2], bbox[3])) merged_images.extend( [merged_image_1, merged_image_2]) else: tiles = [] with tempfile.TemporaryDirectory() as tmpdir: for url in tmp_df['url']: bucket, key = url.replace('cos://', '').split('/', 1) tile = download(cos_client, bucket, key, tmpdir) tiles.append(tile) if bbox[2] > bbox[0]: merged_image = os.path.join(download_dir, layer, 'merged.tif') if force_bbox: merge(tiles, merged_image, bbox) else: merge(tiles, merged_image) merged_images.append(merged_image) # If the given bbox spans over anti-meridian, we have to merge into two parts. Note that this will # also overwrite the value of force_bbox and set it to true. else: merged_image_1 = os.path.join(download_dir, layer, 'merged_1.tif') merged_image_2 = os.path.join(download_dir, layer, 'merged_2.tif') merge(tiles, merged_image_1, (bbox[0], bbox[1], 180, bbox[3])) merge(tiles, merged_image_2, (-180, bbox[1], bbox[2], bbox[3])) merged_images.extend([merged_image_1, merged_image_2]) return merged_images else: tiles = [] for _, row in df.iterrows(): layer = row['layer'] date = row['date'] url = row['url'] bucket, key = url.replace('cos://', '').split('/', 1) tile = download(cos_client, bucket, key, os.path.join(download_dir, layer, str(date))) tiles.append(tile) return tiles
def createCOSClient(args): """ Create a ibm_boto3.client using the connectivity information contained in args. :param args: action parameters :type args: dict :return: An ibm_boto3.client :rtype: ibm_boto3.client """ # if a Cloud Object Storage endpoint parameter was specified # make sure the URL contains the https:// scheme or the COS # client cannot connect if args.get('endpoint') and not args['endpoint'].startswith('https://'): args['endpoint'] = 'https://{}'.format(args['endpoint']) # set the Cloud Object Storage endpoint endpoint = args.get('endpoint', 'https://s3.us.cloud-object-storage.appdomain.cloud') # extract Cloud Object Storage service credentials cos_creds = args.get('__bx_creds', {}).get('cloud-object-storage', {}) # set Cloud Object Storage API key api_key_id = \ args.get('apikey', args.get('apiKeyId', cos_creds.get('apikey', os.environ .get('__OW_IAM_NAMESPACE_API_KEY') or ''))) if not api_key_id: # fatal error; it appears that no Cloud Object Storage instance # was bound to the action's package return None # set Cloud Object Storage instance id svc_instance_id = args.get( 'resource_instance_id', args.get('serviceInstanceId', cos_creds.get('resource_instance_id', ''))) if not svc_instance_id: # fatal error; it appears that no Cloud Object Storage instance # was bound to the action's package return None ibm_auth_endpoint = args.get('ibmAuthEndpoint', 'https://iam.cloud.ibm.com/identity/token') # Create a Cloud Object Storage client using the provided # connectivity information cos = ibm_boto3.client('s3', ibm_api_key_id=api_key_id, ibm_service_instance_id=svc_instance_id, ibm_auth_endpoint=ibm_auth_endpoint, config=Config(signature_version='oauth'), endpoint_url=endpoint) # Return Cloud Object Storage client return cos
class Audio(models.Model): name = models.CharField( unique=True, max_length=64, verbose_name='Nombre del archivo', help_text="Nombre del archivo en el que se guarda el audio generado.") format_choices = [('mp3', 'mp3'), ('wav', 'wav'), ('flac', 'flac'), ('ogg', 'ogg')] format = models.CharField(choices=format_choices, max_length=16, verbose_name='Formato', default="mp3") created_at = models.DateTimeField(auto_now=True, verbose_name="Fecha de Creación") status_choice = (('Aprobado', 'Aprobado'), ('En revisión', 'En revisión')) status = models.CharField(choices=status_choice, default="En revisión", max_length=64, verbose_name='Estado') reference = models.CharField(max_length=256, null=True, blank=True, verbose_name='Referencia') description = models.CharField(max_length=256, null=True, blank=True, verbose_name='Descripción') text = models.CharField(max_length=4096, verbose_name='Texto') created_by = models.ForeignKey(User, null=True, related_name='audios') # Cloud Object Storage Bucket # Los datos, como la api key, se obtienen desde la consola de IBM cloud endpoint_url = 'https://s3.us-south.objectstorage.softlayer.net' bucket_name = 'prueba-text-to-speech' bucket = ibm_boto3.resource( 's3', ibm_api_key_id='yJoxUb3FKZBYaxzTTKJFLHmfiztVJCy81YHFJ1OVQjjp', ibm_service_instance_id= 'crn:v1:bluemix:public:cloud-object-storage:global:a/ce9e974530c83fa4891688cf239a0c9d:c8082bb2-3916-4649-83fc-fdad7999b2c1::', ibm_auth_endpoint='https://iam.bluemix.net/oidc/token', config=Config(signature_version='oauth'), endpoint_url=endpoint_url).Bucket(bucket_name) def __str__(self): return "%s.%s" % (self.name, self.format) def save(self, *args, **kwargs): super(Audio, self).save(*args, **kwargs) def createAudio(self): rawAudio = text_to_specch.getRawAudio(self.text, "audio/%s" % self.format) audioFile = io.BytesIO(rawAudio) self.bucket.upload_fileobj(audioFile, str(self)) # print(self.audio_file, bool(self.audio_file)) # if bool(self.audio_file): # os.remove(self.audio_file.path) # self.audio_file.save("%s.%s"%(self.name, self.format), ContentFile(rawAudio)) def delete(self, *args, **kwargs): a = self.bucket.Object(str(self)).delete() print(">>>", a) super(Audio, self).delete(*args, **kwargs) @property def url(self): return "%s/%s/%s" % (self.endpoint_url, self.bucket_name, str(self)) def getAudioContent(self): file = '/tmp/%s' % str(self) self.bucket.Object(str(self)).download_file(Filename=file) with open(file, 'rb') as data: content = data.read() print(len(data.read())) os.remove(file) return content
def get_ibm_cos_client(config): return ibm_boto3.client(service_name='s3', ibm_api_key_id=config['ibm_cos']['api_key'], config=Config(signature_version='oauth'), endpoint_url=config['ibm_cos']['endpoint'])
def GetObjStoreInfo(): cos = ibm_boto3.client('s3',ibm_api_key_id=cos_credentials['apikey'],ibm_service_instance_id=cos_credentials['resource_instance_id'],ibm_auth_endpoint=auth_endpoint,config=Config(signature_version='oauth'),endpoint_url=service_endpoint) bucs = [] for bucket in cos.list_buckets()['Buckets']: bucket['accessURL'] = thehost + "/" + bucket['Name'] bucs.append(bucket) return render_template('main.html', cons = bucs)
import ibm_boto3 from ibm_botocore.client import Config, ClientError # Constants for IBM COS values COS_ENDPOINT = "https://s3.us-east.cloud-object-storage.appdomain.cloud" COS_API_KEY_ID = input('Enter the API key to use this service: ' ) # eg "W00YiRnLW4a3fTjHB-oiB-2ySfTrFBIQQWanc--P3byk" COS_AUTH_ENDPOINT = "https://iam.cloud.ibm.com/identity/token" COS_RESOURCE_CRN = "crn:v1:bluemix:public:cloud-object-storage:global:a/f3149af813cc4e73937b29b8b01a89e0:b9e56194-35fa-46a4-a64d-ce1c017dfd73::" # Create resource cos = ibm_boto3.resource("s3", ibm_api_key_id=COS_API_KEY_ID, ibm_service_instance_id=COS_RESOURCE_CRN, ibm_auth_endpoint=COS_AUTH_ENDPOINT, config=Config(signature_version="oauth"), endpoint_url=COS_ENDPOINT) # Gets the names of the buckets in the COS (there's only one for now for demo purposes) def get_buckets(): print("Retrieving list of buckets") try: buckets = cos.buckets.all() for bucket in buckets: return bucket.name except ClientError as be: print("CLIENT ERROR: {0}\n".format(be)) except Exception as e: print("Unable to retrieve list buckets: {0}".format(e))
"https://*****:*****@3cb8b46e-599a-4d23-9530-49e397948844-bluemix.cloudantnosqldb.appdomain.cloud" ) client.connect() database_name = "firef1" my_database = client.create_database(database_name) if my_database.exists(): print(f"'{database_name}' Successfully created.") cos = ibm_boto3.resource("s3", ibm_api_key_id=COS_API_KEY_ID, ibm_service_instance_id=COS_INSTANCE_CRN, ibm_auth_endpoint=COS_AUTH_ENDPOINT, config=Config(signature_version="cffvu"), endpoint_url=COS_ENDPOINT) def vis(picname): with open('./' + picname + '.jpg', 'rb') as images_file: classes = visual_recognition.classify( images_file, threshold='0.8', classifier_ids='DefaultCustomModel_1363490324').get_result() print(classes) a = classes['images'][0]['classifiers'][0]['classes'][0]['class'] return a def multi_part_upload(bucket_name, item_name, file_path):