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

推荐订阅源

Hugging Face - Blog
Hugging Face - Blog
Recent Announcements
Recent Announcements
V
Visual Studio Blog
博客园 - 叶小钗
H
Help Net Security
aimingoo的专栏
aimingoo的专栏
宝玉的分享
宝玉的分享
U
Unit 42
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
F
Fortinet All Blogs
V
V2EX
Stack Overflow Blog
Stack Overflow Blog
WordPress大学
WordPress大学
D
DataBreaches.Net
J
Java Code Geeks
H
Hackread – Cybersecurity News, Data Breaches, AI and More
A
About on SuperTechFans
酷 壳 – CoolShell
酷 壳 – CoolShell
量子位
C
Check Point Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
小众软件
小众软件
Microsoft Azure Blog
Microsoft Azure Blog
M
MIT News - Artificial intelligence

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
Google Cloud Storage Usage
2024-07-11 · via jdhao's digital space

Some notes on using the Google Cloud storage.

Install client Library#

We need to install the gcloud storage client to use the storage API. For Python, use pip to install the client:

pip install google-cloud-storage

test in local env#

When we run the gcloud storage client in local PC and want to connect to our project, we need to do the authentication:

gcloud auth application-default login

Follow the instructions and finish the authentication. Then we can use the client locally for testing.

use the correct bucket name#

Note that bucket names refer to the top level folders when you click Cloud Storage --> Buckets. Under each bucket, you can define sub-folder structures. However, when we want to get a bucket, these sub-folders should not be part of the bucket name!!

check whether a file exists?#

from google.cloud import storage

storage_client = storage.Client()

bucket_name = "my-bucket"
bucket = storage_client.bucket(bucket_name)

file_path = "path/to/your_file/under/the/bucket"
blob = bucket.blob(file_path)

print(blob.exists())

ref:

Save Python dict in gcloud storage?#

We need to convert the dict to string using JSON package and then use the method provided by blob to upload it. Here is a working code example:

import json
from google.cloud import storage

storage_client = storage.Client()

bucket_name = "my-bucket"
bucket = storage_client.bucket(bucket_name)

file_path = "path/to/your_file/under/the/bucket"
blob = bucket.blob(file_path)

content = {"message": "hello world", 'code': 0}
blob.upload_from_string(json.dumps(content))

ref:

download file and convert to dict#

We can download the file as string and then convert it to the desired type:

import json
from google.cloud import storage

storage_client = storage.Client()

bucket_name = "my-bucket"
bucket = storage_client.bucket(bucket_name)

file_path = "path/to/your_file/under/the/bucket"
blob = bucket.blob(file_path)

content = blob.download_as_string()
content_real = json.loads(content)

ref: