Google Cloud Storage 提供了以下选项的存取控制:
IAM权限:允许存取 Project 里的所有 bucket 和 object。通过 IAM,您可以控制对整个 project 所有 bucket 和 object 的存取权限, 但是无法针对个别 bucket 或是 object 做控制。
Access control list(ACL):允许对单个 bucket 和 objects 进行读写的控制。当您需详细地控制个别的资源时,可以使用 ACL。
Signed URL:通过产生的 URL 网址可以进行存取和读写。无论取得共享网址的人是否拥有 Google 帐户,每个取得共享网址的人皆可在特定的时间存取 object。
Signed policy document:指定可以上传到 bucket 的内容。在 Policy 文件中,可以比 Signed URL 更精准地掌握例如:文件大小、内容类型等上传属性。网站拥有者可通过 Signed policy document 允许访问者将文件上传到 Google Cloud Storage。
本文将介绍如何是用第三种方法:Signed URL 来对 Object 进行上传下载。
一 、创建 Service Account
Service Account 是 Machine to Machine 通讯时,权限管理的账号。我们通过以下三步获得具有存储管理权限的 Service Account:
创建 Service Account
授权 Service Account
下载 Service Account Key 文件
在客户端激活 Service Account
1 创建 Service Account
在 GCP 的 Console 中选择 IAM->Service Account,选择 Create Service Account:



2 授权 Service Account
创建好 Service Account 后,在 IAM 中对这个 Service Account 进行授权。


给这个 Service Account 分配 Storage Admin 的权限。此时在 GCS 的 Permission 中可以看到这个 Service Account 对每个 Bucket 都有了 Admin 的权限。
也可以采用真的每个 Bucket 单独授权的模式,在 IAM 中取消这个 Service Account 的权限。进入到 GCS 管理界面,针对每个 Bucket 进行授权。
3 下载 Service Account Key 文件
在 IAM 的 Service Account 中创建 Service Account Key:


这个文件会下载到本地。
4 在客户端激活 Service Account
在安装有 gcloud 客户端的机器上激活 Service Account:

二 采用 Signed URL 下载文件
1 查看下载文件
通过 GCS 的命令行工具 gsutil,创建 Signed URL 的链接。
我们对非 public 对文件进行 Signed URL 操作:

2 生成 Signed URL
在命令行中:

可以看到 GCP 返回 Signed URL 链接:

3 使用 Signed URL 下载文件
用浏览器打开这个链接,可以直接下载;也可以用 curl 命令手工下载:


三 用 Signed URL 上传文件
1 生成一个上传文件

2 使用 gsutil 生成 signed URL

3 上传文件

可以看到上传成功。
四 用 Signed URL 实现断点续传上传文件
GCS Signed URL 支持断点续传。具体实现方式如下:
1 生成文件

2 gsutil 生成 Signed URL

得到如下返回

3 生成上传链接

返回如下:

将上传地址取出:

4 上传并中断

中断上传

5 查看上传的大小

返回值中有 Range 信息:

6 生成剩余文件

7 上传剩余文件

8 验证

上传的文件和本地文件一致。
五 总结
通过 Signed URL 可以实现:
下载
上传
支持断点续传的上传
文章信息
相关推荐
