简体   繁体   English

如何从firebase下载图像到计算机

[英]How to download images to computer from firebase

My actual plan is that a Python script should download the latest uploaded image from Firebase to a Raspberry Pi or my computer (Ubuntu).我的实际计划是 Python 脚本应该将最新上传的图像从 Firebase 下载到 Raspberry Pi 或我的计算机 (Ubuntu)。

I already tried with gsutil like this:我已经尝试过像这样使用 gsutil:

from  google.cloud import storage

import os

os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="finalpro.json"

client = storage.Client(project='finalpro-5d930')

bucket = client.get_bucket('gs://finalpro-5d930.appspot.com')

blob = storage.Blob('NewUploads/', bucket)

blob.download_to_filename('/home/simon/Documents/images')

I need only the Python script, so that I can initialise that script in the bashrc file on my Raspberry Pi, so that when the Raspberry Pi boots up, it runs that script automatically to download image from Firebase.我只需要 Python 脚本,这样我就可以在 Raspberry Pi 上的bashrc文件中初始化该脚本,这样当 Raspberry Pi 启动时,它会自动运行该脚本以从 Firebase 下载图像。

Installation安装

pip install firebase

Python Version蟒蛇版

Firebase was written for python 3 and above and will not work correctly with python 2. Firebase 是为 python 3 及更高版本编写的,不能与 python 2 一起正常工作。

Add Firebase to your Application将 Firebase 添加到您的应用程序

Your Google's Firebase configuration data can be found on Firebase > Settings > Project Settings Scroll to bottom > Add to web app > config您的 Google 的 Firebase 配置数据可以在Firebase > 设置 > 项目设置滚动到底部> 添加到网络应用程序 > 配置上找到

For use with only user based authentication we can create the following configuration:为了仅用于基于用户的身份验证,我们可以创建以下配置:

from firebase import Firebase

config = {
  "apiKey": "apiKey",
  "authDomain": "projectId.firebaseapp.com",
  "databaseURL": "https://databaseName.firebaseio.com",
  "storageBucket": "projectId.appspot.com"
}

firebase = Firebase(config)

Adding a service account will authenticate as an admin by default for all database queries, check out the Authentication documentation for how to authenticate users.默认情况下,添加服务帐户将针对所有数据库查询以管理员身份进行身份验证,请查看身份验证文档以了解如何对用户进行身份验证。

Storage贮存

The storage service allows you to upload images to Firebase.存储服务允许您将图像上传到 Firebase。

You first need to initialise your storage object:您首先需要初始化您的存储对象:

storage = firebase.storage()

child孩子

Just like with the Database service, you can build paths to your data with the Storage service.就像使用数据库服务一样,您可以使用存储服务构建数据路径。

storage.child("images/example.jpg")

put

The put method takes the path to the local file and an optional user token. put 方法采用本地文件的路径和可选的用户令牌。

storage = firebase.storage()

# as admin
storage.child("images/example.jpg").put("example2.jpg")

# as user
storage.child("images/example.jpg").put("example2.jpg", user['idToken'])

download下载

The download method takes the path to the saved database file and the name you want the downloaded file to have.下载方法采用保存的数据库文件的路径以及您希望下载的文件具有的名称。

storage.child("images/example.jpg").download("downloaded.jpg")

get_url获取网址

The get_url method takes the path to the saved database file and returns the storage url. get_url 方法获取保存的数据库文件的路径并返回存储 url。

storage.child("images/example.jpg").get_url()

>> https://firebasestorage.googleapis.com/v0/b/storage-url.appspot.com/o/images%2Fexample.jpg?alt=media

I hope this helps.我希望这有帮助。 Read full docs: https://pypi.org/project/firebase阅读完整文档: https : //pypi.org/project/firebase

If you need url for individual photo then :-如果您需要个人照片的网址,则:-

url=storage.child("images/example.jpg").get_url(example.jpg)
print(url)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM