diff --git a/google-cloud-storage/acceptance/storage/bucket_test.rb b/google-cloud-storage/acceptance/storage/bucket_test.rb index ea99e617db26..36e22bb5a778 100644 --- a/google-cloud-storage/acceptance/storage/bucket_test.rb +++ b/google-cloud-storage/acceptance/storage/bucket_test.rb @@ -385,4 +385,28 @@ _(storage.bucket(hns_bucket_name)).must_be :nil? end + + describe "storage move file" do + let(:source_file) { "file_1_name_#{SecureRandom.hex}.txt" } + let(:destination_file) { "file_2_name_#{SecureRandom.hex}.txt" } + let :create_source_file do + file = StringIO.new "test" + bucket.create_file file, source_file + end + + it "moves a file for bucket" do + create_source_file + bucket.move_file source_file, destination_file + refute_nil(bucket.file(destination_file)) + assert_nil(bucket.file(source_file)) + end + + it "raises error if source and destination are having same filename" do + create_source_file + exception = assert_raises Google::Cloud::InvalidArgumentError do + bucket.move_file source_file, source_file + end + assert_equal "invalid: Source and destination object names must be different.", exception.message + end + end end diff --git a/google-cloud-storage/lib/google/cloud/storage/bucket.rb b/google-cloud-storage/lib/google/cloud/storage/bucket.rb index ca8461354bb7..1c935547c185 100644 --- a/google-cloud-storage/lib/google/cloud/storage/bucket.rb +++ b/google-cloud-storage/lib/google/cloud/storage/bucket.rb @@ -3144,7 +3144,7 @@ def reload! alias refresh! reload! ## - # Moves File from source to destination path within the same HNS-enabled bucket + # Moves File from source to destination path within the bucket. # This Operation is being performed at server side # @param [String] source_file The file name in existing bucket # @param [String] destination_file The new filename to be created on bucket diff --git a/google-cloud-storage/samples/acceptance/buckets_test.rb b/google-cloud-storage/samples/acceptance/buckets_test.rb index 7afc71d14415..295fc0715440 100644 --- a/google-cloud-storage/samples/acceptance/buckets_test.rb +++ b/google-cloud-storage/samples/acceptance/buckets_test.rb @@ -586,32 +586,24 @@ describe "storage move file" do let(:source_file) { "file_1_name_#{SecureRandom.hex}.txt" } let(:destination_file) { "file_2_name_#{SecureRandom.hex}.txt" } - let :hns_bucket do - hierarchical_namespace = Google::Apis::StorageV1::Bucket::HierarchicalNamespace.new enabled: true - storage_client.create_bucket random_bucket_name do |b| - b.uniform_bucket_level_access = true - b.hierarchical_namespace = hierarchical_namespace - end - end let :create_source_file do - file_content = "A" * (3 * 1024 * 1024) # 3 MB of 'A' characters - file = StringIO.new file_content - hns_bucket.create_file file, source_file + file = StringIO.new "test" + bucket.create_file file, source_file end it "file is moved and old file is deleted" do create_source_file out, _err = capture_io do - move_object bucket_name: hns_bucket.name, source_file_name: source_file, destination_file_name: destination_file + move_object bucket_name: bucket.name, source_file_name: source_file, destination_file_name: destination_file end assert_includes out, "New File #{destination_file} created\n" - refute_nil(hns_bucket.file(destination_file)) - assert_nil(hns_bucket.file(source_file)) + refute_nil(bucket.file(destination_file)) + assert_nil(bucket.file(source_file)) end it "raises error if source and destination are having same filename" do create_source_file exception = assert_raises Google::Cloud::InvalidArgumentError do - move_object bucket_name: hns_bucket.name, source_file_name: source_file, destination_file_name: source_file + move_object bucket_name: bucket.name, source_file_name: source_file, destination_file_name: source_file end assert_equal "invalid: Source and destination object names must be different.", exception.message end diff --git a/google-cloud-storage/samples/storage_move_object.rb b/google-cloud-storage/samples/storage_move_object.rb index 727f3dc7d891..1edadaa131a4 100644 --- a/google-cloud-storage/samples/storage_move_object.rb +++ b/google-cloud-storage/samples/storage_move_object.rb @@ -27,8 +27,8 @@ def move_object bucket_name:, source_file_name:, destination_file_name: storage = Google::Cloud::Storage.new bucket = storage.bucket bucket_name, skip_lookup: true - bucket.move_file source_file_name, destination_file_name + fetch_file = bucket.file destination_file_name puts "New File #{fetch_file.name} created\n" end