Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move/rename folder in Google Cloud Storage using nodejs gcloud api

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

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

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

like image 459
tomermes Avatar asked Dec 10 '16 11:12

tomermes


People also ask

How do I move a folder in GCP?

Click on the options menu (the vertical ellipsis) in the row and click Move. Click Browse to select the folder to which you want to move the project. Click Move.

Can we rename GCS bucket?

However, you can effectively move or rename your bucket: If there is no data in your old bucket, delete the bucket and create another bucket with a new name, in a new location, or in a new project.

How do I change my bucket name in GCP?

After creating a bucket, the user can always change its default storage class to any class supported in that bucket's location. However, the user needs to delete and re-create the bucket to change the bucket name and location. To create a bucket in GCP, the user must have the storage.


2 Answers

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)
like image 163
Stephen Avatar answered Sep 25 '22 10:09

Stephen


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.

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.

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

like image 34
jarmod Avatar answered Sep 22 '22 10:09

jarmod