How to store Gzipped content in Aerospike (cache)? - python

I'm using aerospike python client here. I don't find any problem while storing plain string in cache because when i retrieve the key, I get the same string I saved when I do conn.get(key)
import aerospike
CONN_AEROSPIKE_CONFIG = {'hosts': [('127.0.0.1', 3000)]}
conn = aerospike.client(CONN_AEROSPIKE_CONFIG).connect()
key = ('namspace_name', 'set_name', 'key_name')
value = "some big string"
conn.put(key, {'value': value})
If I want to save gzipped content in the place of value, I don't find any error but I'm unable to get back the exact content.
from gzip import GzipFile
from io import BytesIO
def compress_string(s):
zbuf = BytesIO()
with GzipFile(mode='wb', compresslevel=6, fileobj=zbuf, mtime=0) as zfile:
zfile.write(s)
return zbuf.getvalue()
put_value = compress_string(value)
conn.put(key, {'value': put_value})
_, _, get_value = conn.get(key)
I checked printing values of put_value, get_value. They don't match, and I need gzipped content, because my content exceeds 1MB and I need gzipped content only. Please guide me where I'm doing wrong.
I know that we can break the content into smaller chunks for storing and concatenate after getting them, but again I need to Gzip the content after getting the data. So I thought why not store directly Gzipped content, but that doesn't seems to be working for me. Any leads are appreceated, Thank you.

If put_value is a string, it will be truncated on the first \0 character that is encountered. Try converting put_value to a bytearray before sending it down:
conn.put(key, {'value': bytearray(put_value,"utf-8")})

Related

Pyrebase sending profile pic in json

I am creating user accounts using prebase auth.create_user_with_email_and_password. Then I am storing the users' data in firebase realtime database. db.child("users").push("data") where data= {"name": name, "email" : email, "password": password, "picture": picture}.
here picture is; picture = request.files['file']
But I am not able to send picture alongwith other data of user, image can not be sent in json object.
How can we upload picture, there are some solutions to upload image data but I want to send it alngwith other data so it may get stored with the other attributes of user data.
I would suggest two approches you could use:
Solution 1:
You could store your images in file system in any directory (i.e img_dir) and renaming it to ensure the name is unique. (i usually use a timestamp prefix) (myimage_20210101.jpg).Now you can store this name in a DataBase. Then, while generating the JSON, you pull this filename ,generating a complete URL (http://myurl.com/img_dir/myimage_20210101.jpg) and then you can insert it into the JSON.
Solution 2:
Encode the image with Base 64 Encoding and decode it while fetching.
Remember to convert to string first by calling .decode(), since you can't JSON-serialize a bytes without knowing its encoding.
That's because base64.b64encode returns bytes, not strings.
import base64
encoded_= base64.b64encode(img_file.read()).decode('utf-8')
How to save an encoded64 image?
my_picture= 'data:image/jpeg;base64,/9j/4AAQSkZJRgAB............'
You'll need to decode the picture from base64 first, and then save it to a file:
import base64
# Separate the metadata from the image data
head, data = my_picture.split(',', 1)
# Decode the image data
plain_image = base64.b64decode(data)
# Write the image to a file
with open('image.jpg', 'wb') as f:
f.write(plain_image)

decoding base64 encoded image in Flask

My Flask app is receives base64 encoded images like so
`b'"{\\"image\\": \\"/9j/4AAQSkZJRgABAQEASABIAAD/4gxYSUNDX1BST0ZJTEUAAQEAAAxITGlubwIQAABtbnRyU...opHNf//Z\\", \\"id\\": \\"e3ad9809-b84c-57f1-bd03-a54e25c59bcc\\"}"'
I have tried a number of ways but I cannot seem to get the image bytes for decoding. If I json.loads this it becomes a string. If I treat it as a bytes dictionary it doesn't take the key and asks it to be an integer which will not work of course. I have tried many things, this is just one other which doesn't work. Any help is appreciated, Thanks.
def main():
try:
p = randint(100, 200)
image = request.data
jsonResponse = json.loads(image.decode('utf-8'))
im = base64.decode(jsonResponse)
print(im)
with open(f'Image{p}.jpg', 'wb') as f:
f.write(im)
If you use request context (from flask import request), then you can access the sended data as a python dictionary using the request.json attribute instead of the request.data.
To get the dictionary as you wish from the example you provided, a double call to json.loads solved the problem:
import json
data = b'"{\\"image\\": \\"/9j/4AAQSkZJRgABAQEASABIAAD/4gxYSUNDX1BST0ZJTEUAAQEAAAxITGlubwIQAABtbnRyU...opHNf//Z\\", \\"id\\": \\"e3ad9809-b84c-57f1-bd03-a54e25c59bcc\\"}"'
data_as_dictionary = json.loads(json.loads(data))
It seems like because your data has the leading " and the trailing ", json.loads is parsing it as a string (which is a valid json) and that's why you don't get the dictionary you want in the first call to json.loads.
import json
import base64
from codecs import encode
request = b'"{\\"image\\": \\"/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAoHCBYVFRgVEhUZ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QUiBV7J5LCntO5lBBTrwL5SaacGaCCmKByCCCYf/2Q==\\", \\"id\\": \\"e3ad9809-b84c-57f1-bd03-a54e25c59bcc\\"}"'
data_as_dictionary = json.loads(json.loads(request.decode('utf-8')))
base64_img = data_as_dictionary['image']
bytes_img = encode(base64_img, 'utf-8')
binary_img = base64.decodebytes(bytes_img)
with open("imageToSave.jpg", "wb") as fh:
fh.write(binary_img)
you need several steps. first of all, you should transform request data into a python dictionary then take the image as base64 then encode it in bytes array then as binary, and last save it.
The above code works well.

Import CSV from AWS S3 instance to Numpy

I've been trying to directly read a csv file from AWS S3 to numpy. I've used:
s3 = boto3.client(service_name = 's3')
def s3_read(filename):
s3_obj = s3.get_object(Bucket = 'bucket-name', Key = filename)
body = s3_obj['Body']
return body.read()
as an attempt to pull the data but I'm running into an issue of formatting from AWS that I don't know how to handle.
When I print out the data that is being returned from that there is a weird header before the data:
b{\n "name":"filename",\n "data":{\n "type":"Buffer,\n "data:[\n 114,\n 97,...]}}
So there's a bunch of \n's and the weird header. Would this have something to do with the way I uploaded the file to AWS or is there something I'm messing up with the reading of the file?
body.read() returns bytes.
import json
j = json.loads(s3_obj['Body'].read().decode('utf-8'))
decode will turn bytes to string, json.loads will parse the string to dictionary.

How does rfile.read() work?

I'm sending a text file with a string in a python script via POST to my server:
fo = open('data'.txt','a')
fo.write("hi, this is my testing data")
fo.close()
with open('data.txt', 'rb') as f:
r = requests.post("http://XXX.XX.X.X", data = {'data.txt':f})
f.close()
And receiving and handling it here in my server handler script, built off an example found online:
def do_POST(self):
data = self.rfile.read(int(self.headers.getheader('Content-Length')))
empty = [data]
with open('processing.txt', 'wb') as file:
for item in empty:
file.write("%s\n" % item)
file.close()
self._set_headers()
self.wfile.write("<html><body><h1>POST!</h1></body></html>")
My question is, how does:
self.rfile.read(int(self.headers.getheader('Content-Length')))
take the length of my data (an integer, # of bytes/characters) and read my file? I am confused how it knows what my data contains. What is going on behind the scenes with HTTP?
It outputs data.txt=hi%2C+this+is+my+testing+data
to my processing.txt, but I am expecting "hi this is my testing data"
I tried but failed to find documentation for what exactly rfile.read() does, and if simply finding that answers my question I'd appreciate it, and I could just delete this question.
Your client code snippet reads contents from the file data.txt and makes a POST request to your server with data structured as a key-value pair. The data sent to your server in this case is one key data.txt with the corresponding value being the contents of the file.
Your server code snippet reads the entire HTTP Request body and dumps it into a file. The key-value pair structured and sent from the client comes in a format that can be decoded by Python's built in library urlparse.
Here is a solution that could work:
def do_POST(self):
length = int(self.headers.getheader('content-length'))
field_data = self.rfile.read(length)
fields = urlparse.parse_qs(field_data)
This snippet of code was shamefully borrowed from: https://stackoverflow.com/a/31363982/705471
If you'd like to extract the contents of your text file back, adding the following line to the above snippet could help:
data_file = fields["data.txt"]
To learn more about how such information is encoded for the purposes of HTTP, read more at: https://en.wikipedia.org/wiki/Percent-encoding

Flask Application File Upload - Error while getting contents of file

I am developing a flask application which uploads a file to IBM Bluemix Cloudant DB. I need to save the contents of the file as a key value pair in Cloudant.
If I try to save a text file, it reads the content correctly. For other type of files it does not work.
Following is my flask REST API CODE:
#app.route('/upload', methods=['POST'])
def upload_file():
file_to_upload = request.files['file_upload'];
response = CloudantDB().upload_file_to_db(file_to_upload);
//tHE FUNCTION upload_file under CloudantDB is as shown below.
file_name = file.filename;
uploaded_file_content = file.read();
data = {
'file_name': file_name,
'file_contents': uploaded_file_content,
'version': version
}
my_doc = self.database.create_document(data);
I know the error is because "uploaded_file_content" is in a different format (i.e. For PDFs, JPGs etc).
Is there anyway I can overcome this?
Thanks!
The difference is that text files contain ordinary text whereas JPG, PNG etc. contain binary data.
Binary data should be uploaded as an attachment with a mime type, and you need to base64 encode the data. You don't show what create_document() is doing, but it's unlikely that it is able to treat binary data as an attachment. This might fix it for you:
from base64 import b64encode
uploaded_file_content = b64encode(file.read());
data = {
'file_name': file_name,
'version': version,
'_attachments': {
file_name : {
'content-type': 'image/png',
'data': uploaded_file_content
}
}
}
my_doc = self.database.create_document(data);
It should also be possible with your current code to simply base64 encode the file content and upload it. So that you know what type of data is stored should you later retrieve it, you will need to add another key value pair to store the mime type as content-type does above.
Attachments have advantages in that they can be individually addressed, read, deleted, updated without affecting the containing document, so you are probably better off using them.

Categories

Resources