» Given a URL, how to encode a files contents as base64 with Python / Django?
Given a URL, how to encode a files contents as base64 with Python / Django?
|July 22, 2014
Posted by forumadmin
I am building a Django-based website, and am having trouble figuring out a decent way to email some larger PDFs and such to my users.
The files in question never touch our servers; they’re handled on a CDN. So, my starting point is with the unique URLs for the files, not with the files themselves. It would be nice to find a solution that doesn’t involve saving the files locally.
In order for me to be able to send the email in the way I want (with the PDF/DOCX/whatever attached to it), I need to be able to encode the attachment as a base-64 string.
I would prefer not to save the file to our server; I would also prefer not to read a response object in chunks and write it plainly to a file on our server, then encode that file.
That said, given a direct url to a file is there a way to stream the response and encode it in base64 as it comes in?
I have been reading about Django’s
FileWrapper and feel like I am close, but I’m not able to put it together just yet.
Edit: the snippet below is working for now, but I’m worried about memory usage – how well would something like this scale?
req = requests.get('url')
encoded = base64.b64encode(req.content)
More Related Questions
- send attachmant with chinese name to kindle fail in django I'm developing a django site to send ebooks to kindle by email. when the ebook name is in English, everything is ok,my kindle can recieve the ebook. However, if the ebook name is Chinese, […]
- Django – File Upload: Open the file as text instead of bytes On Python 3.3 and Django 1.6
Is it possible, in a ModelForm's View, to open an uploaded file as a text file instead of bytes ?
Here's some example code :
def post(self, request, *args, […]
- encrypt a string to obtain a fixed length let's suppose I have the following possible values of a string:
actually, this is a GET parameter obtained from the URL. exp can become very […]
- Python 2.7 Unicode/IDLE confusion I've read a lot about Unicode and the various encodings/decodings in Python 2.7, but I'm still having trouble understanding why IDLE can't seem to print the right string.
I have a unicode […]
- Decoding attachment encoded as base64 string I have an email that was sent to me with an attachment, however for some reason it came through as a base64 encoded string. Half way down the email it looks like this;
- encoding when sending a message app engine I created an app on the app Engine, it has a feedback form to contact the administrator. But when I test the transmission of messages on my email, I get characters sledushchego type ������ […]
- UTF-8 characters doesnt show correctly in email I have an html email sending script which can contain UTF-8 characters. Whenever I send UTF-8 characters to the script it sends the email with weird characters like Ø¹Ù…Ø±Ø§Ù†ÛŒ.I have the […]
- reading foreign characters in python I'm trying to read a csv file containing foreign characters (french accents at the moment but will be russian as well in the future). Is there a way to read these csvs without […]
- How to write the ASN1 object to a file in python I am trying implement HMAC-SHA256 of a file. I want to read the data in 512 bytes of chunks and hash it, and then write the hash digest into a file forexample filename.hmac in ASN1 format. […]
- Problems with encoding Website in Python. Getting charmap codec cant encode character \x9f in position I want to build an RSS Feed Reader by myself. So I started up.
My Test Page, from where I get my feed is 'http://heise.de.feedsportal.com/c/35207/f/653902/index.rss'.
It is a German page […]