簡體   English   中英

重命名谷歌雲存儲中的文件?

[英]renaming files in Google Cloud Storage?

您可以重命名 Google Cloud Storage 中的文件嗎?

我允許用戶上傳照片,但我想讓他們能夠編輯照片,至少通過更改他們上傳照片時使用的名稱。 我正在使用 Javascript 到 Node.js。

謝謝!

這個命令:

gsutil mv gs://my_bucket/olddir gs://my_bucket/newdir

會做的。 如果您有大量文件需要進行多處理移動,請使用以下命令:

gsutil -m mv gs://my_bucket/olddir gs://my_bucket/newdir

您可以在文檔中找到此信息。

是的。 您可以使用mv命令將其復制到一個新名稱。 下面將file.html重命名為file

gsutil mv gs://xxxx/folder/file.html gs://xxxx/folder/file

有關更多信息,請參閱位於mv - Move/rename objects的 Google 的 gsutil 文檔。

您可以使用 storage.move() 方法重命名文件。

const {Storage} = require('@google-cloud/storage');
const storage = new Storage();

const oldFileName = 'path/to/old_file.jpg';
const newFileName = 'path/to/new_file.jpg';

storage
  .bucket(bucketName)
  .file(oldFileName)
  .move(newFileName)
  .then(() => {
    console.log(`File ${oldFileName} was renamed to ${newFileName}.`);
  })
  .catch(err => {
    console.error('Error renaming file:', err);
  });

bucketName是文件所在的桶的名稱。

oldFileName是文件的當前名稱。

newFileName是您要為文件指定的新名稱。

你應該有適當的權限。


這是Python函數執行多線程/多處理移動:

from subprocess import call, CalledProcessError

def rename_object(self, old_dir, new_dir):
    base_cmd = ["gsutil", "-m", "mv"]
    old_path = "gs://{}".format(old_dir)
    new_path = "gs://{}".format(new_dir)
    cmd = base_cmd + [old_path, new_path]
    try:
        call(cmd)
    except CalledProcessError as e:
        print e

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM