Skip to content Skip to sidebar Skip to footer

Tmp File In Google Cloud Functions For Python

Python runs like a charm on google cloud functions, but for the tmp files. Here's my simplified code: FILE_PATH = '{}/report.pdf'.format(tempfile.gettempdir()) pdf.output(FILE_PATH

Solution 1:

It is a little difficult to find Google official documentation for writing in temporary folder. In My case, I needed to write in a temporary directory and upload it to google cloud storage using GCF.

Writing in temporary directory of Google Cloud Functions, it will consume memory resources provisioned for the function.

After creating the file and using it, it is recommended to remove it from the temporary directory. I used this code snippet for Write a csv into a temp dir in GCF(Python 3.7).

import pandas as pd
import os
import tempfile
from werkzeug.utils import secure_filename


defget_file_path(filename):
    file_name = secure_filename(filename)
    return os.path.join(tempfile.gettempdir(), file_name)

defwrite_temp_dir():
    data = [['tom', 10], ['nick', 15]] 
    df = pd.DataFrame(data, columns = ['Name', 'Age'])
    name = 'example.csv'
    path_name = get_file_path(name)
    df.to_csv(path_name, index=False)
    os.remove(path_name)

Post a Comment for "Tmp File In Google Cloud Functions For Python"