python - 使用 s3fs 连接到 aws s3 中的谷歌云存储(gcs)相当于什么?

标签 python google-cloud-platform google-cloud-storage fsspec

我想按照下面的代码访问谷歌云存储。

# amazon s3 connection
import s3fs as fs 

with fs.open("s3://mybucket/image1.jpg") as f:
    image = Image.open(f).convert("RGB")


# Is there an equivalent code like this GCP side?
with cloudstorage.open("gs://my_bucket/image1.jpg") as f:
     image = Image.open(f).convert("RGB")

最佳答案

您正在寻找 gcsfs . s3fs 和 gcsfs 都是 fsspec 的一部分项目并具有非常相似的 API。

import gcsfs

fs = gcsfs.GCSFileSystem()

with fs.open("gs://my_bucket/image1.jpg") as f:
     image = Image.open(f).convert("RGB")

请注意,只要您安装了底层驱动程序,就可以从 fsspec 界面访问这两者,例如:

import fsspec

with fsspec.open('s3://my_s3_bucket/image1.jpg') as f:
    image1 = Image.open(f).convert("RGB")

with fsspec.open('gs://my_gs_bucket/image1.jpg') as f:
    image2 = Image.open(f).convert("RGB")

# fsspec handles local paths too!
with fsspec.open('/Users/myname/Downloads/image1.jpg') as f:
    image3 = Image.open(f).convert("RGB")

fsspec 是 pandas 和其他解析云 URL 的库的文件系统处理程序。以下“正常工作”的原因是因为 fsspec 正在提供云 URI 处理:

pd.read_csv("s3://path/to/my/aws.csv")
pd.read_csv("gs://path/to/my/google.csv")
pd.read_csv("my/local.csv")

关于python - 使用 s3fs 连接到 aws s3 中的谷歌云存储(gcs)相当于什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72552217/

相关文章:

python - 使用 celery 任务创建新进程

kubernetes - 有没有办法在一定的空闲时间后将 GKE 集群的大小调整为 0 个节点?

python - 从服务器端保护 Google Cloud Functions 调用,身份验证策略?

logging - 尝试导出日志时出现"No Log sinks are configured"

node.js - 云存储连接域与API服务器

python - 根据特定列更改 3D 散点图颜色

python重新匹配unicode字符

python - 使用Python中的Turtle模块将缩小的 turtle 向上移动窗口屏幕

google-cloud-platform - "Service account authorization without OAuth",我们可以通过这种方法从谷歌云存储中获取文件吗?

c# - 向谷歌电子表格添加一行时出现异常