# Renaming Objects

<!-- -->

Tigris renames an object without a rewrite of the data. Tigris keeps object metadata in a metadata service, so a rename updates the metadata in place. The rename is fast, and it costs one request.

You can rename an object in three ways:

* With the S3 `RenameObject` API. This is the standard API, and the AWS CLI and the AWS SDKs support it.
* With the `X-Tigris-Rename: true` header on a `CopyObject` request. This is a Tigris-specific form. Use it with an SDK that does not have `RenameObject`.
* In Tigris Dashboard.

## Renaming objects with the S3 RenameObject API[​](#renaming-objects-with-the-s3-renameobject-api "Direct link to Renaming objects with the S3 RenameObject API")

`RenameObject` is `PUT /bucket/key?renameObject` with the source object in the `x-amz-rename-source` header. Tigris serves it for every bucket. On AWS, the API is limited to directory buckets.

The request renames `source-key` to `new-key` in the same bucket:

* AWS CLI
* Go
* JavaScript
* Python

```
aws s3api rename-object \

  --endpoint-url https://t3.storage.dev \

  --bucket my-bucket \

  --key new-key \

  --rename-source source-key
```

```
_, err := client.RenameObject(ctx, &s3.RenameObjectInput{

	Bucket:       aws.String("my-bucket"),

	Key:          aws.String("new-key"),

	RenameSource: aws.String("source-key"),

})

if err != nil {

	log.Fatalf("unable to rename object: %v", err)

}
```

```
import { RenameObjectCommand, S3Client } from "@aws-sdk/client-s3";



const S3 = new S3Client({

  region: "auto",

  endpoint: "https://t3.storage.dev",

});



await S3.send(

  new RenameObjectCommand({

    Bucket: "my-bucket",

    Key: "new-key",

    RenameSource: "source-key",

  }),

);
```

```
import boto3



s3 = boto3.client("s3", endpoint_url="https://t3.storage.dev")



s3.rename_object(

    Bucket="my-bucket",

    Key="new-key",

    RenameSource="source-key",

)
```

A successful rename returns `200 OK` with an empty body. After the rename, the source key does not exist, and the new key has the same content, ETag, and metadata.

### Rules[​](#rules "Direct link to Rules")

* The source and the destination must be in the same bucket. The `x-amz-rename-source` value is the source key, URL-encoded, and nothing else.
* The source cannot name a version.
* A rename to a key that exists replaces that key, as `CopyObject` does. To refuse the replacement, send `If-None-Match: *`. See [Conditional rename](#conditional-rename).
* The AWS CLI and the AWS SDKs send an `x-amz-client-token` header with each request. Tigris accepts the header and does not use it.

note

Do not prefix the rename source with a slash or the bucket name. As on AWS, the whole decoded value is read as the key, so `/my-bucket/source-key` names a different object and the request fails with `404 NoSuchKey`. A `?versionId=` suffix is read as part of the key in the same way.

note

Tigris does not de-duplicate a retry of a rename. A retry after a successful rename finds no source and returns `404 NoSuchKey`.

### Conditional rename[​](#conditional-rename "Direct link to Conditional rename")

`RenameObject` accepts conditions on the destination and on the source. Tigris checks all of them in the rename transaction, so the objects the conditions describe are the objects the rename moves and replaces.

| Header                                    | Applies to  | Behavior                                                                   |
| ----------------------------------------- | ----------- | -------------------------------------------------------------------------- |
| `If-Match`                                | destination | The rename proceeds only if the destination ETag matches.                  |
| `If-None-Match`                           | destination | Use `*` to rename only if the destination does not exist.                  |
| `If-Modified-Since`                       | destination | The rename proceeds only if the destination changed after the date.        |
| `If-Unmodified-Since`                     | destination | The rename proceeds only if the destination did not change after the date. |
| `x-amz-rename-source-if-match`            | source      | The rename proceeds only if the source ETag matches.                       |
| `x-amz-rename-source-if-none-match`       | source      | The rename proceeds only if the source ETag does not match.                |
| `x-amz-rename-source-if-modified-since`   | source      | The rename proceeds only if the source changed after the date.             |
| `x-amz-rename-source-if-unmodified-since` | source      | The rename proceeds only if the source did not change after the date.      |

A failed condition returns `412 Precondition Failed`, and nothing moves. An `If-Match` on a destination that does not exist returns `404 NoSuchKey`, as a conditional `PutObject` does.

### Permissions[​](#permissions "Direct link to Permissions")

A rename needs the same permissions as a copy plus a delete:

* `s3:PutObject` on the destination key. AWS defines no `s3:RenameObject` action.
* `s3:GetObject` and `s3:DeleteObject` on the source key.

A bucket role of `Editor` or higher grants all three. A key with `ReadOnly` on the source bucket cannot rename objects out of it.

## Renaming objects with the X-Tigris-Rename header[​](#renaming-objects-with-the-x-tigris-rename-header "Direct link to Renaming objects with the X-Tigris-Rename header")

Before `RenameObject` existed, Tigris used a `CopyObject` request with the `X-Tigris-Rename: true` header. This form still works, and it returns a `CopyObjectResult` body. Use it with an SDK that does not have `RenameObject`.

```
X-Tigris-Rename: true
```

* Go
* JavaScript
* Python

```
func WithRename() func(*s3.Options) {

	return func(options *s3.Options) {

		options.APIOptions = append(options.APIOptions, http.AddHeaderValue("X-Tigris-Rename", "true"))

	}

}



// rename the object in the bucket

_, err = client.CopyObject(ctx, &s3.CopyObjectInput{

   Bucket:     aws.String(bucketName),

   CopySource: aws.String(bucketName + "/" + keyName),

   Key:        aws.String(targetName),

}, WithRename())

if err != nil {

   log.Fatalf("Unable to rename object. Here's why: %v", err)

}
```

Add the header to the S3 client middleware stack for the rename operation:

```
export const renameObject = async (S3, bucket, oldKey, newKey) => {

  S3.middlewareStack.add(

    (next) => async (args) => {

      // eslint-disable-next-line no-param-reassign

      args.request.headers["X-Tigris-Rename"] = "true";

      return next(args);

    },

    {

      step: "build",

      name: "renameObject",

      tags: ["METADATA", "RENAME"],

    },

  );



  const copyCommand = new CopyObjectCommand({

    Bucket: bucket,

    CopySource: `${bucket}/${oldKey}`,

    Key: newKey,

  });



  await S3.send(copyCommand);



  S3.middlewareStack.remove("renameObject");

};



const S3 = new S3Client({

  region: "auto",

  s3ForcePathStyle: false,

  endpoint: "https://t3.storage.dev",

});



console.log("Rename object");

await renameObject(S3, bucket, object, newObject);
```

Use the [`tigris-boto3-ext`](https://github.com/tigrisdata/tigris-boto3-ext) package.

```
pip install tigris-boto3-ext
```

Use the `rename_object` helper for a single rename:

```
import boto3

from tigris_boto3_ext import rename_object



s3 = boto3.client(

    "s3",

    endpoint_url="https://t3.storage.dev",

    aws_access_key_id="your-access-key",

    aws_secret_access_key="your-secret-key",

)



rename_object(s3, "my-bucket", "old-name.txt", "new-name.txt")
```

Use the `TigrisRename` context manager when you want to issue several renames on the same client:

```
from tigris_boto3_ext import TigrisRename



pairs = [

    ("a.txt", "renamed-a.txt"),

    ("b.txt", "renamed-b.txt"),

    ("c.txt", "renamed-c.txt"),

]



with TigrisRename(s3):

    for src, dst in pairs:

        s3.copy_object(

            Bucket="my-bucket",

            CopySource=f"my-bucket/{src}",

            Key=dst,

        )
```

The permissions are the same as for `RenameObject`: `s3:PutObject` on the destination, and `s3:GetObject` and `s3:DeleteObject` on the source.

## Renaming objects in the Dashboard[​](#renaming-objects-in-the-dashboard "Direct link to Renaming objects in the Dashboard")

To rename an object in Tigris Dashboard:

1. Open the [Tigris Dashboard](https://console.storage.dev/) and log in.
2. In the side navigation, click "Buckets". Select the bucket, and find the object.
3. In the action menu next to the object, click "Rename".

![Tigris File Browser](/docs/assets/images/rename-obj-step-1-2542ba7db764a01ec368d7df623a1ec4.png)

4. Type the new name in the input field. ![Tigris Rename Object Popup](/docs/assets/images/rename-obj-step-2-2d418291f75e562890aa81fa80521c79.png)

5. Click "Save".

6. Make sure that the list shows the new name.

![Tigris Rename Object Success modal ](/docs/assets/images/rename-obj-step-3-52186663c3389c3a8fdd32dbc8089302.png)
