0

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

对于 Firebase 存储集成,我使用 pyrebase 包。

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

但是,我存储的文件总是空的。这是什么原因?

1.生成localFile

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. 存储本地文件

# 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
4

1 回答 1

1

问题是我只用写权限打开了我的文件:

localFile = open(localFileName,"w+")

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

# 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)
于 2018-10-30T13:20:18.183 回答