How to Rename or Move a File to Google Cloud Storage (PHP API)

I am currently trying to rename and / or move the cloud storage file to a different name / position, but I cannot get it to work. I am using https://github.com/google/google-api-php-client as client, download works great:

...
$storageService = new \Google_Service_Storage( $client )
$file = new \Google_Service_Storage_StorageObject()
$file->setName( 'test.txt' );
$storageService->objects->insert(
   $bucketName,
    $file,
    array(
        'name'          => $filename,
        'data'          => file_get_contents( $somefile )
    )
);
...

      

So, I tried to change the filename with the $ storageObject-> objects-> update () method, but I can't find any documentation on this. I used $ storageService-> objects-> get ($ bucketName, $ fileName) to get the specific file that I wanted to rename (with $ file-> setName ()), but it seems like I just can't seem to pass the file to the objects. > update function. Am I doing it wrong?

+1


source to share


3 answers


Ok, it seems I can't directly rename the file (please correct me if I'm wrong), I could only update the metadata. I managed to get it working by copying the file to the new filename / destination and then deleting the old file. For this I have successfully used $ storageService-> objects-> copy and $ storageService-> objects-> delete. It doesn't seem right, but at least it works.



+5


source


Since this is not well documented by google, here's a basic example:



//RENAME FILE ON GOOGLE CLOUD STORAGE (GCS)

//Get client and auth token (might vary depending on the way you connect to gcs – here with laravel framework facade)
//DOC: https://cloud.google.com/storage/docs/json_api/v1/json-api-php-samples
//DOC: https://developers.google.com/api-client-library/php/auth/service-accounts
//Laravel Client: https://github.com/pulkitjalan/google-apiclient 
//Get google client
$gc = \Google::getClient();
//Get auth token if it is not valid/not there yet
if($gc->isAccessTokenExpired())
    $gc->getAuth()->refreshTokenWithAssertion();
//Get google cloud storage service with the client
$gcStorageO = new \Google_Service_Storage($gc);

//GET object at old position ($path)
//DOC: https://cloud.google.com/storage/docs/json_api/v1/objects/get
$oldObj = $gcStorageO->objects->get($bucket, $path);

//COPY desired object from old position ($path) to new position ($newpath)
//DOC: https://cloud.google.com/storage/docs/json_api/v1/objects/copy
$gcStorageO->objects->copy(
    $bucket, $path, 
    $bucket, $newpath,
    $oldObj
);

//DELETE old object ($path)
//DOC: https://cloud.google.com/storage/docs/json_api/v1/objects/delete
$gcStorageO->objects->delete($bucket, $path);

      

0


source


I found that when using gcutils in combination with PHP, you can execute almost all php file commands in your application. Copy, delete, check if the file exists.

if(file_exists("gs://$bucket/{$folder}/$old_temp_file")){

        $old_path = "gs://$bucket/{$folder}/$old_temp_file";
        $new_permanent_path = "gs://$bucket/{$folder}/$new_permanent_file";

        copy($old_path, $new_permanent_path);  

        unlink($old_path);

    }

      

0


source







All Articles