Skip to content

Feat(Storage): add samples for support MoveObject #2078

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions storage/src/move_object_atomic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Copyright 2025 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* For instructions on how to run the full sample:
*
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storage/README.md
*/

namespace Google\Cloud\Samples\Storage;

# [START storage_move_object]
use Google\Cloud\Storage\StorageClient;

/**
* Move an object to a new name within HNS-enabled bucket.
*
* @param string $bucketName The name of your Cloud Storage bucket.
* (e.g. 'my-bucket')
* @param string $objectName The name of your Cloud Storage object.
* (e.g. 'my-object')
* @param string $newObjectName the destination object name.
* (e.g. 'my-other-object')
*/
function move_object_atomic(string $bucketName, string $objectName, string $newObjectName): void
{
$storage = new StorageClient();
$bucket = $storage->bucket($bucketName);
$object = $bucket->object($objectName);
$object->move($newObjectName);
printf('Moved gs://%s/%s to gs://%s/%s' . PHP_EOL,
$bucketName,
$objectName,
$bucketName,
$newObjectName);
}
# [END storage_move_object]

// The following 2 lines are only needed to run the samples
require_once __DIR__ . '/../../testing/sample_helpers.php';
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
38 changes: 38 additions & 0 deletions storage/test/ObjectsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,44 @@ public function testManageObject()
$this->assertEquals($output, $outputString);
}

public function testMoveObjectAtomic()
{
$bucketName = self::$bucketName . '-hns';
$objectName = 'test-object-' . time();
$newObjectName = $objectName . '-moved';
$bucket = self::$storage->createBucket($bucketName, [
'hierarchicalNamespace' => ['enabled' => true],
'iamConfiguration' => ['uniformBucketLevelAccess' => ['enabled' => true]]
]);

$object = $bucket->upload('test', ['name' => $objectName]);
$this->assertTrue($object->exists());

$output = self::runFunctionSnippet('move_object_atomic', [
$bucketName,
$objectName,
$newObjectName
]);

$this->assertEquals(
sprintf(
'Moved gs://%s/%s to gs://%s/%s' . PHP_EOL,
$bucketName,
$objectName,
$bucketName,
$newObjectName
),
$output
);

$this->assertFalse($object->exists());
$movedObject = $bucket->object($newObjectName);
$this->assertTrue($movedObject->exists());

$bucket->object($newObjectName)->delete();
$bucket->delete();
}

public function testCompose()
{
$bucket = self::$storage->bucket(self::$bucketName);
Expand Down