すべてのプロダクト
Search
ドキュメントセンター

Cloud Storage Gateway:特定のディレクトリで逆同期をアクティブにトリガーするにはどうすればよいですか?

最終更新日:Sep 20, 2024

このトピックでは、特定のディレクトリで逆同期をアクティブにトリガーする方法について説明します。

前提条件

共有が作成されます。 詳細については、「共有の作成」をご参照ください。

手順

TriggerGatewayRemoteSyncRequest操作を呼び出して、特定のディレクトリで逆同期をアクティブにトリガーできます。

説明

Cloud Storage Gateway (CSG) SDK for Pythonは、Python 2およびPython 3と互換性があります。 次の例では、Python 3が使用されています。

  1. CSG SDK for Pythonをインストールします。

    pip install aliyun-python-sdk-core
    pip install aliyun-python-sdk-sgw
  2. 次のサンプルコードは、特定のディレクトリで逆同期をアクティブにトリガーする方法の例を示しています。 ビジネス要件に基づいてサンプルコードを変更できます。

    #!/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()
                            

    パラメーター

    説明
    • Gateway Id: 逆同期するディレクトリを含むバケットに関連付けられたゲートウェイのID。

    • インデックスId: 逆同期をアクティブにトリガーする共有のID。 共有がネットワークファイルシステム (NFS) プロトコルを使用し、test1という名前の場合は、インデックスIdをNFStest1に置き換えます。 共有がサーバーメッセージブロック (SMB) プロトコルを使用し、test2という名前の場合は、インデックスIdをSMBtest2に置き換えます。

    • パス: 逆同期をアクティブにトリガーするマウントターゲット内のディレクトリ。

      • 共有を作成したときに、関連付けられたObject Storage Service (OSS) バケットのサブディレクトリを指定しなかった場合、Pathパラメーターはバケットのサブディレクトリを指定します。

        たとえば、oss://<bucket name>/dirで逆同期をアクティブにトリガーする場合は、パスを /dirに設定します。

      • 共有を作成したときに、関連付けられたOSSバケットのサブディレクトリを指定した場合、Pathパラメーターはサブディレクトリからの相対ディレクトリを指定します。

        たとえば、oss://<bucket name>/<prefix>/dir (<prefix> は指定されたバケットサブディレクトリ) で逆同期をアクティブにトリガーする場合は、Pathを /dirに設定します。

      • バケットのルートディレクトリで逆同期をアクティブにトリガーする場合は、Pathを空のままにするか、Pathを /に設定します。

    レスポンス例:

    <?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. 逆同期を積極的にトリガすることは、非同期で時間がかかる。 その結果、同期が完了したかどうかを検出するための動作ポーリングが行われる。 次のサンプルコードは、操作ポーリングの例を示しています。 ビジネス要件に基づいてサンプルコードを変更できます。

    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()
                            

    次のサンプルコードは、レスポンスの例を示しています。 XMLデータを解析することで、Progressパラメーターの値を取得できます。 Progressパラメーターの値が100の場合、逆同期タスクは完了です。 Progressパラメーターの値が-1の場合、逆同期タスクは失敗しました。 Progressパラメーターの値が0〜100の場合、タスクは進行中です。

    <?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>