Search code examples
ruby-on-railsgoogle-cloud-storage

ruby on rails: error "unknown keyword: :soft_deleted" when download file with google-cloud-storage


bucket_name = "bucket name"
    file_name = 'file_name.csv'

    storage = Google::Cloud::Storage.new(
      project_id: "project id",
    )
      file = storage.bucket(bucket_name).file(file_name)

      file.download(to: "path/to/local/file/#{file_name}")

when I download file with code above, I get error :"unknown keyword: :soft_deleted". I don't know why, please help!

ecpected: I can download file from GCS


Solution

  • Looking at the error, I believe you are trying to download soft-deleted files.

    When you delete an object from your bucket with soft delete enabled, Cloud Storage Moves the deleted object to a soft-deleted state. In this state, the object is invisible to the bucket except when you explicitly list or restore soft-deleted objects.

    Google has introduced this soft delete feature on March 25, 2024. By default, a soft delete feature is enabled for newly created Cloud Storage buckets. This feature allows for the restoration of deleted objects within a set retention period of seven days. However, once this period expires, the objects are permanently deleted from Cloud Storage and can no longer be recovered.

    You might have deleted files accidentally with the soft delete feature enabled and trying to download them. If you want to download those files, you must restore those objects first, if they are still within the retention period.

    You can follow this document for restoring objects and this document to remove the soft delete policy.

    For additional reference check this Cloud Storage API class Google::Cloud::Storage::File.