0

我正在尝试从名为 Transcripts 的子文件夹下的 Firebase 存储中读取文件。当我尝试读取根文件夹中的文本文件时,它可以正常工作。但是,它无法读取名为“Transcripts”的子文件夹下的任何文本文件。

这是我的 Firebase 存储桶的结构:

Transcripts/
    Audio 1.txt
    Audio 2.txt
Audio 1.amr
Audio 2.amr
Audio Name.txt

这是我尝试读取根文件夹中文件的python代码:

import pyrebase

config = {
    "apiKey": "XXXXXXXX",
    "authDomain": "XXXXXXXX.firebaseapp.com",
    "databaseURL": "https://XXXXXXXX.firebaseio.com",
    "projectId": "XXXXXXXX",
    "storageBucket": "XXXXXXXX.appspot.com",
    "messagingSenderId": "XXXXXXXX",
    "appId": "XXXXXXXX",
    "measurementId": "XXXXXXXX",
    "serviceAccount":"/Users/faizarahman/Desktop/MY-PROJECT.json"

}
 
firebase = pyrebase.initialize_app(config)  # initializing firebase
storage = firebase.storage()  # getting storage reference 1
storage2 = firebase.storage()  # getting storage reference 2 (to avoid overwriting to storage reference 1)
url = storage.child("Audio Name").get_url(None)  # getting the url from storage
print(url)  # printing the url
text_file = urllib.request.urlopen(url).read()  # reading the text file

name_list = storage.child("Transcripts/").list_files()  # getting all the list of files inside the Transcripts folder.
folder_name = "Transcripts/ "
for file in name_list:  # iterating through all the files in the list.
    try:
        if folder_name in file.name:  # Check if the path has "Transcripts"
            
            transcript_name = file.name.replace("Transcripts/ ", "")  # Extract the name from the file from "Transcripts/ Audio Number"
            unicode_text = text_file.decode("utf-8")  # converting the content inside the Audio Name file to a string value.
            if transcript_name == unicode_text:  # If the content inside the Audio Name file (the content will be a file name) matches with the file name then read that file
                text_file1 = storage2.child("Audio Name").get_url(None)  # for testing purposes the "Audio Name" works here...
                print(text_file1)
    except:
        print('Download Failed')

它给我的链接如下所示:

https://firebasestorage.googleapis.com/v0/b/MY-PROJECT-ID.appspot.com/o/Audio%20Name?alt=media

这是我单击链接时得到的结果: 读取音频名称文本文件成功。

这是我尝试读取“Transcripts”文件夹中文件的python代码:

import pyrebase
    
    config = {
        "apiKey": "XXXXXXXX",
        "authDomain": "XXXXXXXX.firebaseapp.com",
        "databaseURL": "https://XXXXXXXX.firebaseio.com",
        "projectId": "XXXXXXXX",
        "storageBucket": "XXXXXXXX.appspot.com",
        "messagingSenderId": "XXXXXXXX",
        "appId": "XXXXXXXX",
        "measurementId": "XXXXXXXX",
        "serviceAccount":"/Users/faizarahman/Desktop/MY-PROJECT.json"
    
    }
     
    firebase = pyrebase.initialize_app(config)  # initializing firebase
    storage = firebase.storage()  # getting storage reference 1
    storage2 = firebase.storage()  # getting storage reference 2 (to avoid overwriting to storage reference 1)
    url = storage.child("Audio Name").get_url(None)  # getting the url from storage
    print(url)  # printing the url
    text_file = urllib.request.urlopen(url).read()  # reading the text file
    
    name_list = storage.child("Transcripts/").list_files()  # getting all the list of files inside the Transcripts folder.
    folder_name = "Transcripts/ "
    for file in name_list:  # iterating through all the files in the list.
        try:
            if folder_name in file.name:  # Check if the path has "Transcripts"
                
                transcript_name = file.name.replace("Transcripts/ ", "")  # Extract the name from the file from "Transcripts/ Audio Number"
                unicode_text = text_file.decode("utf-8")  # converting the content inside the Audio Name file to a string value.
                if transcript_name == unicode_text:  # If the content inside the Audio Name file (the content will be a file name) matches with the file name then read that file
                    text_file1 = storage2.child("Transcripts/" + unicode_text).get_url(None)   # for testing purposes the "Audio Name" works here but reading the file under transcripts does not work here...
                    print(text_file1)
        except:
            print('Download Failed')

它给我的链接如下所示:

https://firebasestorage.googleapis.com/v0/b/MY-PROJECT-ID.appspot.com/o/Transcripts%2FAudio%202?alt=media

这是我尝试读取“Transcripts”子文件夹中的文件时得到的结果: Reading Audio 2 under transcript sub folder failed。

我相信错误在这一行:

text_file1 = storage2.child("Transcripts/" + unicode_text).get_url(None)
4

0 回答 0