Google Cloud Storage

Google Cloud Storage

路径指定为 remote:bucket(对于 lsd 命令,可使用 remote:)。你也可以指定子目录,例如 remote:bucket/path/to/dir

配置

Google Cloud Storage 的初始设置需要你在浏览器中从 Google Cloud Storage 获取一个令牌。rclone config 命令会引导你完成这个过程。

以下是一个创建名为 remote 的远程存储的示例。首先运行:

 rclone config

这将引导你完成一个交互式的设置过程:

n) New remote
d) Delete remote
q) Quit config
e/n/d/q> n
name> remote
Type of storage to configure.
Choose a number from below, or type in your own value
[snip]
XX / Google Cloud Storage (this is not Google Drive)
   \ "google cloud storage"
[snip]
Storage> google cloud storage
Google Application Client Id - leave blank normally.
client_id>
Google Application Client Secret - leave blank normally.
client_secret>
Project number optional - needed only for list/create/delete buckets - see your developer console.
project_number> 12345678
Service Account Credentials JSON file path - needed only if you want use SA instead of interactive login.
service_account_file>
Access Control List for new objects.
Choose a number from below, or type in your own value
 1 / Object owner gets OWNER access, and all Authenticated Users get READER access.
   \ "authenticatedRead"
 2 / Object owner gets OWNER access, and project team owners get OWNER access.
   \ "bucketOwnerFullControl"
 3 / Object owner gets OWNER access, and project team owners get READER access.
   \ "bucketOwnerRead"
 4 / Object owner gets OWNER access [default if left blank].
   \ "private"
 5 / Object owner gets OWNER access, and project team members get access according to their roles.
   \ "projectPrivate"
 6 / Object owner gets OWNER access, and all Users get READER access.
   \ "publicRead"
object_acl> 4
Access Control List for new buckets.
Choose a number from below, or type in your own value
 1 / Project team owners get OWNER access, and all Authenticated Users get READER access.
   \ "authenticatedRead"
 2 / Project team owners get OWNER access [default if left blank].
   \ "private"
 3 / Project team members get access according to their roles.
   \ "projectPrivate"
 4 / Project team owners get OWNER access, and all Users get READER access.
   \ "publicRead"
 5 / Project team owners get OWNER access, and all Users get WRITER access.
   \ "publicReadWrite"
bucket_acl> 2
Location for the newly created buckets.
Choose a number from below, or type in your own value
 1 / Empty for default location (US).
   \ ""
 2 / Multi-regional location for Asia.
   \ "asia"
 3 / Multi-regional location for Europe.
   \ "eu"
 4 / Multi-regional location for United States.
   \ "us"
 5 / Taiwan.
   \ "asia-east1"
 6 / Tokyo.
   \ "asia-northeast1"
 7 / Singapore.
   \ "asia-southeast1"
 8 / Sydney.
   \ "australia-southeast1"
 9 / Belgium.
   \ "europe-west1"
10 / London.
   \ "europe-west2"
11 / Iowa.
   \ "us-central1"
12 / South Carolina.
   \ "us-east1"
13 / Northern Virginia.
   \ "us-east4"
14 / Oregon.
   \ "us-west1"
location> 12
The storage class to use when storing objects in Google Cloud Storage.
Choose a number from below, or type in your own value
 1 / Default
   \ ""
 2 / Multi-regional storage class
   \ "MULTI_REGIONAL"
 3 / Regional storage class
   \ "REGIONAL"
 4 / Nearline storage class
   \ "NEARLINE"
 5 / Coldline storage class
   \ "COLDLINE"
 6 / Durable reduced availability storage class
   \ "DURABLE_REDUCED_AVAILABILITY"
storage_class> 5
Remote config
Use web browser to automatically authenticate rclone with remote?
 * Say Y if the machine running rclone has a web browser you can use
 * Say N if running rclone on a (remote) machine without web browser access
If not sure try Y. If Y failed, try N.
y) Yes
n) No
y/n> y
If your browser doesn't open automatically go to the following link: http://127.0.0.1:53682/auth
Log in and authorize rclone for access
Waiting for code...
Got code
Configuration complete.
Options:
- type: google cloud storage
- client_id:
- client_secret:
- token: {"AccessToken":"xxxx.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","RefreshToken":"x/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx_xxxxxxxxx","Expiry":"2014-07-17T20:49:14.929208288+01:00","Extra":null}
- project_number: 12345678
- object_acl: private
- bucket_acl: private
Keep this "remote" remote?
y) Yes this is OK
e) Edit this remote
d) Delete this remote
y/e/d> y

请参阅 远程设置文档,了解如何在没有互联网浏览器的机器上进行设置。

请注意,如果使用网络浏览器自动进行身份验证,rclone 会在本地机器上运行一个 Web 服务器,用于收集 Google 返回的令牌。这个 Web 服务器仅在打开浏览器到你获取验证代码的这段时间内运行。它的地址是 http://127.0.0.1:53682/,如果你正在运行主机防火墙,可能需要临时解除对该地址的阻止,或者使用手动模式。

这个远程存储名为 remote,现在可以像这样使用:

查看项目中的所有存储桶

rclone lsd remote:

创建一个新的存储桶

rclone mkdir remote:bucket

列出存储桶的内容

rclone ls remote:bucket

/home/local/directory 同步到远程存储桶,并删除存储桶中多余的文件。

rclone sync --interactive /home/local/directory remote:bucket

服务账户支持

你可以以无人值守模式将 rclone 与 Google Cloud Storage 进行设置,即不绑定到特定的最终用户 Google 账户。当你想将文件同步到没有活跃登录用户的机器(例如构建机器)时,这非常有用。

要获取 Google Cloud Platform IAM 服务账户 的凭据,请前往 Google 开发者控制台的 服务账户 部分。服务账户在 Google Cloud Storage ACL 中的行为与普通 用户 权限相同,因此你可以限制它们的访问权限(例如,使其只读)。创建账户后,包含服务账户凭据的 JSON 文件将下载到你的机器上。rclone 将使用这些凭据进行身份验证。

要使用服务账户而不是 OAuth2 令牌流程,请在 service_account_file 提示处输入服务账户凭据的路径,rclone 将不会使用基于浏览器的身份验证流程。如果你想将凭据文件的内容直接放入 rclone 配置文件中,可以将 service_account_credentials 设置为文件的实际内容,或者设置等效的环境变量。

使用访问令牌进行服务账户身份验证

另一种服务账户身份验证的方法是通过 gcloud impersonate-service-account 使用访问令牌。访问令牌通过避免使用可能被泄露的 JSON 密钥文件来保护安全。它们还绕过了 OAuth 登录流程,这对于没有网络浏览器的远程虚拟机来说更加简单。

如果你已经有一个可用的服务账户,请跳至步骤 3。

1. 使用以下命令创建一个服务账户

gcloud iam service-accounts create gcs-read-only 

你也可以重复使用现有的服务账户(例如上面创建的那个)

2. 为服务账户附加查看者(只读)或用户(读写)角色

 $ PROJECT_ID=my-project
 $ gcloud --verbose iam service-accounts add-iam-policy-binding \
    gcs-read-only@${PROJECT_ID}.iam.gserviceaccount.com  \
    --member=serviceAccount:gcs-read-only@${PROJECT_ID}.iam.gserviceaccount.com \
    --role=roles/storage.objectViewer

使用 Google Cloud 控制台来确定一个受限角色。一些相关的预定义角色如下:

  • roles/storage.objectUser – 读写访问权限,但无管理权限
  • roles/storage.objectViewer – 对对象的只读访问权限
  • roles/storage.admin – 创建存储桶和管理角色

3. 获取服务账户的临时访问密钥

$ gcloud auth application-default print-access-token  \
   --impersonate-service-account \
      gcs-read-only@${PROJECT_ID}.iam.gserviceaccount.com  

ya29.c.c0ASRK0GbAFEewXD [已截断]

4. 更新 access_token 设置

当你看到 正在等待代码 时,按下 CTRL-C。这将保存配置而不进行 OAuth 流程。

rclone config update ${REMOTE_NAME} access_token ya29.c.c0Axxxx

5. 像往常一样运行 rclone

rclone ls dev-gcs:${MY_BUCKET}/

关于服务账户的更多信息

匿名访问

对于允许公共访问的对象下载,你可以通过将 anonymous 设置为 true 来配置 rclone 使用匿名访问。 在未经授权的访问下,你不能写入或创建文件,只能读取或列出那些具有公共读取访问权限的存储桶和对象。

应用默认凭据

如果没有提供其他凭据来源,rclone 将回退到 应用默认凭据。这在你已经为开发者账户配置了身份验证,或者在 Google 计算主机上运行生产环境时都很有用。请注意,如果在 Docker 中运行,你可能需要在 Google 计算机器上运行额外的命令 - 请参阅此页面

请注意,在使用应用默认凭据的情况下,无需显式配置项目编号。

–fast-list

此远程存储支持 --fast-list,它允许你以使用更多内存为代价来减少事务次数。有关更多详细信息,请参阅 rclone 文档

自定义上传头部

你可以使用 --header-upload 标志来设置自定义上传头部。Google Cloud Storage 支持 处理元数据文档 中描述的头部。

  • Cache-Control
  • Content-Disposition
  • Content-Encoding
  • Content-Language
  • Content-Type
  • X-Goog-Storage-Class
  • X-Goog-Meta-

例如 --header-upload "Content-Type text/potato"

请注意,最后一个头部用于以 --header-upload "x-goog-meta-key: value" 的形式设置自定义元数据。

修改时间

Google Cloud Storage 原生存储 md5 校验和。 Google 的 gsutil 工具将修改时间以一秒精度存储为文件元数据中的 goog-reserved-file-mtime

为了确保与 gsutil 兼容,rclone 将修改时间存储在两个单独的元数据条目中。 mtime 使用具有一纳秒精度的 RFC3339 格式。 goog-reserved-file-mtime 使用具有一秒精度的 Unix 时间戳格式。 为了从对象元数据中获取修改时间,rclone 按以下顺序读取元数据:mtimegoog-reserved-file-mtime、对象更新时间。

请注意,rclone 的默认修改窗口为 1 纳秒。 由 gsutil 上传的文件仅包含具有一秒精度的时间戳。 如果你使用 rclone 同步之前由 gsutil 上传的文件,rclone 将尝试更新所有这些文件的修改时间。 为了避免这些可能不必要的更新,请使用 --modify-window 1s

受限文件名字符

字符 替换字符
NUL 0x00
LF 0x0A
CR 0x0D
/ 0x2F

无效的 UTF-8 字节也将被 替换,因为它们不能用于 JSON 字符串。

标准选项

以下是 Google Cloud Storage(Google 云存储,不是 Google Drive)特定的标准选项。

–gcs-client-id

OAuth 客户端 ID。

通常留空。

属性:

  • 配置项:client_id
  • 环境变量:RCLONE_GCS_CLIENT_ID
  • 类型:字符串
  • 是否必需:否

–gcs-client-secret

OAuth 客户端密钥。

通常留空。

属性:

  • 配置项:client_secret
  • 环境变量:RCLONE_GCS_CLIENT_SECRET
  • 类型:字符串
  • 是否必需:否

–gcs-project-number

项目编号。

可选 - 仅在列出/创建/删除存储桶时需要 - 请查看你的开发者控制台。

属性:

  • 配置项:project_number
  • 环境变量:RCLONE_GCS_PROJECT_NUMBER
  • 类型:字符串
  • 是否必需:否

–gcs-user-project

用户项目。

可选 - 仅在请求者付费时需要。

属性:

  • 配置项:user_project
  • 环境变量:RCLONE_GCS_USER_PROJECT
  • 类型:字符串
  • 是否必需:否

–gcs-service-account-file

服务账户凭据 JSON 文件路径。

通常留空。 仅在你想使用服务账户而不是交互式登录时需要。

文件名中的前导 ~ 以及环境变量(如 ${RCLONE_CONFIG_DIR})将被展开。

属性:

  • 配置项:service_account_file
  • 环境变量:RCLONE_GCS_SERVICE_ACCOUNT_FILE
  • 类型:字符串
  • 是否必需:否

–gcs-service-account-credentials

服务账户凭据 JSON 数据。

通常留空。 仅在你想使用服务账户而不是交互式登录时需要。

属性:

  • 配置项:service_account_credentials
  • 环境变量:RCLONE_GCS_SERVICE_ACCOUNT_CREDENTIALS
  • 类型:字符串
  • 是否必需:否

–gcs-anonymous

在不使用凭据的情况下访问公共存储桶和对象。

如果你只想下载文件且不配置凭据,请设置为 ’true’。

属性:

  • 配置项:anonymous
  • 环境变量:RCLONE_GCS_ANONYMOUS
  • 类型:布尔值
  • 默认值:false

–gcs-object-acl

新对象的访问控制列表。

属性:

  • 配置项:object_acl
  • 环境变量:RCLONE_GCS_OBJECT_ACL
  • 类型:字符串
  • 是否必需:否
  • 示例:
    • “authenticatedRead”
      • 对象所有者拥有所有者访问权限。
      • 所有经过身份验证的用户拥有读取者访问权限。
    • “bucketOwnerFullControl”
      • 对象所有者拥有所有者访问权限。
      • 项目团队所有者拥有所有者访问权限。
    • “bucketOwnerRead”
      • 对象所有者拥有所有者访问权限。
      • 项目团队所有者拥有读取者访问权限。
    • “private”
      • 对象所有者拥有所有者访问权限。
      • 若留空则为默认值。
    • “projectPrivate”
      • 对象所有者拥有所有者访问权限。
      • 项目团队成员根据其角色获得访问权限。
    • “publicRead”
      • 对象所有者拥有所有者访问权限。
      • 所有用户拥有读取者访问权限。

–gcs-bucket-acl

新存储桶的访问控制列表。

属性:

  • 配置项:bucket_acl
  • 环境变量:RCLONE_GCS_BUCKET_ACL
  • 类型:字符串
  • 是否必需:否
  • 示例:
    • “authenticatedRead”
      • 项目团队所有者拥有所有者访问权限。
      • 所有经过身份验证的用户拥有读取者访问权限。
    • “private”
      • 项目团队所有者拥有所有者访问权限。
      • 若留空则为默认值。
    • “projectPrivate”
      • 项目团队成员根据其角色获得访问权限。
    • “publicRead”
      • 项目团队所有者拥有所有者访问权限。
      • 所有用户拥有读取者访问权限。
    • “publicReadWrite”
      • 项目团队所有者拥有所有者访问权限。
      • 所有用户拥有写入者访问权限。

–gcs-bucket-policy-only

访问检查应使用存储桶级别的 IAM 策略。

如果你想将对象上传到已设置存储桶策略仅有的存储桶,则需要设置此选项。

设置后,rclone 会:

  • 忽略存储桶上设置的 ACL
  • 忽略对象上设置的 ACL
  • 创建具有存储桶策略仅有的存储桶

文档:https://cloud.google.com/storage/docs/bucket-policy-only

属性:

  • 配置项:bucket_policy_only
  • 环境变量:RCLONE_GCS_BUCKET_POLICY_ONLY
  • 类型:布尔值
  • 默认值:false

–gcs-location

新创建存储桶的位置。

属性:

  • 配置项:location
  • 环境变量:RCLONE_GCS_LOCATION
  • 类型:字符串
  • 是否必需:否
  • 示例:
    • ""
      • 留空表示默认位置(美国)
    • “asia”
      • 亚洲多区域位置
    • “eu”
      • 欧洲多区域位置
    • “us”
      • 美国多区域位置
    • “asia-east1”
      • 台湾
    • “asia-east2”
      • 香港
    • “asia-northeast1”
      • 东京
    • “asia-northeast2”
      • 大阪
    • “asia-northeast3”
      • 首尔
    • “asia-south1”
      • 孟买
    • “asia-south2”
      • 德里
    • “asia-southeast1”
      • 新加坡
    • “asia-southeast2”
      • 雅加达
    • “australia-southeast1”
      • 悉尼
    • “australia-southeast2”
      • 墨尔本
    • “europe-north1”
      • 芬兰
    • “europe-west1”
      • 比利时
    • “europe-west2”
      • 伦敦
    • “europe-west3”
      • 法兰克福
    • “europe-west4”
      • 荷兰
    • “europe-west6”
      • 苏黎世
    • “europe-central2”
      • 华沙
    • “us-central1”
      • 爱荷华州
    • “us-east1”
      • 南卡罗来纳州
    • “us-east4”
      • 北弗吉尼亚州
    • “us-west1”
      • 俄勒冈州
    • “us-west2”
      • 加利福尼亚州
    • “us-west3”
      • 盐湖城
    • “us-west4”
      • 拉斯维加斯
    • “northamerica-northeast1”
      • 蒙特利尔
    • “northamerica-northeast2”
      • 多伦多
    • “southamerica-east1”
      • 圣保罗
    • “southamerica-west1”
      • 圣地亚哥
    • “asia1”
      • 双区域:亚洲东北 1 和亚洲东北 2。
    • “eur4”
      • 双区域:欧洲北部 1 和欧洲西部 4。
    • “nam4”
      • 双区域:美国中部 1 和美国东部 1。

–gcs-storage-class

在 Google Cloud Storage 中存储对象时使用的存储类。

属性:

  • 配置项:storage_class
  • 环境变量:RCLONE_GCS_STORAGE_CLASS
  • 类型:字符串
  • 是否必需:否
  • 示例:
    • ""
      • 默认值
    • “MULTI_REGIONAL”
      • 多区域存储类
    • “REGIONAL”
      • 区域存储类
    • “NEARLINE”
      • 近线存储类
    • “COLDLINE”
      • 冷线存储类
    • “ARCHIVE”
      • 存档存储类
    • “DURABLE_REDUCED_AVAILABILITY”
      • 持久化低可用性存储类

–gcs-env-auth

从运行时(环境变量或实例元数据,如果没有环境变量)获取 GCP IAM 凭据。

仅在 service_account_file 和 service_account_credentials 为空时适用。

属性:

  • 配置项:env_auth
  • 环境变量:RCLONE_GCS_ENV_AUTH
  • 类型:布尔值
  • 默认值:false
  • 示例:
    • “false”
      • 在下一个步骤中输入凭据。
    • “true”
      • 从环境(环境变量或 IAM)中获取 GCP IAM 凭据。

高级选项

以下是 Google Cloud Storage(Google 云存储,不是 Google Drive)特定的高级选项。

–gcs-token

作为 JSON 数据的 OAuth 访问令牌。

属性:

  • 配置项:token
  • 环境变量:RCLONE_GCS_TOKEN
  • 类型:字符串
  • 是否必需:否

–gcs-auth-url

认证服务器 URL。

留空以使用提供商的默认值。

属性:

  • 配置项:auth_url
  • 环境变量:RCLONE_GCS_AUTH_URL
  • 类型:字符串
  • 是否必需:否

–gcs-token-url

令牌服务器 URL。

留空以使用提供商的默认值。

属性:

  • 配置项:token_url
  • 环境变量:RCLONE_GCS_TOKEN_URL
  • 类型:字符串
  • 是否必需:否

–gcs-client-credentials

使用客户端凭据 OAuth 流程。

这将使用 RFC 6749 中描述的 OAUTH2 客户端凭据流程。

属性:

  • 配置项:client_credentials
  • 环境变量:RCLONE_GCS_CLIENT_CREDENTIALS
  • 类型:布尔值
  • 默认值:false

–gcs-access-token

短期访问令牌。

通常留空。 仅在你想使用短期访问令牌而不是交互式登录时需要。

属性:

  • 配置项:access_token
  • 环境变量:RCLONE_GCS_ACCESS_TOKEN
  • 类型:字符串
  • 是否必需:否

–gcs-directory-markers

在创建新目录时,上传一个带有尾随斜杠的空对象

基于存储桶的远程存储不支持空文件夹,此选项会创建一个以 “/” 结尾的空对象来保留文件夹。

属性:

  • 配置项:directory_markers
  • 环境变量:RCLONE_GCS_DIRECTORY_MARKERS
  • 类型:布尔值
  • 默认值:false

–gcs-no-check-bucket

如果设置,则不尝试检查存储桶是否存在或创建它。

如果你知道存储桶已经存在,并且希望尽量减少 rclone 执行的事务数量,那么此选项会很有用。

属性:

  • 配置项:no_check_bucket
  • 环境变量:RCLONE_GCS_NO_CHECK_BUCKET
  • 类型:布尔值
  • 默认值:false

–gcs-decompress

如果设置,将解压缩 gzip 编码的对象。

可以将对象以 “Content-Encoding: gzip” 设置上传到 GCS。通常 rclone 会将这些文件作为压缩对象下载。

如果设置了此标志,rclone 将在接收到这些带有 “Content-Encoding: gzip” 的文件时对其进行解压缩。这意味着 rclone 无法检查文件的大小和哈希值,但文件内容将被解压缩。

属性:

  • 配置项:decompress
  • 环境变量:RCLONE_GCS_DECOMPRESS
  • 类型:布尔值
  • 默认值:false

–gcs-endpoint

服务的端点。

通常留空。

属性:

  • 配置项:endpoint
  • 环境变量:RCLONE_GCS_ENDPOINT
  • 类型:字符串
  • 是否必需:否

–gcs-encoding

后端的编码方式。

有关更多信息,请参阅 概述中的编码部分

属性:

  • 配置项:encoding
  • 环境变量:RCLONE_GCS_ENCODING
  • 类型:Encoding
  • 默认值:Slash,CrLf,InvalidUtf8,Dot

–gcs-description

远程存储的描述。

属性:

  • 配置项:description
  • 环境变量:RCLONE_GCS_DESCRIPTION
  • 类型:字符串
  • 是否必需:否

限制

Google Cloud Storage 后端不支持 rclone about 命令。没有此功能的后端无法确定 rclone 挂载的可用空间,也不能在 rclone 联合远程存储中使用 mfs(最大可用空间)策略。

请参阅 不支持 rclone about 的后端列表rclone about