All Products
Search
Document Center

Cloud Storage Gateway:How do I actively trigger reverse synchronization on a specific directory?

Last Updated:Mar 25, 2025

This topic describes how to actively trigger reverse synchronization on a specific directory.

Prerequisites

A share is created. For more information, see Create a share.

Procedure

You can call the TriggerGatewayRemoteSyncRequest operation to actively trigger reverse synchronization on a specific directory.

Note

Cloud Storage Gateway (CSG) SDK for Python is compatible with Python 2 and Python 3. In the following example, Python 3 is used.

  1. Install CSG SDK for Python.

    pip install aliyun-python-sdk-core
    pip install aliyun-python-sdk-sgw
  2. The following sample code provides an example on how to actively trigger reverse synchronization on a specific directory. You can modify the sample code based on your business requirements.

    #!/usr/bin/env python3
    
    from aliyunsdkcore.client import AcsClient
    from aliyunsdksgw.request.v20180511 import TriggerGatewayRemoteSyncRequest
    
    def trigger_gateway_remote_sync():
        # The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
        # The AccessKey ID and AccessKey secret of a RAM user are used to access Alibaba Cloud Elastic Compute Service (ECS). Keep the AccessKey ID and AccessKey secret secure. 
        # Replace <AccessKey ID> and <AccessKey Secret> with your actual information. 
        # Replace <RegionId> with the ID of the region in which the gateway instance is located. 
        client = AcsClient('<AccessKey ID>', '<AccessKey Secret>', '<RegionId>')
       # Call the TriggerGatewayRemoteSyncRequest operation to actively trigger reverse synchronization. 
        request =TriggerGatewayRemoteSyncRequest.TriggerGatewayRemoteSyncRequest()
        request.set_GatewayId("<Gateway Id>")
        request.set_IndexId("<Index Id>")
        request.set_Path("<Path>") 
        result = client.do_action_with_exception(request)
        print(result)
    
    if __name__ == "__main__":
        trigger_gateway_remote_sync()
                            

    Parameters

    Note
    • Gateway Id: the ID of the gateway associated with the bucket that contains the directory to reversely synchronize.

    • Index Id: the ID of the share for which you want to actively trigger reverse synchronization. If the share uses the Network File System (NFS) protocol and is named test1, replace Index Id with NFStest1. If the share uses the Server Message Block (SMB) protocol and is named test2, replace Index Id with SMBtest2.

    • Path: the directory in the mount target for which you want to actively trigger reverse synchronization.

      • If you did not specify a subdirectory for the associated Object Storage Service (OSS) bucket when you created the share, the Path parameter specifies a subdirectory of the bucket.

        For example, if you want to actively trigger reverse synchronization on oss://<bucket name>/dir, set Path to /dir.

      • If you specified a subdirectory for the associated OSS bucket when you created the share, the Path parameter specifies a directory relative to the subdirectory.

        For example, if you want to actively trigger reverse synchronization onoss://<bucket name>/<prefix>/dir, where <prefix> is the specified bucket subdirectory, set Path to /dir.

      • If you want to actively trigger reverse synchronization on the root directory of the bucket, leave Path empty or set Path to /.

    Sample response:

    <?xml version='1.0' encoding='UTF-8'?>
    <TriggerGatewayRemoteSyncResponse>
        <TaskId>t-000cbzf2v72pcall****</TaskId>
        <Message>successful</Message>
        <RequestId>72191BD9-10AF-43BA-B1B5-4677F992DA3E</RequestId>
        <Code>200</Code>
        <Success>true</Success>
    </TriggerGatewayRemoteSyncResponse>
  3. Actively triggering reverse synchronization is asynchronous and time-consuming. As a result, operation polling is performed to detect whether the synchronization is complete. The following sample code provides an example of operation polling. You can modify the sample code based on your business requirements.

    from aliyunsdksgw.request.v20180511 import DescribeTasksRequest
    
    def describe_tasks():
        client = AcsClient('<AccessKey ID>', '<AccessKey Secret>', '<RegionId>')
        request = DescribeTasksRequest.DescribeTasksRequest()
        request.set_TaskId("<Task Id>")         # You can obtain the task ID from the XML data returned in the previous step (t-000cbzf2v72pcall**** in this example). 
        request.set_TargetId("<Gateway Id>")    # Gateway Id is the ID of the gateway the ID of the gateway associated with the bucket that contains the directory to reversely synchronize. 
        result = client.do_action_with_exception(request)
        print(result)
    if __name__ == "__main__":
        describe_tasks()
                            

    The following sample code provides an example response. You can obtain the value of the Progress parameter by parsing XML data. If the value of the Progress parameter is 100, the reverse synchronization task is complete. If the value of the Progress parameter is -1, the reverse synchronization task failed. If the value of the Progress parameter is between 0 and 100, the task is in progress.

    <?xml version='1.0' encoding='UTF-8'?>
    <DescribeTasksResponse>
        <TotalCount>1</TotalCount>
        <Tasks>
            <SimpleTask>
                <Progress>100</Progress>
                <TaskId>t-000cbzf2v72pcall****</TaskId>
                <CreatedTime>1622194647</CreatedTime>
                <StateCode>task.state.completed</StateCode>
                <UpdatedTime>1622194656</UpdatedTime>
                <StageCode>completed</StageCode>
                <Name>task.name.operate_gateway.trigger_remote_sync</Name>
            </SimpleTask>
        </Tasks>
        <RequestId>0CE0F47E-1664-468E-AE95-8FC86A9C8BB3</RequestId>
        <Message>successful</Message>
        <PageSize>10</PageSize>
        <PageNumber>1</PageNumber>
        <Code>200</Code>
        <Success>true</Success>
    </DescribeTasksResponse>