我如何 push/upload 照片到 Google (Firebase) 存储?

How do I push/upload photos to Google (Firebase) Storage?

基本上我的问题是我似乎无法将照片上传到我的 Firebase 项目。我可以将用户信息推送到 Realtime Database and I successfully have integrated Firebase Authentication as well, which I use with Flask Sessions to keep users logged in. It appears that in order to save files/photos you need to use Firebase Storage,但我不知道如何推送。

用户可以点击我的“/upload-image”路径中的“上传文件”按钮,现在我将图像保存(在用户上传后)到与我的 fiddl.py 文件.

以下所有代码都在我的Fiddl.py 文件中。

这是我的 Firebase 初始化:
注意storageRef是我试过的,但是存储中没有ref()这样的东西,所以我得到了错误。我还有 storage,因为我相信它引用了我的项目存储。

#initialize database
firebase = pyrebase.initialize_app(json.load(open('firebase/firebaseConfig.json')))
auth = firebase.auth()
db = firebase.database()                                    
#storageRef = firebase.storage.ref()                           #Points to the root reference
app.secret_key = os.urandom(24)                                #secret key:track if user is logged in
storage = firebase.storage("gs://fiddl-dev.appspot.com")      

这是我用来将用户上传的图片存储在本地文件夹中的配置 /FIDDL/Photo test。为了您的方便,我缩短了路径名称。 旁注我希望稍后将这些配置添加到它们自己的文件中以确保安全,但仍然不能 100% 确定它们应该如何工作。除了这条路线,我还有让用户从他们的设备上传照片的路线。

#temp image upload location
#TODO: put these in a private config file
app.config["IMAGE_UPLOAD"] = "/FIDDL//photosTest"
app.config["ALLOWED_IMAGE_EXTENSIONS"] = ["PNG", "JPG", "JPEG"]
app.config["MAX_IMAGE_FILESIZE"] = 1.5 * 1024 * 1024    #1,572,864 Bytes or 1572.864 KB

# Upload Image
@app.route('/upload-image', methods=["GET", "POST"])
def upload_image():
    try:
        print(session['usr'])                   #this is equal to logged in users id token->  user['idToken']
        app.logger.info("A user is already logged in")
        #TODO: add a logout button, make sure to delete session for use on logout

        # Alert Messages
        noFileName = 'Image must have a file name.'
        badFileSize = 'Image file size is too large.'
        badFileName = 'That image extension or file name is not allowed'
        dataAdded = 'User photos added to realtime database'

        if request.method == "POST":
            if request.files:
                image = request.files["image"]                          #works the same as user input emal in register, get the image file
                print(image)

                #Grab the file size
                image.seek(0, os.SEEK_END)
                file_size = image.tell()
                app.logger.info("Image File Size: ")
                app.logger.info(file_size)

                #Check the image file size
                if not allowed_image_filesize(file_size):
                    #TODO: alert user on screen of the issue
                    app.logger.info(badFileSize)
                    return redirect(request.url)
                
                #Check for file name
                if image.filename == "":
                    app.logger.info(noFileName)
                    return redirect(request.url)
                
                #Check if image has correct type
                if not allow_image(image.filename):
                    app.logger.info(badFileName)
                    return redirect(request.url)
                else:
                    filename = secure_filename(image.filename)          #create new secure file name
                    app.logger.info(filename)
                    print(auth.current_user)
                    #Add data to realtime database
                    #TODO: figure out how to store photos in firebase
                    
                 

                    #Update progress
                    app.logger.info(dataAdded)




                    #image.save(os.path.join(app.config["IMAGE_UPLOAD"], filename))  #save images to /photosTest for testing
                    #print("image saved")
                
                return redirect(request.url)
    except KeyError:
        app.logger.info("No user logged in, redirect to login")
        return redirect(url_for('login'))
    
    return render_template("upload_image.html")

这是 Firebase/Develop/Storage/Rules 的规则。我正在使用 Firebase 身份验证,但仍然收到错误消息:

    "code": 403,
    "message": "Permission denied. Could not perform this operation"
  }

规则:

rules_version = '2';

// Grants a user access to a node matching their user ID
service firebase.storage {
  match /b/fiddl-dev.appspot.com/o {
    // Files look like: "user/<UID>/path/to/file.txt"
    match /user/{userId}/{allPaths=**} {
        allow write: if request.auth != null;
      allow read: if true; 
    }
  }
}

我知道这里没有真正要解决的问题,但任何一点在正确的方向上都会很棒!谢谢!

TLDR 答案,包括一些小文件检查,是我遇到问题的部分原因。如果需要,上面有更多详细信息。

要将 Image/file 上传到 Google Firebase 存储:

firebase = pyrebase.initialize_app(json.load(open('firebase/firebaseConfig.json')))
storage = firebase.storage("gs://fiddl-dev.appspot.com")

app.config["IMAGE_UPLOAD"] = "/FIDDL//photosTest"
app.config["ALLOWED_IMAGE_EXTENSIONS"] = ["PNG", "JPG", "JPEG"]
app.config["MAX_IMAGE_FILESIZE"] = 1.5 * 1024 * 1024    #1,572,864 Bytes or 1572.864 KB

# Upload Image
@app.route('/upload-image', methods=["GET", "POST"])
def upload_image():
    try:
       if request.method == "POST":
            if request.files:
                image = request.files["image"]                          

                #Grab the file size
                image.seek(0, os.SEEK_END) # Using seek here requires another later
                file_size = image.tell()

                #Check the image file size
                if not allowed_image_filesize(file_size):
                    return redirect(request.url)
                
                #Check for file name
                if image.filename == "":
                    return redirect(request.url)
                
                #Check if image has correct type
                if not allow_image(image.filename):
                    return redirect(request.url)
                else:

                    filename = secure_filename(image.filename)  #create new secure file name        
                    image.seek(0)
                    storage.child("images/" + filename).put(image)
     except: 
          ...