如何重命名或移动Google云端存储中的文件(PHP API) [英] How to rename or move a file in Google Cloud Storage (PHP API)
问题描述
我目前正尝试将云存储文件重命名和/或移动到另一个名称/位置,但我无法使其工作。我正在使用 https://github.com/google/google-api-php-client 作为客户端,上传可以正常工作:
I am currently trying to rename and/or move a cloud storage file to another name/position, but I can't get it to work. I am using https://github.com/google/google-api-php-client as client, the uploads works fine with:
...
$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 )
)
);
...
所以我试图通过$ storageObject-> objects-> update()方法,但我找不到任何文档。我使用$ storageService-> objects-> get($ bucketName,$ fileName)来获取我想重命名的特定文件(使用$ file-> setName()),但似乎我只是无法将文件传递给对象 - >更新功能。我做错了吗?
So I have tried to change a filename by the $storageObject->objects->update() method, but I cannot find any documentation on this. I used $storageService->objects->get( $bucketName, $fileName ) to get a specific file I wanted to rename (with $file->setName()), but it seems I just cannot pass the file to the objects->update function. Am I doing it wrong?
推荐答案
好吧,我似乎无法直接重命名一个文件(请纠正我,如果我错了),我只能更新元数据。我设法通过将文件复制到新的文件名/目的地然后删除旧文件来实现它。我为此成功使用了$ storageService-> objects-> copy和$ storageService-> objects-> delete。这感觉不对,但至少它工作。
Ok, it seems I cannot directly rename a file (please correct me if I'm wrong), I could only update the metadata. I managed to get it to work by copying the file to a new filename/destination and then delete the old file. I successfully used $storageService->objects->copy and $storageService->objects->delete for this. This doesn't feels right but at least it works.
这篇关于如何重命名或移动Google云端存储中的文件(PHP API)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!