How can I upload files into django database - django

I want to upload files to the database that Django use, I know that I can do it
through forms, but I want to read the files in my files system get the path of the docx or pdf and uploaded it into the database, how can I do that
Here is the code that i use to get the path of the files in my filedsystem
for dir_, _, files in os.walk(superpath):
for fileName in files:
print fileName
if fileName.find('~$')==-1:
relDir = os.path.relpath(dir_, superpath)
if relDir=='.':
relFile =os.path.join(superpath, fileName)
else:
relFile = os.path.join(superpath,os.path.join(relDir, fileName))
path.append(relFile)

You can use ContentFile for this.
from django.core.files.base import ContentFile
f = open(file_path, 'r')
object.image.save('image.jpg', ContentFile(f.read()))

Related

How to Update file name in FileField after file is uploaded on s3 via presigned post URL in Django?

I have integrated django-storages in Django Project. for large file sizes, I have used a pre-signed URL to upload files from externally without taking a load on my server.
by pre-signed URL, Files uploaded successfully in the s3 bucket AWS, after uploading the file in s3 I need to update the name of the file in FileField.
Probably you need something like, using the boto3 library to retrieve the file from S3 and os library to rename the file.
import boto3
import os
s3 = boto3.client('s3')
uploaded_file = s3.get_object(Bucket='your-bucket-name', Key='object-key')
new_filename = 'new_file_name.txt'
os.rename(uploaded_file['Body'].name, new_filename)
...
with open(new_filename, 'rb') as f:
file_obj = File(f, name=new_filename)
my_model.file_field_name = file_obj
my_model.save()

How to download a file using default_storage class in django

I am using Django default_storage API to save my media files.
I am able to save the file, and open the file for writing. But I am not able to download the file.
I used the code below to save the file:
default_storage.save(filename, ContentFile(str(a).encode()))
Is there any way to download the file in the same way?
I used the code below to download the file, but it is not either downloading or not throwing any error:
with default_storage.open(filepath, 'rb') as fh:
response = HttpResponse(fh.read(), content_type="application/vnd.ms-excel")
response['Content-Disposition'] = 'inline ; filename=' +os.path.basename(filepath)
return response
raise Http404
You are on the right path.
with default_storage.open(filepath, 'rb') as fh:
with open('my_local_file','wb') as wh:
data = fh.read() # You may want to split this into chunks..
wh.write(data)

How to use pickle.load in flask app - Python3

#app.route('/', methods=['POST'])
def upload_file():
if request.method == 'POST':
if 'files[]' not in request.files:
flash('No file part')
return redirect(request.url)
files = request.files.getlist('files[]')
for file in files:
if file and allowed_file(file.filename):
#print(file.filename)
filename = secure_filename(file.filename)
encrypted_list = pickle.load(open(file, "rb"))
print(encrypted_list)
I am having some string whose base64 encoding I have stored in .pem files and idea is X person will upload multiple .pem files in the flask app and I don't want to save them in disk, just read those .pem files using pickle.load(open(file, "rb")) but this command is giving me error. As while encrypting I have stored those base64 encoded string in .pem files. Now I want to decode those string from .pem files in Flask.
Any help will be appreciated and thanks in advance!!
As the error message suggests, you cannot pass a FileStorage instance into pickle's load function.
Instead, you should pass in a file like object, like so encrypted_list = pickle.load(file.stream).
https://werkzeug.palletsprojects.com/en/1.0.x/datastructures/#werkzeug.datastructures.FileStorage

Django AES Encryption : how encrypt user-uploaded files before they are saved?

I want to encrypt user uploaded files in django before saving them.
When user send files through POST requests, I get a "InMemoryUploadedFile" type object.
How can I encrypt the files before saving them ? I currently use pyAesCrypt to encrypt files but I can't manage to pass in it the "InMemoryUploadedFile" objects. I manage to only encrypt them after they are saved with :
import pyAesCrypt
with open("*FileName*", "rb") as InputFile:
with open("*OutputFileName*", "wb+") as OutputFile:
pyAesCrypt.encryptStream(InputFile, OutputFile, Password, BufferSize)
I recently asked this questions and a user told me to use a package with better community support. It is pyca/cryptography. I was stuck in the same thing and I found a solution. Mind that, I use Django Rest Framework.
from cryptography.fernet import Fernet
# Generate a key and store safely
key = Fernet.generate_key()
f = Fernet(key)
I'll take an excel file for example but you could actually use any file.
import pandas as pd
import io
from django.core.files.uploadedfile import SimpleUploadedFile
# Request file from user and load the file into a dataframe
df = pd.read_excel(request.FILES('file_name'))
# Create BytesIO
output = io.BytesIO()
# Output df to BytesIO
df.to_excel(output, index=False)
# Encrypt data (BytesIO)
encrypted_out = f.encrypt(output.getvalue())
# Export encrypted file
output_file = SimpleUploadedFile('<some_file_name.extension>',encrypted_out)
# Typically you would pass this through a serializer.
To decrypt the file before you can serve the user. Read the file and write it to BytesIO and then you can serve the file to the user.

Programmatically Upload Files in Django

I have checked several other threads but I am still having a problem. I have a model that includes a FileField and I am generating semi-random instances for various purposes. However, I am having a problem uploading the files.
When I create a new file, it appears to work (the new instance is saved to the database), a file is created in the appropriate directory, but the file's content is missing or corrupt.
Here is the relevant code:
class UploadedFile(models.Model):
document = models.FileField(upload_to=PATH)
from django.core.files import File
doc = UploadedFile()
with open(filepath, 'wb+') as doc_file:
doc.documen.save(filename, File(doc_file), save=True)
doc.save()
Thank you!
Could it be as simple as the opening of the file. Since you opened the file in 'wb+' (write, binary, append) the handle is at the end of the file. try:
class UploadedFile(models.Model):
document = models.FileField(upload_to=PATH)
from django.core.files import File
doc = UploadedFile()
with open(filepath, 'rb') as doc_file:
doc.document.save(filename, File(doc_file), save=True)
doc.save()
Now its open at the beginning of the file.