Search code examples
google-cloud-platformlifecyclegsutil

How to fix the error in GCP CommandException: "lifecycle" command spanning providers not allowed


I am learning GCP now. I have a bucket with the name of welynx-test1_copy I want to set a lifecycle policy to it so as the bucket would be deleted after 23 days, by following the command help I executed the following command in CLI:

xenonxie@cloudshell:~ (rock-perception-263016)$ gsutil ls
gs://rock-perception-263016.appspot.com/
gs://staging.rock-perception-263016.appspot.com/
gs://welynx-test1/
gs://welynx-test1_copy/

So you can see the bucket exists.

Setting the policy below errors me out:

xenonxie@cloudshell:~ (rock-perception-263016)$ gsutil lifecycle set {"rule": [{"action": {"type": "Delete"}, "condition": {"age": 23}}]} gs://welynx-test1_copy

CommandException: "lifecycle" command spanning providers not allowed.

I've tried to follow the syntax found in the help as below:

xenonxie@cloudshell:~ (rock-perception-263016)$ gsutil lifecycle --help NAME lifecycle - Get or set lifecycle configuration for a bucket

SYNOPSIS gsutil lifecycle get url gsutil lifecycle set config-json-file url...

DESCRIPTION The lifecycle command can be used to get or set lifecycle management policies for the given bucket(s). This command is supported for buckets only, not objects. For more information on object lifecycle management, please see the Google Cloud Storage docs <https://cloud.google.com/storage/docs/lifecycle>_.

The lifecycle command has two sub-commands: GET Gets the lifecycle configuration for a given bucket. You can get the lifecycle configuration for only one bucket at a time. The output can be
redirected into a file, edited and then updated via the set sub-command.

SET Sets the lifecycle configuration on one or more buckets. The config-json-file specified on the command line should be a path to a local file containing the lifecycle configuration JSON document.

EXAMPLES The following lifecycle configuration JSON document specifies that all objects in this bucket that are more than 365 days old will be deleted automatically:

{
  "rule":
  [
    {
      "action": {"type": "Delete"},
      "condition": {"age": 365}
    }
  ]
}

The following (empty) lifecycle configuration JSON document removes all lifecycle configuration for a bucket:

{}

What am I missing here and how do I fix it? Thank you very much.


Solution

  • The issue with your command is that you put the rules in the command you want to run instead of the configuration file.

    The way to do it is to:

    • Create a JSON file with the lifecycle configuration rules
    • Use lifecycle set like this gsutil lifecycle set [CONFIG_FILE] gs://[BUCKET_NAME]

    Basically, you can just put as in the example you gave:

    {
      "rule":
      [
        {
          "action": {"type": "Delete"},
          "condition": {"age": 23}
        }
      ]
    }
    

    And change CONFIG_FILE with the JSON file you have created.