从url检索图像并使用python上传到firestore

时间:2019-08-07 09:46:31

标签: python image firebase

摘要:我必须从Firebase实时获取的网址中获取图片,然后上传到Firebase存储中

免责声明:我真的很喜欢python

这是我连接到Firebase的方式:

cred = credentials.Certificate('./ServiceAccountKey.json')

db_dev = firebase_admin.initialize_app(cred, {'databaseURL': 'https://<realtime_database>.firebaseio.com/'}, name='db_dev')
storage_dev = firebase_admin.initialize_app(cred, {'storage': 'gs://<storage_dev>.appspot.com/'}, name='storage_dev')

此刻,我能够获取用户信息以获取其图像网址

def getUsersData(user_db):
    # Function to obtain the data from the users such as name, image

    users = {}
    users_profile = {}
    for user in user_db:
        users[user] = {}
        users_profile[user] = {}
        if 'userData' in user_db[user]:
            users[user] = flatten_dict({'userName': user_db[user]['userData'].get('userName', ''),
                                        'photoUrlHq': user_db[user]['userData'].get('photoUrlHq', {})
                                        })
    return users

使用此信息,我必须循环dict,以获取用户ID和网址图片。

然后,对于每个用户,我必须下载图像,然后将其推送到路径gs://<storage>.appspot.com/ProfileImage的Firebase存储中

这是我迷路的地方。

我已经完成了这一行以获得图像

def getImageData(users):
    i = 0
    for key in users:
        # To verify we the info that we are recovering
        print(key + str(users[key]))
        try:
            url = users[key]['photoUrlHq']
            urllib.request.urlretrieve(url, key + ".jpg")
            # TODO Here is where is supposed to upload image to firebase storage
        except:
            print("no vlaid user info")

        # This count is only for test purposes, to avoid to all the images while testing
        i = i + 1
        if i >= 5:
            break

从这一步开始,我不知道如何将其推入存储。甚至更多,将其推入循环是否正确?还是有一些异步方式? (我真的迷上了python)

谢谢

0 个答案:

没有答案