def upload_image_file(file): """ Upload the user-uploaded file to Google Cloud Storage and retrieve its publicly-accessible URL. """ if not file: return None public_url = storage.upload_file(file.read(), file.filename, file.content_type) current_app.logger.info("Uploaded file %s as %s.", file.filename, public_url) return public_url
def download_and_upload_image(src, dst_filename): """ Downloads an image file and then uploads it to Google Cloud Storage, essentially re-hosting the image in GCS. Returns the public URL of the image in GCS """ r = requests.get(src) if not r.status_code == 200: return return storage.upload_file(r.content, dst_filename, r.headers.get("content-type", "image/jpeg"))
def upload_image_file(file): """ Upload the user-uploaded file to Local Storage and retrieve its publicly-accessible URL. """ if not file: return None public_url = storage.upload_file(file, file.filename, file.content_type, current_app.config['UPLOAD_FOLDER']) current_app.logger.info("Uploaded file %s as %s.", file.filename, public_url) return public_url
def download_and_upload_image(src, dst_filename): """ Downloads an image file and then uploads it to Google Cloud Storage, essentially re-hosting the image in GCS. Returns the public URL of the image in GCS """ r = requests.get(src) if not r.status_code == 200: return return storage.upload_file(r.content, dst_filename, r.headers.get('content-type', 'image/jpeg'))
def view_csv(id): report = get_model().read(id) if not report['csvUrl']: upload_csv(id) else: credentials = GoogleCredentials.get_application_default() service = discovery.build('bigquery', 'v2', credentials=credentials) projectId = current_app.config['PROJECT_ID'] datasetId = 'Gift' tableId = id bq_request = service.tabledata().list(projectId=projectId, datasetId=datasetId, tableId=tableId) data = bq_request.execute() rowCollection = [] headerRow = [] # __file__ refers to the file settings.py APP_ROOT = os.path.dirname(os.path.abspath(__file__)) # refers to application_top APP_CSV = os.path.join(APP_ROOT, 'csv/placeholder.csv') # csvFile = file('/csv/test.csv') csvFile = open(APP_CSV, 'w+') a = csv.writer(csvFile) for row in data['rows']: rowVal = [] for cell in row['f']: rowVal.append(cell['v']) rowCollection.append(rowVal) headerRow.append(['organizationName_ext', 'organizationName_int', 'organizationID', 'amountOver', 'amountUnder', 'label', 'giftType', 'donorType', 'date']) a.writerows(headerRow) a.writerows(rowCollection) # Save file to cloud via blob public_url = storage.upload_file( csvFile.read(), report['reportName'] + '.csv', 'text/csv') csvFile.close() csvRender = rowCollection csvResponse = '' return render_template("csv.html", report=report, csvResponse=csvResponse, csvRender=csvRender, public_url=public_url)
def upload_csv(id): report = get_model().read(id) csvResponse = '' SCHEMA = [ SchemaField('organizationName_ext', 'STRING', mode='required'), SchemaField('organizationName_int', 'STRING', mode='required'), SchemaField('organizationID', 'INTEGER', mode='required'), SchemaField('amountOver', 'INTEGER', mode='required'), SchemaField('amountUnder', 'INTEGER', mode='required'), SchemaField('label', 'STRING', mode='required'), SchemaField('giftType', 'STRING', mode='required'), SchemaField('donorType', 'STRING', mode='required'), SchemaField('date', 'STRING', mode='required'), ] client = bigquery.Client(current_app.config['PROJECT_ID']) dataset = client.dataset('Gift') # dataset.create() # API request table = dataset.table(id, SCHEMA) if request.method == 'POST': csvFile = request.files.get('csv') if table.exists(): table.delete() table.create() else: table.create() # Save file to cloud via blob public_url = storage.upload_file( csvFile.read(), csvFile.filename, csvFile.content_type) response = urllib2.urlopen(public_url) table.upload_from_file(response, source_format='CSV', skip_leading_rows=1) data = report data['csvUrl'] = public_url get_model().update(data, id) report = get_model().read(id) csvResponse = 'The CSV was for report ' + report['reportName'] + ' was successfully added to Greasebelt. ' + \ 'Please click here to request a sync with Fundtracker.' return view_csv(id)