2
votes

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.

2

2 Answers

1
votes

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.

0
votes

Apparently, gsutil checks to see if the bucket name belongs to google before it checks to see if the lifecycle file exists:

❯ gsutil lifecycle set foo bar gs://baz
CommandException: "lifecycle" command spanning providers not allowed.

❯ gsutil lifecycle set foo gs://baz
AccessDeniedException: 403 [email protected] does not have storage.buckets.get access to baz.

❯ gsutil lifecycle set foo gs://a-real-bucket-name
Setting lifecycle configuration on gs://a-real-bucket-name/...
ArgumentException: JSON lifecycle data could not be loaded from:

So if you provide anything other than a google-controlled bucket in the fifth position:

gsutil lifecycle set file.json THIS_ARGUMENT

You'll see errors relating to that problem instead of errors relating to the file.

This confused me as well, I think google could make some simple modifications to gsutil to make the error messages more helpful. I've filed a bug to that effect here: https://issuetracker.google.com/issues/147020031