How to store image file or file temporary using python or django rest_framework without saving in the database? I am new to Django - python-3.x

How to store image file or file temporary using python or django rest_framework without saving in the database?
In dajngo rest_framework I did these things in a function based view.
temp_file = request.FILES['document_file']
Then, how I store this file temporary without saving in the database?

You can refer the below function which handles a .txt file.You can create a temporary_folder directory. To write file to temporary_folder:
def handle_uploaded_file(f):
with open('/full/path/to/temporary_folder/name.txt', 'wb+') as destination:
for chunk in f.chunks():
destination.write(chunk)
Once your business logic is done, you can delete the file. Please check this How to delete a file or folder?
Other Reference: https://docs.djangoproject.com/en/3.1/topics/http/file-uploads/

Related

How to create json file path with variable in Python3?

I am trying to write cookies to a json file.
cookies = web.get_cookies()
with open('test.json', 'w') as outputdata:
json.dump(cookies, outputdata)
sleep(5)
Which writes cookies to test.json.
I am trying to create a cookie file for each user and each website visited such as.
cookies/{user}/{website}.json
'w' should create a file if it does not exist. But instead of the file being created I am getting error file does not exist.
I have defined the variables, printing user shows the username.
When manually creating the file such as test.json in the first example the script works.
How can I create the file at the path with variables?
Thank you
'w' does create a file provided the containing directory exists.
You could ensure that the directory exists with os.makedirs:
# create directories if required without error if they exist
# thanks to exists_ok=True
os.makedirs(f'cookies/{users}', exist_ok=True)
with os.open(f'cookies/{users}/{website}.json':
...

Python: Can you upload a .xlsx file from xlsxwriter without creating one locally?

I hope this question is as straightforward as I think it is.
Here is some background:
I'm helping out on python backend that is getting messy data as a csv. In its current state it just reroutes the url given by the API and triggers a download on the client computer. I wrote a utility using Pandas and xlsxwriter that cleans up this data, separates into multiple tabs and makes some graphs then writes them to a .xlsx file. Basically like this:
import pandas as pd
writer = pd.ExcelWriter(output_file_name, engine = 'xlsxwriter')
#Do a bunch of stuff and save each tab to writer
writer.save() #Writes the file
This .xlsx file would be created locally and there would need to be additional backend stuff that uploads it and cleans up the local file.
Seeing as the file is created all at once using the .save() method at the end, I was thinking its probably possible to trigger an upload directly without creating the local file at all, but I'm not seeing anything in xlsxwriter documentation about it. Is there any way to avoid saving a local file within or outside of xlsxwriter?
Assuming df is your dataframe variable:
import io
buffer = io.BytesIO()
writer = pd.ExcelWriter(buffer, engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1')
writer.save()
data = buffer.getvalue()
data contains the binary data of excel file. For instance, you can use requests module to upload file somewhere.

Any way to import a saved search file over 10mb to File Cabinet using SuiteScript?

I use SuiteScript to execute Saved Searches and save CSV files to the File Cabinet. However, the saved files are limited to 10mb or the script fails. Is there any way to work around the 10mb limit? I'm able to upload a file through the UI over 10mb in size and the ability to do so using SuiteScript would be very useful.
Thanks for any insight.
Like #bknights answered, you can use the N/task module to have NetSuite create a CSV for you.
var searchTask = task.create({
taskType: task.TaskType.SEARCH
});
searchTask.savedSearchId = 51;
searchTask.filePath = 'ExportFolder/export.csv';;
var searchTaskId = searchTask.submit();
If for whatever reason you need more control over the output, you can create files larger than 10MB using N/file#File.appendLine() to set the contents of the file line by line.
Use a SS2.0 N/task method to schedule the script to be published to a file id or path.

Appending to a text file in S3

I know how to write and read from a file in S3 using boto. I'm wondering if there is a way to append to a file without having to download the file and re-upload an edited version?
There is no way to append data to an existing object in S3. You would have to grab the data locally, add the extra data, and then write it back to S3.

Upload and Save an excel file with BottlePy

I am creating an application using Bottle framework. I need a feature to upload an Excel file.
I am using the following for file upload.
http://bottlepy.org/docs/dev/tutorial.html#post-form-data-and-file-uploads
On the server side I am getting the file data as binary content. I want to save it in a temporary folder as an Excel file.
I am new to Python and Bottle. Any help will be much appreciated.
Thanks
Chirdeep
Your request.files.data object contains the data about your excel file. So you only need to create a temporary folder and save it inside. This can be done using the tempfile module
f = tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx")
f.write(request.files.data.file.read())
f.close()
I was not able to get simple file writing code like yours to work, So I used the tempfile module. Looking at your code, I would have assumed it would write to the directory where the python file is, if the code is working. Try using the code below, if you don't pass arguments to dir, it will create a file in the current directory.
def save_as_temp_file(data):
with tempfile.NamedTemporaryFile(dir=settings.TEMP_PATH,
delete=False,
suffix=".xlsx") as f:
f.write(data.file.read())
return f.name

Resources