Search code examples
google-cloud-storagegsutil

Move files within same GCP storage bucket and keep version history


How do you move files within the same storage bucket to different folders while keeping their version history? I couldn't find any related option for gsutil mv/cp.


Solution

  • Moving objects (e.g., using gsutil mv) is accomplished using a copy followed by a delete, so the moved object is really a new object. There's no way to re-associate the version history of the old object to the new object.

    gsutil cp supports copying noncurrent versions of files and since a move is just a copy followed by a delete, you could first copy with the metadata preservation option and then delete.

    gsutil cp -A gs://bucket-name/oldDir/file gs://bucket-name/newDir/file
    gsutil rm gs://bucket-name/oldDir/file