diff --git a/google-cloud-storage/lib/google/cloud/storage/bucket.rb b/google-cloud-storage/lib/google/cloud/storage/bucket.rb index ca8461354bb7..9b2a49311bbd 100644 --- a/google-cloud-storage/lib/google/cloud/storage/bucket.rb +++ b/google-cloud-storage/lib/google/cloud/storage/bucket.rb @@ -717,6 +717,29 @@ def default_kms_key= new_default_kms_key patch_gapi! :encryption end + ## + # Restart resumable upload + # @param [String, ::File] file Path of the file on the filesystem to + # upload. Can be an File object, or File-like object such as StringIO. + # (If the object does not have path, a `path` argument must be also be + # provided.) + # @param [String] upload_id Unique Id of a resumable upload + # + # @example + # require "google/cloud/storage" + # + # storage = Google::Cloud::Storage.new + # + # bucket = storage.bucket "my-bucket" + # bucket.restart_resumable_upload file,upload_id + + def restart_resumable_upload file, upload_id + ensure_service! + ensure_io_or_file_exists! file + raise "Upload Id missing" unless upload_id + service.restart_resumable_upload name, file, upload_id + end + ## # The period of time (in seconds) that files in the bucket must be # retained, and cannot be deleted, overwritten, or archived. @@ -1410,6 +1433,23 @@ def delete if_metageneration_match: nil, if_metageneration_not_match: nil user_project: user_project end + ## + # Delete resumable upload + # @param [String] upload_id Unique Id of an resumable upload + # + # @example + # require "google/cloud/storage" + # + # storage = Google::Cloud::Storage.new + # + # bucket = storage.bucket "my-bucket" + # bucket.delete_resumable_upload file,upload_id + + def delete_resumable_upload upload_id + ensure_service! + raise "Upload Id missing" unless upload_id + service.delete_resumable_upload name, upload_id + end ## # Retrieves a list of files matching the criteria. # diff --git a/google-cloud-storage/lib/google/cloud/storage/service.rb b/google-cloud-storage/lib/google/cloud/storage/service.rb index d9adf2acff65..d2cf44bde14f 100644 --- a/google-cloud-storage/lib/google/cloud/storage/service.rb +++ b/google-cloud-storage/lib/google/cloud/storage/service.rb @@ -696,6 +696,29 @@ def delete_file bucket_name, end end + def restart_resumable_upload bucket_name, source, upload_id, options: {} + execute do + service.restart_resumable_upload bucket_name, source, upload_id, options: options + end + end + + def delete_resumable_upload bucket_name, upload_id, options: {} + execute do + service.delete_resumable_upload bucket_name, upload_id, options: options + end + end + + ## + # Restore soft deleted bucket + def restore_bucket bucket_name, + generation, + options: {} + execute do + service.restore_bucket bucket_name, generation, + options: options + end + end + ## # Restore soft deleted bucket def restore_bucket bucket_name, diff --git a/google-cloud-storage/test/google/cloud/storage/bucket_test.rb b/google-cloud-storage/test/google/cloud/storage/bucket_test.rb index 203e423467e5..f70fdf5505b9 100644 --- a/google-cloud-storage/test/google/cloud/storage/bucket_test.rb +++ b/google-cloud-storage/test/google/cloud/storage/bucket_test.rb @@ -1407,6 +1407,36 @@ assert_equal "invalid: Source and destination object names must be different.", exception.message end end + + it "restarts a resumable upload with upload_id" do + new_file_name = random_file_path + upload_id= "TEST_ID" + + Tempfile.open ["google-cloud", ".txt"] do |tmpfile| + tmpfile.write "Hello world" + tmpfile.rewind + mock = Minitest::Mock.new + mock.expect :restart_resumable_upload, create_file_gapi(bucket.name, new_file_name), + [bucket.name, tmpfile, upload_id], + **restart_resumable_upload_args(options: {}) + bucket.service.mocked_service = mock + bucket.restart_resumable_upload tmpfile, upload_id + + mock.verify + end + end + + it "deletes a resumable upload with upload_id" do + upload_id= "TEST_ID" + + mock = Minitest::Mock.new + mock.expect :delete_resumable_upload, true, + [bucket.name, upload_id], + **delete_resumable_upload_args(options: {}) + bucket.service.mocked_service = mock + bucket.delete_resumable_upload upload_id + mock.verify + end def create_file_gapi bucket=nil, name = nil Google::Apis::StorageV1::Object.from_json random_file_hash(bucket, name).to_json diff --git a/google-cloud-storage/test/helper.rb b/google-cloud-storage/test/helper.rb index 5d37f92feb26..f5518a4810fc 100644 --- a/google-cloud-storage/test/helper.rb +++ b/google-cloud-storage/test/helper.rb @@ -391,6 +391,18 @@ def insert_object_args name: nil, } end + def delete_resumable_upload_args options: {} + { + options: options + } + end + + def restart_resumable_upload_args options: {} + { + options: options + } + end + def get_object_args generation: nil, if_generation_match: nil, if_generation_not_match: nil,