简体   繁体   中英

Google Cloud Storage - renaming "directories" using the PHP API

I have a Google Cloud Storage bucket that looks like:

bash-3.2$ gsutil ls gs://<my bucket name>/AR7020014/file_manager/

gs://<my bucket name>/AR7020014/file_manager/
gs://<my bucket name>/AR7020014/file_manager/2013-second-half-cancellations.csv
gs://<my bucket name>/AR7020014/file_manager/F59PH_860_High_resolution.jpeg
gs://<my bucket name>/AR7020014/file_manager/HostBuildZoneProcess.txt
gs://<my bucket name>/AR7020014/file_manager/office.jpg
gs://<my bucket name>/AR7020014/file_manager/4 Big McCarthy Lake Road/
gs://<my bucket name>/AR7020014/file_manager/6001 Johntown Creek Rd/
gs://<my bucket name>/AR7020014/file_manager/Listings/
gs://<my bucket name>/AR7020014/file_manager/Local_Utilities/
gs://<my bucket name>/AR7020014/file_manager/Rental_Documents/
gs://<my bucket name>/AR7020014/file_manager/TestBlog/
gs://<my bucket name>/AR7020014/file_manager/TestBlog/bobcat.png
gs://<my bucket name>/AR7020014/file_manager/TestBlog/testfile.png

I'm trying to use the StorageObject rename function. I have the following code:

$bucket_name = '<my bucket name>';
$object_name = 'AR7020014/file_manager/TestBlog';

$bucket = $client->bucket($bucket_name );
$object = $bucket->object( $object_name );

echo "Object found, name = " . $object->name() . "\n";

This produces: "Object found, name = AR7020014/file_manager/TestBlog/";

echo "Object info:\n";
print_r( $object->info() )

Note: If I don't append the "/", the name() function works, but the info() function Throws a "File not found" exception. Inconsistent. But when it works:

Array
(
  [kind] => storage#object
  [id] => <my bucket name>/AR7020014/file_manager/TestBlog//1570061029345142
  [selfLink] => https://www.googleapis.com/storage/v1/b/<my bucket name>/o/AR7020014%2Ffile_manager%2FTestBlog%2F
  [mediaLink] => https://storage.googleapis.com/download/storage/v1/b/<my bucket name>/o/AR7020014%2Ffile_manager%2FTestBlog%2F?generation=1570061029345142&alt=media
  [name] => AR7020014/file_manager/TestBlog/
  [bucket] => <my bucket name>
 << stuff removed for some brevity >>
)

Then:

$new_name = 'AR7020014/file_manager/TestBlog-1';
$new_object = $object->rename( $new_name );
print_r($new_object);

This produces:

 Array
 (
     [kind] => storage#object
     [id] => <my bucket name>/AR7020014/file_manager/TestBlog-1/1603307775687371
     [selfLink] => https://www.googleapis.com/storage/v1/b/<my bucket name>/o/AR7020014%2Ffile_manager%2FTestBlog-1
     [mediaLink] => https://storage.googleapis.com/download/storage/v1/b/<my bucket name>/o/AR7020014%2Ffile_manager%2FTestBlog-1?generation=1603307775687371&alt=media
     [name] => AR7020014/file_manager/TestBlog-1
     [bucket] => <my bucket name>
     << stuff removed for some brevity >>
 )

BUT... The result leaves me with

bash-3.2$ gsutil ls   gs://idx-acnt-gs.ihousedev.com/AR7020014/file_manager

gs://<my bucket name>/AR7020014/file_manager/
gs://<my bucket name>/AR7020014/file_manager/2013-second-half-cancellations.csv
gs://<my bucket name>/AR7020014/file_manager/F59PH_860_High_resolution.jpeg
gs://<my bucket name>/AR7020014/file_manager/HostBuildZoneProcess.txt
gs://<my bucket name>/AR7020014/file_manager/office.jpg
gs://<my bucket name>/AR7020014/file_manager/4 Big McCarthy Lake Road/
gs://<my bucket name>/AR7020014/file_manager/6001 Johntown Creek Rd/
gs://<my bucket name>/AR7020014/file_manager/Listings/
gs://<my bucket name>/AR7020014/file_manager/Local_Utilities/
gs://<my bucket name>/AR7020014/file_manager/Rental_Documents/
gs://<my bucket name>/AR7020014/file_manager/TestBlog/
gs://<my bucket name>/AR7020014/file_manager/TestBlog/bobcat.png
gs://<my bucket name>/AR7020014/file_manager/TestBlog/testfile.png
gs://<my bucket name>/AR7020014/file_manager/TestBlog-1

Leaving my "directory" TestBlog in place, but creating a new "file" named TestBlog-1. We have implemented a virtual file system for our users, which is pretty well done, but for this - renaming directories.

The file structure you see in the GCS is only visual. As mentioned in the GCP documentation :

To the service, the object gs://your-bucket/abc/def.txt is just an object that happens to have "/" characters in its name. There is no "abc" directory; just a single object with the given name.

Therefore, TestBlog in your case is not really an object on its own.

In order to rename the object you will first need to copy it, giving it a new name, and then remove the original:

$bucket = $storage->bucket('[BUCKET-NAME]');
$object = $bucket->object('AR7020014/file_manager/TestBlog/test_file.txt');

$object->copy($bucket, ['name' => 'AR7020014/file_manager/TestBlog-1/test_file.txt']);
$object->delete();

As you have multiple objects under TestBlog , you will need to iterate through them, renaming them one by one.


This can also be done with the gsutil tool:

gsutil mv -r \
gs://[BUCKET-NAME]/AR7020014/file_manager/TestBlog/* \
gs://[BUCKET-NAME]/AR7020014/file_manager/TestBlog-1/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM