使用nodejs gcloud api在谷歌云存储中移动/重命名文件夹。

时间:2022-12-20 15:25:57

I am trying to rename or move a folder in google cloud storage using the gcloud api.

我正在尝试使用gcloud api重命名或移动谷歌云存储中的一个文件夹。

A similar question explains how to delete a folder: Delete folder in Google Cloud Storage using nodejs gcloud api

一个类似的问题解释了如何删除文件夹:使用nodejs gcloud api在谷歌云存储中删除文件夹

But how can one rename a folder? or move to another path?

但是如何重命名一个文件夹呢?还是换一条路?

2 个解决方案

#1


2  

You can try something like this:

你可以试试这样的方法:

'use strict'

var async = require('async')
var storage = require('@google-cloud/storage')()
var bucket = storage.bucket('stephen-has-a-new-bucket')

bucket.renameFolder = function(source, dest, callback) {
  bucket.getFiles({ prefix: source }, function(err, files) {
    if (err) return callback(err)

    async.eachLimit(files, 5, function(file, next) {
      file.move(file.name.replace(source, dest), next)
    }, callback)
  })
}

bucket.renameFolder('photos/cats', 'photos/dogs', console.log)

#2


1  

There are no folders. There is simply a collection of objects that all happen to have the same key prefix, for example photos/animals/cat.png and photos/animals/dog.png both have a common prefix photos/animals/ and that's what makes them appear to be in the same folder.

没有文件夹。有一个简单的对象集合,它们都有相同的键前缀,例如照片/动物/猫。png和照片/动物/狗。png都有一个公共的前缀photos/animals/这就是为什么它们看起来在同一个文件夹中。

You will need to copy (or move) each of the objects to its new key, for example move photos/animals/cat.png to photos/pets/cat.png and move photos/animals/dog.png to photos/pets/dog.png.

您需要将每个对象复制(或移动)到它的新键,例如移动照片/动物/猫。png图片/宠物猫。png和移动照片/动物/狗。png图片/宠物/ dog.png。

That said, Google Cloud provides a way to do this from the command line using gsutil mv.

也就是说,谷歌Cloud提供了一种使用gsutil mv从命令行执行此操作的方法。

#1


2  

You can try something like this:

你可以试试这样的方法:

'use strict'

var async = require('async')
var storage = require('@google-cloud/storage')()
var bucket = storage.bucket('stephen-has-a-new-bucket')

bucket.renameFolder = function(source, dest, callback) {
  bucket.getFiles({ prefix: source }, function(err, files) {
    if (err) return callback(err)

    async.eachLimit(files, 5, function(file, next) {
      file.move(file.name.replace(source, dest), next)
    }, callback)
  })
}

bucket.renameFolder('photos/cats', 'photos/dogs', console.log)

#2


1  

There are no folders. There is simply a collection of objects that all happen to have the same key prefix, for example photos/animals/cat.png and photos/animals/dog.png both have a common prefix photos/animals/ and that's what makes them appear to be in the same folder.

没有文件夹。有一个简单的对象集合,它们都有相同的键前缀,例如照片/动物/猫。png和照片/动物/狗。png都有一个公共的前缀photos/animals/这就是为什么它们看起来在同一个文件夹中。

You will need to copy (or move) each of the objects to its new key, for example move photos/animals/cat.png to photos/pets/cat.png and move photos/animals/dog.png to photos/pets/dog.png.

您需要将每个对象复制(或移动)到它的新键,例如移动照片/动物/猫。png图片/宠物猫。png和移动照片/动物/狗。png图片/宠物/ dog.png。

That said, Google Cloud provides a way to do this from the command line using gsutil mv.

也就是说,谷歌Cloud提供了一种使用gsutil mv从命令行执行此操作的方法。