Copying and Moving Objects

This page shows you how to copy and move objects within and between buckets in Cloud Storage. For an overview of objects, read the Key Terms and see the Object naming requirements.

Copying an object

To copy an object in one of your Cloud Storage buckets:

Console

  1. Open the Cloud Storage browser in the Google Cloud Platform Console.
    Open the Cloud Storage browser

  2. Navigate to the object you want to copy.

  3. Click the more options button (three vertical dots) associated with the object.

  4. Click Copy.

  5. In the overlay window that appears, click Browse.

  6. Select the destination for the copied object and the name for the copied object.

  7. Click Select.

  8. Click Copy.

gsutil

Use the gsutil cp command, replacing [VALUES_IN_BRACKETS] with the appropriate values:

gsutil cp gs://[SOURCE_BUCKET_NAME]/[SOURCE_OBJECT_NAME] gs://[DESTINATION_BUCKET_NAME]/[NAME_OF_COPY]

If successful, the response looks like the following example:

Operation completed over 1 objects/58.8 KiB.

Code samples

C#

For more on installing and creating a Cloud Storage client, refer to Cloud Storage Client Libraries.

private void CopyObject(string sourceBucketName, string sourceObjectName,
    string destBucketName, string destObjectName)
{
    var storage = StorageClient.Create();
    storage.CopyObject(sourceBucketName, sourceObjectName,
        destBucketName, destObjectName);
    Console.WriteLine($"Copied {sourceBucketName}/{sourceObjectName} to "
        + $"{destBucketName}/{destObjectName}.");
}

Go

For more on installing and creating a Cloud Storage client, refer to Cloud Storage Client Libraries.

dstObject := srcObject + "-copy"
src := client.Bucket(srcBucket).Object(srcObject)
dst := client.Bucket(dstBucket).Object(dstObject)

if _, err := dst.CopierFrom(src).Run(ctx); err != nil {
	return err
}

Java

For more on installing and creating a Cloud Storage client, refer to Cloud Storage Client Libraries.

CopyWriter copyWriter = blob.copyTo(bucketName);
Blob copiedBlob = copyWriter.getResult();

Node.js

For more on installing and creating a Cloud Storage client, refer to Cloud Storage Client Libraries.

// Imports the Google Cloud client library
const Storage = require('@google-cloud/storage');

// Creates a client
const storage = new Storage();

/**
 * TODO(developer): Uncomment the following lines before running the sample.
 */
// const srcBucketName = 'Name of the source bucket, e.g. my-bucket';
// const srcFilename = 'Name of the source file, e.g. file.txt';
// const destBucketName = 'Name of the destination bucket, e.g. my-other-bucket';
// const destFilename = 'Destination name of file, e.g. file.txt';

// Copies the file to the other bucket
storage
  .bucket(srcBucketName)
  .file(srcFilename)
  .copy(storage.bucket(destBucketName).file(destFilename))
  .then(() => {
    console.log(
      `gs://${srcBucketName}/${srcFilename} copied to gs://${destBucketName}/${destFilename}.`
    );
  })
  .catch(err => {
    console.error('ERROR:', err);
  });

PHP

For more on installing and creating a Cloud Storage client, refer to Cloud Storage Client Libraries.

use Google\Cloud\Storage\StorageClient;

/**
 * Copy an object to a new name and/or bucket.
 *
 * @param string $bucketName the name of your Cloud Storage bucket.
 * @param string $objectName the name of your Cloud Storage object.
 * @param string $newBucketName the destination bucket name.
 * @param string $newObjectName the destination object name.
 * @return void
 */
function copy_object($bucketName, $objectName, $newBucketName, $newObjectName)
{
    $storage = new StorageClient();
    $bucket = $storage->bucket($bucketName);
    $object = $bucket->object($objectName);
    $object->copy($newBucketName, ['name' => $newObjectName]);
    printf('Copied gs://%s/%s to gs://%s/%s' . PHP_EOL,
        $bucketName, $objectName, $newBucketName, $newObjectName);
}

Python

For more on installing and creating a Cloud Storage client, refer to Cloud Storage Client Libraries.

def copy_blob(bucket_name, blob_name, new_bucket_name, new_blob_name):
    """Copies a blob from one bucket to another with a new name."""
    storage_client = storage.Client()
    source_bucket = storage_client.get_bucket(bucket_name)
    source_blob = source_bucket.blob(blob_name)
    destination_bucket = storage_client.get_bucket(new_bucket_name)

    new_blob = source_bucket.copy_blob(
        source_blob, destination_bucket, new_blob_name)

    print('Blob {} in bucket {} copied to blob {} in bucket {}.'.format(
        source_blob.name, source_bucket.name, new_blob.name,
        destination_bucket.name))

Ruby

For more on installing and creating a Cloud Storage client, refer to Cloud Storage Client Libraries.

# project_id         = "Your Google Cloud project ID"
# source_bucket_name = "Source bucket to copy file from"
# source_file_name   = "Source file name"
# dest_bucket_name   = "Destination bucket to copy file to"
# dest_file_name     = "Destination file name"

require "google/cloud/storage"

storage = Google::Cloud::Storage.new project: project_id
bucket  = storage.bucket source_bucket_name
file    = bucket.file source_file_name

destination_bucket = storage.bucket dest_bucket_name
destination_file   = file.copy destination_bucket.name, file.name

puts "#{file.name} in #{bucket.name} copied to " +
     "#{copied_file.name} in #{destination_bucket.name}"

REST APIs

JSON API

  1. Get an authorization access token from the OAuth 2.0 Playground. Configure the playground to use your own OAuth credentials.
  2. Use cURL to call the JSON API with a POST Object request, replacing [VALUES_IN_BRACKETS] with the appropriate values:
    curl -X POST \
        -H "Authorization: Bearer [OAUTH2_TOKEN]" \
        -H "Content-Length: 0" \
        "https://www.googleapis.com/storage/v1/b/[SOURCE_BUCKET_NAME]/o/[SOURCE_OBJECT_NAME]/rewriteTo/b/[DESTINATION_BUCKET_NAME]/o/[NAME_OF_COPY]"

XML API

  1. Get an authorization access token from the OAuth 2.0 Playground. Configure the playground to use your own OAuth credentials.
  2. Use cURL to call the XML API with a PUT Object request, replacing [VALUES_IN_BRACKETS] with the appropriate values:
    curl -X PUT \
        -H "Authorization: Bearer [OAUTH2_TOKEN]" \
        -H "x-goog-copy-source: [SOURCE_BUCKET_NAME]/[SOURCE_OBJECT_NAME]" \
        "https://storage.googleapis.com/[DESTINATION_BUCKET_NAME]/[NAME_OF_COPY]"

Moving an object

To move an object in one of your Cloud Storage buckets:

Console

  1. Open the Cloud Storage browser in the Google Cloud Platform Console.
    Open the Cloud Storage browser

  2. Navigate to the object you want to move.

  3. Click the more options button (three vertical dots) associated with the object.

  4. Click Move.

  5. In the overlay window that appears, click Browse.

  6. Select the destination for the object your are moving.

  7. Click Select.

  8. Click Move.

gsutil

Use the gsutil mv command, replacing [VALUES_IN_BRACKETS] with the appropriate values:

gsutil mv gs://[SOURCE_BUCKET_NAME]/[SOURCE_OBJECT_NAME] gs://[DESTINATION_BUCKET_NAME]/[DESTINATION_OBJECT_NAME]

If successful, the response looks like the following example:

Operation completed over 1 objects/58.8 KiB.

Code samples

C#

For more on installing and creating a Cloud Storage client, refer to Cloud Storage Client Libraries.

private void MoveObject(string bucketName, string sourceObjectName,
    string destObjectName)
{
    var storage = StorageClient.Create();
    storage.CopyObject(bucketName, sourceObjectName, bucketName,
        destObjectName);
    storage.DeleteObject(bucketName, sourceObjectName);
    Console.WriteLine($"Moved {sourceObjectName} to {destObjectName}.");
}

Go

For more on installing and creating a Cloud Storage client, refer to Cloud Storage Client Libraries.

dstName := object + "-rename"

src := client.Bucket(bucket).Object(object)
dst := client.Bucket(bucket).Object(dstName)

if _, err := dst.CopierFrom(src).Run(ctx); err != nil {
	return err
}
if err := src.Delete(ctx); err != nil {
	return err
}

Java

For more on installing and creating a Cloud Storage client, refer to Cloud Storage Client Libraries.

CopyWriter copyWriter = blob.copyTo(destBucket, destBlob);
Blob copiedBlob = copyWriter.getResult();
boolean deleted = blob.delete();

Node.js

For more on installing and creating a Cloud Storage client, refer to Cloud Storage Client Libraries.

// Imports the Google Cloud client library
const Storage = require('@google-cloud/storage');

// Creates a client
const storage = new Storage();

/**
 * TODO(developer): Uncomment the following lines before running the sample.
 */
// const bucketName = 'Name of a bucket, e.g. my-bucket';
// const srcFilename = 'File to move, e.g. file.txt';
// const destFilename = 'Destination for file, e.g. moved.txt';

// Moves the file within the bucket
storage
  .bucket(bucketName)
  .file(srcFilename)
  .move(destFilename)
  .then(() => {
    console.log(
      `gs://${bucketName}/${srcFilename} moved to gs://${bucketName}/${destFilename}.`
    );
  })
  .catch(err => {
    console.error('ERROR:', err);
  });

PHP

For more on installing and creating a Cloud Storage client, refer to Cloud Storage Client Libraries.

use Google\Cloud\Storage\StorageClient;

/**
 * Move an object to a new name and/or bucket.
 *
 * @param string $bucketName the name of your Cloud Storage bucket.
 * @param string $objectName the name of your Cloud Storage object.
 * @param string $newBucketName the destination bucket name.
 * @param string $newObjectName the destination object name.
 *
 * @return void
 */
function move_object($bucketName, $objectName, $newBucketName, $newObjectName)
{
    $storage = new StorageClient();
    $bucket = $storage->bucket($bucketName);
    $object = $bucket->object($objectName);
    $object->copy($newBucketName, ['name' => $newObjectName]);
    $object->delete();
    printf('Moved gs://%s/%s to gs://%s/%s' . PHP_EOL,
        $bucketName,
        $objectName,
        $newBucketName,
        $newObjectName);
}

Python

For more on installing and creating a Cloud Storage client, refer to Cloud Storage Client Libraries.

def rename_blob(bucket_name, blob_name, new_name):
    """Renames a blob."""
    storage_client = storage.Client()
    bucket = storage_client.get_bucket(bucket_name)
    blob = bucket.blob(blob_name)

    new_blob = bucket.rename_blob(blob, new_name)

    print('Blob {} has been renamed to {}'.format(
        blob.name, new_blob.name))

Ruby

For more on installing and creating a Cloud Storage client, refer to Cloud Storage Client Libraries.

# project_id  = "Your Google Cloud project ID"
# bucket_name = "Your Google Cloud Storage bucket name"
# file_name   = "Name of file in Google Cloud Storage to rename"
# new_name    = "File will be renamed to this new name"

require "google/cloud/storage"

storage = Google::Cloud::Storage.new project: project_id
bucket  = storage.bucket bucket_name
file    = bucket.file file_name

renamed_file = file.copy new_name

file.delete

puts "my-file.txt has been renamed to #{renamed_file.name}"

REST APIs

JSON API

  1. Get an authorization access token from the OAuth 2.0 Playground. Configure the playground to use your own OAuth credentials.
  2. Use cURL to call the JSON API with a POST Object request, replacing [VALUES_IN_BRACKETS] with the appropriate values:
    curl -X POST \
        -H "Authorization: Bearer [OAUTH2_TOKEN]" \
        -H "Content-Length: 0" \
        "https://www.googleapis.com/storage/v1/b/[SOURCE_BUCKET_NAME]/o/[SOURCE_OBJECT_NAME]/rewriteTo/b/[DESTINATION_BUCKET_NAME]/o/[DESTINATION_OBJECT_NAME]"
  3. Use cURL to call the JSON API with a DELETE Object request, replacing [VALUES_IN_BRACKETS] with the appropriate values:
    curl -X DELETE \
        -H "Authorization: Bearer [OAUTH2_TOKEN]" \
        "https://www.googleapis.com/storage/v1/b/[SOURCE_BUCKET_NAME]/o/[SOURCE_OBJECT_NAME]"

XML API

  1. Get an authorization access token from the OAuth 2.0 Playground. Configure the playground to use your own OAuth credentials.
  2. Use cURL to call the XML API with a PUT Object request, replacing [VALUES_IN_BRACKETS] with the appropriate values:
    curl -X PUT \
        -H "Authorization: Bearer [OAUTH2_TOKEN]" \
        -H "x-goog-copy-source: [SOURCE_BUCKET_NAME]/[SOURCE_OBJECT_NAME]" \
        "https://storage.googleapis.com/[DESTINATION_BUCKET_NAME]/[DESTINATION_OBJECT_NAME]"
  3. Use cURL to call the XML API with a DELETE Object request, replacing [VALUES_IN_BRACKETS] with the appropriate values:
    curl -X DELETE \
        -H "Authorization: Bearer [OAUTH2_TOKEN]" \
        "https://storage.googleapis.com/[SOURCE_BUCKET_NAME]/[SOURCE_OBJECT_NAME]"

What's next

Send feedback about...

Cloud Storage Documentation