使用 GetLatestConfiguration API 操作读取自由格式配置文件 - Amazon AppConfig
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

使用 GetLatestConfiguration API 操作读取自由格式配置文件

以下每个示例都包含有关代码所执行操作的注释。本节中的示例调用以下 API:

Java
/* The example below uses two Amazon AppConfig Data APIs: StartConfigurationSession and GetLatestConfiguration. For more information about these APIs, see Amazon AppConfig Data. This class is meant to be used as a singleton to retrieve the latest configuration data from Amazon AppConfig. This class maintains a cache of the latest configuration data in addition to the configuration token to be passed to the next GetLatestConfiguration API call. */ class AppConfigApiRetriever { /* Amazon AppConfig Data SDK client used to interact with the Amazon AppConfig Data service. */ private AppConfigDataClient appConfigData; /* The configuration token to be passed to the next GetLatestConfiguration API call. */ private String configurationToken; /* The cached configuration data to be returned when there is no new configuration data available. */ private SdkBytes configuration; public AppConfigApiRetriever() { this.appConfigData = AppConfigDataClient.create(); } /* Returns the latest configuration data stored in Amazon AppConfig. */ public SdkBytes getConfig() { /* If there is no configuration token yet, get one by starting a new session with the StartConfigurationSession API. Note that this API does not return configuration data. Rather, it returns an initial configuration token that is subsequently passed to the GetLatestConfiguration API. */ if (this.configurationToken == null) { StartConfigurationSessionResponse session = appConfigData.startConfigurationSession(req -> req .applicationIdentifier("MyDemoApp") .configurationProfileIdentifier("MyConfig") .environmentIdentifier("Beta")); this.configurationToken = session.initialConfigurationToken(); } /* Retrieve the configuration from the GetLatestConfiguration API, providing the current configuration token. If this caller does not yet have the latest configuration (e.g. this is the first call to GetLatestConfiguration or new configuration data has been deployed since the first call), the latest configuration data will be returned. Otherwise, the GetLatestConfiguration API will not return any data since the caller already has the latest. */ GetLatestConfigurationResponse response = appConfigData.getLatestConfiguration( GetLatestConfigurationRequest.builder().configurationToken(this.configurationToken).build()); /* Save the returned configuration token so that it can be passed to the next GetLatestConfiguration API call. Warning: Not persisting this token for use in the next GetLatestConfiguration API call may result in higher than expected usage costs. */ this.configurationToken = response.nextPollConfigurationToken(); /* If the GetLatestConfiguration API returned configuration data, update the cached configuration with the returned data. Otherwise, assume the configuration has not changed, and return the cached configuration. */ SdkBytes configFromApi = response.configuration(); if (configFromApi.asByteArray().length != 0) { this.configuration = configFromApi; System.out.println("Configuration contents have changed since the last GetLatestConfiguration call, new contents = " + this.configuration.asUtf8String()); } else { System.out.println("GetLatestConfiguration returned an empty response because we already have the latest configuration"); } return this.configuration; } }
Python
# The example below uses two Amazon AppConfig Data APIs: StartConfigurationSession and GetLatestConfiguration. # For more information about these APIs, see Amazon AppConfig Data. # # This class is meant to be used as a singleton to retrieve the latest configuration data from Amazon AppConfig. # This class maintains a cache of the latest configuration data in addition to the configuration token to be # passed to the next GetLatestConfiguration API call. class AppConfigApiRetriever: def __init__(self): # Amazon AppConfig Data SDK client used to interact with the Amazon AppConfig Data service. self.appconfigdata = boto3.client('appconfigdata') # The configuration token to be passed to the next GetLatestConfiguration API call. self.configuration_token = None # The cached configuration data to be returned when there is no new configuration data available. self.configuration = None # Returns the latest configuration data stored in Amazon AppConfig. def get_config(self): # If there is no configuration token yet, get one by starting a new session with the StartConfigurationSession API. # Note that this API does not return configuration data. Rather, it returns an initial configuration token that is # subsequently passed to the GetLatestConfiguration API. if not self.configuration_token: session = self.appconfigdata.start_configuration_session( ApplicationIdentifier='MyDemoApp', ConfigurationProfileIdentifier='MyConfig', EnvironmentIdentifier='Beta' ) self.configuration_token = session['InitialConfigurationToken'] # Retrieve the configuration from the GetLatestConfiguration API, providing the current configuration token. # If this caller does not yet have the latest configuration (e.g. this is the first call to GetLatestConfiguration # or new configuration data has been deployed since the first call), the latest configuration data will be returned. # Otherwise, the GetLatestConfiguration API will not return any data since the caller already has the latest. response = self.appconfigdata.get_latest_configuration(ConfigurationToken=self.configuration_token) # Save the returned configuration token so that it can be passed to the next GetLatestConfiguration API call. # Warning: Not persisting this token for use in the next GetLatestConfiguration API call may result in higher # than expected usage costs. self.configuration_token = response['NextPollConfigurationToken'] # If the GetLatestConfiguration API returned configuration data, update the cached configuration with the returned data. # Otherwise, assume the configuration has not changed, and return the cached configuration. config_from_api = response['Configuration'].read() if config_from_api: self.configuration = config_from_api print('Configuration contents have changed since the last GetLatestConfiguration call, new contents = ' + str(self.configuration)) else: print('GetLatestConfiguration returned an empty response because we already have the latest configuration') return self.configuration
JavaScript
/* The example below uses two Amazon AppConfig Data APIs: StartConfigurationSession and GetLatestConfiguration. For more information about these APIs, see Amazon AppConfig Data. This class is meant to be used as a singleton to retrieve the latest configuration data from Amazon AppConfig. This class maintains a cache of the latest configuration data in addition to the configuration token to be passed to the next GetLatestConfiguration API call. */ class AppConfigApiRetriever { constructor() { /* Amazon AppConfig Data SDK client used to interact with the Amazon AppConfig Data service. */ this.appconfigdata = new AppConfigDataClient(); /* The configuration token to be passed to the next GetLatestConfiguration API call. */ this.configurationToken = null; /* The cached configuration data to be returned when there is no new configuration data available. */ this.configuration = null; } /* Returns the latest configuration data stored in Amazon AppConfig. */ async getConfig() { /* If there is no configuration token yet, get one by starting a new session with the StartConfigurationSession API. Note that this API does not return configuration data. Rather, it returns an initial configuration token that is subsequently passed to the GetLatestConfiguration API. */ if (!this.configurationToken) { const session = await this.appconfigdata.send( new StartConfigurationSessionCommand({ ApplicationIdentifier: "MyDemoApp", ConfigurationProfileIdentifier: "MyConfig", EnvironmentIdentifier: "Beta" }) ); this.configurationToken = session.InitialConfigurationToken; } /* Retrieve the configuration from the GetLatestConfiguration API, providing the current configuration token. If this caller does not yet have the latest configuration (e.g. this is the first call to GetLatestConfiguration or new configuration data has been deployed since the first call), the latest configuration data will be returned. Otherwise, the GetLatestConfiguration API will not return any data since the caller already has the latest. */ const response = await this.appconfigdata.send( new GetLatestConfigurationCommand({ ConfigurationToken: this.configurationToken }) ); /* Save the returned configuration token so that it can be passed to the next GetLatestConfiguration API call. Warning: Not persisting this token for use in the next GetLatestConfiguration API call may result in higher than expected usage costs. */ this.configurationToken = response.NextPollConfigurationToken; /* If the GetLatestConfiguration API returned configuration data, update the cached configuration with the returned data. Otherwise, assume the configuration has not changed, and return the cached configuration. */ const configFromApi = response.Configuration.transformToString(); if (configFromApi) { this.configuration = configFromApi; console.log("Configuration contents have changed since the last GetLatestConfiguration call, new contents = " + this.configuration); } else { console.log("GetLatestConfiguration returned an empty response because we already have the latest configuration"); } return this.configuration; } }