使用Python存储在Firebase上的空文件 [英] Empty file stored on Firebase with Python

查看:80
本文介绍了使用Python存储在Firebase上的空文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是在我的Python服务器上生成某些文件(txt/pdf/excel),然后将其推送到Firebase存储.

My goal is to generate certain files (txt/pdf/excel) on my Python server and subsequently push it to the Firebase Storage.

对于Firebase Storage集成,我使用pyrebase软件包.

For the Firebase Storage integration I use the pyrebase package.

到目前为止,我已经设法在本地生成文件,然后将其存储在Firebase Storage数据库的正确路径上.

So far I have managed to generate the file locally and subsequently store it on the right path on the Firebase Storage database.

但是,我存储的文件始终为空.是什么原因呢?

However, the files I store are always empty. What is the reason for this?

import os
def save_templocalfile(specs):


    # Random something
    localFileName = "test.txt"
    localFile     = open(localFileName,"w+")
    for i in range(1000):
        localFile.write("This is line %d\r\n" % (i+1))


    return {
            'localFileName':    localFileName,
            'localFile':        localFile
        }

2.存储localFile

# Required Libraries
import pyrebase
import time


# Firebase Setup & Admin Auth
config = {
  "apiKey":        "<PARAMETER>",
  "authDomain":    "<PARAMETER>",
  "databaseURL":   "<PARAMETER>",
  "projectId":     "<PARAMETER>",
  "storageBucket": "<PARAMETER>",
  "messagingSenderId": "<PARAMETER>"
}

firebase    = pyrebase.initialize_app(config)
storage     = firebase.storage()


def fb_upload(localFile):


    # Define childref
    childRef      = "/test/test.txt"
    storage.child(childRef).put(localFile)


    # Get the file url
    fbResponse = storage.child(childRef).get_url(None)


    return fbResponse

推荐答案

问题是我仅以写"权限打开了文件:

The problem was that I opened my file with Write permissions only:

localFile = open(localFileName,"w+")

解决方案是关闭写入操作并使用读取权限打开它:

The solution was to close the write operation and opening it with Read permissions:

# close (Write)
localFile.close()

# Open (Read)
my_file       = open(localFileName, "rb")
my_bytes      = my_file.read()

# Store on FB
fbUploadObj   = storage.child(storageRef).put(my_bytes)

这篇关于使用Python存储在Firebase上的空文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆