惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
人人都是产品经理
人人都是产品经理
爱范儿
爱范儿
云风的 BLOG
云风的 BLOG
Last Week in AI
Last Week in AI
H
Hackread – Cybersecurity News, Data Breaches, AI and More
B
Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
H
Help Net Security
B
Blog RSS Feed
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
N
Netflix TechBlog - Medium
S
SegmentFault 最新的问题
The Cloudflare Blog
I
InfoQ
美团技术团队
博客园 - 三生石上(FineUI控件)
MyScale Blog
MyScale Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 司徒正美
L
LangChain Blog
A
About on SuperTechFans
T
The Blog of Author Tim Ferriss
Y
Y Combinator Blog

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
How to Download Files from Google Cloud Storage in the Da...
2023-11-22 · via jdhao's digital space

In this post, I want to share the complete process and setup to download files from GCP in a Databricks workspace notebook. Since the notebook itself is non-interactive when you run the shell command, the setup process is a bit different from the normal GCP authentication.

Install gcloud cli#

For my Databricks clusters, I checked that the under system is Ubuntu Linux (version 20.04). So we can use apg-get to update and install packages like a normal Ubuntu system.

First we need to update the dependency libraries:

sudo apt-get update
sudo apt-get install apt-transport-https ca-certificates gnupg curl sudo

Set up the correct public GPG key for the google cloud deb source:

# download the google cloud gpg key, turn it into binary format and save it under directory /user/share/keyrings/.
# you can use the `file` command to check it: `file /usr/share/keyrings/cloud.google.gpg`
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | gpg --dearmor | sudo tee -a /usr/share/keyrings/cloud.google.gpg > /dev/null

# create the google cloud source entry under directory /etc/apt/sources.list.d/,
# the signed-by field specifies the key file to use for authentication,
# which is exactly the keyring we created in the first command.
echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] https://packages.cloud.google.com/apt cloud-sdk main" | sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list > /dev/null

In the above commands, we import and google cloud repo keyring and set up the meta info about the repo.

Now we should be able to install gcloud cli without issue:

sudo apt-get update && sudo apt-get install google-cloud-cli

ref:

Authentication#

User or service account credential#

If you want to user or service account credentials, you need to do it interactively in a shell. This is difficult to do in the databricks notebooks, because the shell command you run in notebooks can not accept input. However, if you have access to the web terminal provided by databricks, you can do the authentication interactively:

gcloud auth application-default login

Note that the authentication is not permanent, it will be gone when you restart your cluster. So you need to do this authentication each time you run your code.

ref:

Service account keys#

First, we need to create a service account key, the documentation is here. After getting the key file (JSON format), we can upload it to databricks workspace. Usually, we just set the environment variable GOOGLE_APPLICATION_CREDENTIALS to point to the key file path. But this does not work for databricks if you use shell commands: %sh export GOOGLE_APPLICATION_CREDENTIALS="/path/to/key_file". Instead, we can directly set this environment variable in the Python code:

import os

os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/path/to/key_file"

ref:

First, install the gcloud storage package:

pip install --upgrade google-cloud-storage

Then you can download a file from cloud storage bucket like this:

from google.cloud import storage
import os

# this is for authentication
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/path/to/key_file"

# Initialise a client
storage_client = storage.Client("<project-name>")
# Create a bucket object for our bucket
bucket = storage_client.get_bucket("<bucket-name>")

# Create a blob object from the filepath
blob = bucket.blob("<sub-directory>/file_name.ext")

# Download the file to a destination
blob.download_to_filename('./my_file.ext')

ref: