Skip to content

rename_object async

rename_object(
    bucket: BucketName,
    key: ObjectKey,
    rename_source: RenameSource,
    *,
    config_overrides: Optional[AsyncS3ClientConfig] = None,
    destination_if_match: Optional[IfMatch] = None,
    destination_if_none_match: Optional[IfNoneMatch] = None,
    destination_if_modified_since: Optional[IfModifiedSince] = None,
    destination_if_unmodified_since: Optional[IfUnmodifiedSince] = None,
    source_if_match: Optional[RenameSourceIfMatch] = None,
    source_if_none_match: Optional[RenameSourceIfNoneMatch] = None,
    source_if_modified_since: Optional[RenameSourceIfModifiedSince] = None,
    source_if_unmodified_since: Optional[RenameSourceIfUnmodifiedSince] = None,
    client_token: Optional[ClientToken] = None,
) -> RenameObjectOutput

Renames an existing object in a directory bucket that uses the S3 Express One Zone storage class. You can use RenameObject by specifying an existing object’s name as the source and the new name of the object as the destination within the same directory bucket.

RenameObject is only supported for objects stored in the S3 Express One Zone storage class.

To prevent overwriting an object, you can use the If-None-Match conditional header.

  • If-None-Match - Renames the object only if an object with the specified name does not already exist in the directory bucket. If you don't want to overwrite an existing object, you can add the If-None-Match conditional header with the value ‘*’ in the RenameObject request. Amazon S3 then returns a 412 Precondition Failed error if the object with the specified name already exists. For more information, see RFC 7232.

Permissions

To grant access to the RenameObject operation on a directory bucket, we recommend that you use the CreateSession operation for session-based authorization. Specifically, you grant the s3express:CreateSession permission to the directory bucket in a bucket policy or an IAM identity-based policy. Then, you make the CreateSession API call on the directory bucket to obtain a session token. With the session token in your request header, you can make API requests to this operation. After the session token expires, you make another CreateSession API call to generate a new session token for use. The Amazon Web Services CLI and SDKs will create and manage your session including refreshing the session token automatically to avoid service interruptions when a session expires. In your bucket policy, you can specify the s3express:SessionMode condition key to control who can create a ReadWrite or ReadOnly session. A ReadWrite session is required for executing all the Zonal endpoint API operations, including RenameObject. For more information about authorization, see CreateSession . To learn more about Zonal endpoint API operations, see Authorizing Zonal endpoint API operations with CreateSession in the Amazon S3 User Guide.

HTTP Host header syntax

Directory buckets - The HTTP Host header syntax is Bucket-name.s3express-zone-id.region-code.amazonaws.com.

You must URL encode any signed header values that contain spaces. For example, if your header value is my file.txt, containing two spaces after my, you must URL encode this value to my%20%20file.txt.

Parameters:

  • bucket (BucketName) –

    The bucket name of the directory bucket containing the object.

    You must use virtual-hosted-style requests in the format Bucket-name.s3express-zone-id.region-code.amazonaws.com. Path-style requests are not supported. Directory bucket names must be unique in the chosen Availability Zone. Bucket names must follow the format bucket-base-name--zone-id--x-s3 (for example, amzn-s3-demo-bucket--usw2-az1--x-s3). For information about bucket naming restrictions, see Directory bucket naming rules in the Amazon S3 User Guide.

  • key (ObjectKey) –

    Key name of the object to rename.

  • rename_source (RenameSource) –

    Specifies the source for the rename operation. The value must be URL encoded.

  • destination_if_match (Optional[IfMatch], default: None ) –

    Renames the object only if the ETag (entity tag) value provided during the operation matches the ETag of the object in S3. The If-Match header field makes the request method conditional on ETags. If the ETag values do not match, the operation returns a 412 Precondition Failed error.

    Expects the ETag value as a string.

  • destination_if_none_match (Optional[IfNoneMatch], default: None ) –

    Renames the object only if the destination does not already exist in the specified directory bucket. If the object does exist when you send a request with If-None-Match:*, the S3 API will return a 412 Precondition Failed error, preventing an overwrite. The If-None-Match header prevents overwrites of existing data by validating that there's not an object with the same key name already in your directory bucket.

    Expects the * character (asterisk).

  • destination_if_modified_since (Optional[IfModifiedSince], default: None ) –

    Renames the object if the destination exists and if it has been modified since the specified time.

  • destination_if_unmodified_since (Optional[IfUnmodifiedSince], default: None ) –

    Renames the object if it hasn't been modified since the specified time.

  • source_if_match (Optional[RenameSourceIfMatch], default: None ) –

    Renames the object if the source exists and if its entity tag (ETag) matches the specified ETag.

  • source_if_none_match (Optional[RenameSourceIfNoneMatch], default: None ) –

    Renames the object if the source exists and if its entity tag (ETag) is different than the specified ETag. If an asterisk (*) character is provided, the operation will fail and return a 412 Precondition Failed error.

  • source_if_modified_since (Optional[RenameSourceIfModifiedSince], default: None ) –

    Renames the object if the source exists and if it has been modified since the specified time.

  • source_if_unmodified_since (Optional[RenameSourceIfUnmodifiedSince], default: None ) –

    Renames the object if the source exists and hasn't been modified since the specified time.

  • client_token (Optional[ClientToken], default: None ) –

    A unique string with a max of 64 ASCII characters in the ASCII range of 33 - 126.

    RenameObject supports idempotency using a client token. To make an idempotent API request using RenameObject, specify a client token in the request. You should not reuse the same client token for other API requests. If you retry a request that completed successfully using the same client token and the same parameters, the retry succeeds without performing any further actions. If you retry a successful request using the same client token, but one or more of the parameters are different, the retry fails and an IdempotentParameterMismatch error is returned.

Raises:

  • IdempotencyParameterMismatch –

    Parameters on this idempotent request are inconsistent with parameters used in previous request(s).

    For a list of error codes and more information on Amazon S3 errors, see Error codes.

    Idempotency ensures that an API request completes no more than one time. With an idempotent request, if the original request completes successfully, any subsequent retries complete successfully without performing any further actions.

  • UnknownServiceError –

    The service returned an error code this client does not model.

Source code in docs_src/capo_s3/_services/async_s3.py
async def rename_object(
    self,
    bucket: "capo_s3.types.bucket_name.BucketName",
    key: "capo_s3.types.object_key.ObjectKey",
    rename_source: "capo_s3.types.rename_source.RenameSource",
    *,
    config_overrides: Optional[AsyncS3ClientConfig] = None,
    destination_if_match: Optional["capo_s3.types.if_match.IfMatch"] = None,
    destination_if_none_match: Optional[
        "capo_s3.types.if_none_match.IfNoneMatch"
    ] = None,
    destination_if_modified_since: Optional[
        "capo_s3.types.if_modified_since.IfModifiedSince"
    ] = None,
    destination_if_unmodified_since: Optional[
        "capo_s3.types.if_unmodified_since.IfUnmodifiedSince"
    ] = None,
    source_if_match: Optional[
        "capo_s3.types.rename_source_if_match.RenameSourceIfMatch"
    ] = None,
    source_if_none_match: Optional[
        "capo_s3.types.rename_source_if_none_match.RenameSourceIfNoneMatch"
    ] = None,
    source_if_modified_since: Optional[
        "capo_s3.types.rename_source_if_modified_since.RenameSourceIfModifiedSince"
    ] = None,
    source_if_unmodified_since: Optional[
        "capo_s3.types.rename_source_if_unmodified_since.RenameSourceIfUnmodifiedSince"
    ] = None,
    client_token: Optional["capo_s3.types.client_token.ClientToken"] = None,
) -> "capo_s3.types.rename_object_output.RenameObjectOutput":
    """<p>Renames an existing object in a directory bucket that uses the S3 Express One Zone storage class. You can use <code>RenameObject</code> by specifying an existing object’s name as the source and the new name of the object as the destination within the same directory bucket.</p> <note> <p> <code>RenameObject</code> is only supported for objects stored in the S3 Express One Zone storage class.</p> </note> <p> To prevent overwriting an object, you can use the <code>If-None-Match</code> conditional header.</p> <ul> <li> <p> <b>If-None-Match</b> - Renames the object only if an object with the specified name does not already exist in the directory bucket. If you don't want to overwrite an existing object, you can add the <code>If-None-Match</code> conditional header with the value <code>‘*’</code> in the <code>RenameObject</code> request. Amazon S3 then returns a <code>412 Precondition Failed</code> error if the object with the specified name already exists. For more information, see <a href="https://datatracker.ietf.org/doc/rfc7232/">RFC 7232</a>.</p> </li> </ul> <dl> <dt>Permissions</dt> <dd> <p> To grant access to the <code>RenameObject</code> operation on a directory bucket, we recommend that you use the <code>CreateSession</code> operation for session-based authorization. Specifically, you grant the <code>s3express:CreateSession</code> permission to the directory bucket in a bucket policy or an IAM identity-based policy. Then, you make the <code>CreateSession</code> API call on the directory bucket to obtain a session token. With the session token in your request header, you can make API requests to this operation. After the session token expires, you make another <code>CreateSession</code> API call to generate a new session token for use. The Amazon Web Services CLI and SDKs will create and manage your session including refreshing the session token automatically to avoid service interruptions when a session expires. In your bucket policy, you can specify the <code>s3express:SessionMode</code> condition key to control who can create a <code>ReadWrite</code> or <code>ReadOnly</code> session. A <code>ReadWrite</code> session is required for executing all the Zonal endpoint API operations, including <code>RenameObject</code>. For more information about authorization, see <a href="https://docs.aws.amazon.com/AmazonS3/latest/API/API_CreateSession.html"> <code>CreateSession</code> </a>. To learn more about Zonal endpoint API operations, see <a href="https://docs.aws.amazon.com/AmazonS3/latest/userguide/s3-express-create-session.html">Authorizing Zonal endpoint API operations with CreateSession</a> in the <i>Amazon S3 User Guide</i>. </p> </dd> <dt>HTTP Host header syntax</dt> <dd> <p> <b>Directory buckets </b> - The HTTP Host header syntax is <code> <i>Bucket-name</i>.s3express-<i>zone-id</i>.<i>region-code</i>.amazonaws.com</code>.</p> </dd> </dl> <important> <p>You must URL encode any signed header values that contain spaces. For example, if your header value is <code>my file.txt</code>, containing two spaces after <code>my</code>, you must URL encode this value to <code>my%20%20file.txt</code>.</p> </important>

    Args:
        bucket: <p>The bucket name of the directory bucket containing the object.</p> <p> You must use virtual-hosted-style requests in the format <code>Bucket-name.s3express-zone-id.region-code.amazonaws.com</code>. Path-style requests are not supported. Directory bucket names must be unique in the chosen Availability Zone. Bucket names must follow the format <code>bucket-base-name--zone-id--x-s3 </code> (for example, <code>amzn-s3-demo-bucket--usw2-az1--x-s3</code>). For information about bucket naming restrictions, see <a href="https://docs.aws.amazon.com/AmazonS3/latest/userguide/directory-bucket-naming-rules.html">Directory bucket naming rules</a> in the <i>Amazon S3 User Guide</i>.</p>
        key: <p>Key name of the object to rename.</p>
        rename_source: <p>Specifies the source for the rename operation. The value must be URL encoded.</p>
        destination_if_match: <p>Renames the object only if the ETag (entity tag) value provided during the operation matches the ETag of the object in S3. The <code>If-Match</code> header field makes the request method conditional on ETags. If the ETag values do not match, the operation returns a <code>412 Precondition Failed</code> error.</p> <p>Expects the ETag value as a string.</p>
        destination_if_none_match: <p> Renames the object only if the destination does not already exist in the specified directory bucket. If the object does exist when you send a request with <code>If-None-Match:*</code>, the S3 API will return a <code>412 Precondition Failed</code> error, preventing an overwrite. The <code>If-None-Match</code> header prevents overwrites of existing data by validating that there's not an object with the same key name already in your directory bucket.</p> <p> Expects the <code>*</code> character (asterisk).</p>
        destination_if_modified_since: <p>Renames the object if the destination exists and if it has been modified since the specified time.</p>
        destination_if_unmodified_since: <p>Renames the object if it hasn't been modified since the specified time.</p>
        source_if_match: <p>Renames the object if the source exists and if its entity tag (ETag) matches the specified ETag. </p>
        source_if_none_match: <p>Renames the object if the source exists and if its entity tag (ETag) is different than the specified ETag. If an asterisk (<code>*</code>) character is provided, the operation will fail and return a <code>412 Precondition Failed</code> error. </p>
        source_if_modified_since: <p>Renames the object if the source exists and if it has been modified since the specified time.</p>
        source_if_unmodified_since: <p>Renames the object if the source exists and hasn't been modified since the specified time.</p>
        client_token: <p> A unique string with a max of 64 ASCII characters in the ASCII range of 33 - 126.</p> <note> <p> <code>RenameObject</code> supports idempotency using a client token. To make an idempotent API request using <code>RenameObject</code>, specify a client token in the request. You should not reuse the same client token for other API requests. If you retry a request that completed successfully using the same client token and the same parameters, the retry succeeds without performing any further actions. If you retry a successful request using the same client token, but one or more of the parameters are different, the retry fails and an <code>IdempotentParameterMismatch</code> error is returned. </p> </note>

    Raises:
        capo_s3.errors.idempotency_parameter_mismatch.IdempotencyParameterMismatch: <p>Parameters on this idempotent request are inconsistent with parameters used in previous request(s). </p> <p>For a list of error codes and more information on Amazon S3 errors, see <a href="https://docs.aws.amazon.com/AmazonS3/latest/API/ErrorResponses.html#ErrorCodeList">Error codes</a>.</p> <note> <p>Idempotency ensures that an API request completes no more than one time. With an idempotent request, if the original request completes successfully, any subsequent retries complete successfully without performing any further actions.</p> </note>
        capo_s3.errors.UnknownServiceError: The service returned an error code this client does not model.
    """

    async def _handler(
        req: "AsyncOperationRequest[capo_s3.types.rename_object_request.RenameObjectRequest]",
    ) -> AsyncOperationResponse[
        "capo_s3.types.rename_object_output.RenameObjectOutput"
    ]:
        import capo_s3._operations.amazon_s3.rename_object

        (
            output,
            http_response,
        ) = await capo_s3._operations.amazon_s3.rename_object.async_rename_object(
            req.options, req.input
        )
        return AsyncOperationResponse(output=output, response=http_response)

    interceptors_, options_ = self.operation_options(
        config_overrides, OperationLogging("RenameObject")
    )
    input_: capo_s3.types.rename_object_request.RenameObjectRequest = {
        "bucket": bucket,
        "key": key,
        "rename_source": rename_source,
    }
    if destination_if_match is not None:
        input_["destination_if_match"] = destination_if_match
    if destination_if_none_match is not None:
        input_["destination_if_none_match"] = destination_if_none_match
    if destination_if_modified_since is not None:
        input_["destination_if_modified_since"] = destination_if_modified_since
    if destination_if_unmodified_since is not None:
        input_["destination_if_unmodified_since"] = destination_if_unmodified_since
    if source_if_match is not None:
        input_["source_if_match"] = source_if_match
    if source_if_none_match is not None:
        input_["source_if_none_match"] = source_if_none_match
    if source_if_modified_since is not None:
        input_["source_if_modified_since"] = source_if_modified_since
    if source_if_unmodified_since is not None:
        input_["source_if_unmodified_since"] = source_if_unmodified_since
    if client_token is None:
        client_token = str(uuid.uuid4())
    input_["client_token"] = client_token

    response = await aexecute_pipeline(
        AsyncOperationRequest(input=input_, options=options_),
        handler=_handler,
        interceptors=list(interceptors_),
    )
    await response.response.aclose()
    return response.output