D'autres exemples de AWS SDK sont disponibles dans le référentiel AWS Doc SDK Examples GitHub .
Les traductions sont fournies par des outils de traduction automatique. En cas de conflit entre le contenu d'une traduction et celui de la version originale en anglais, la version anglaise prévaudra.
Utilisation CreateResource avec un AWS SDK ou une CLI
Les exemples de code suivants illustrent comment utiliser CreateResource.
Les exemples d’actions sont des extraits de code de programmes de plus grande envergure et doivent être exécutés en contexte. Vous pouvez voir cette action en contexte dans l’exemple de code suivant :
- CLI
-
- AWS CLI
-
Pour créer une ressource dans une API
Commande :
aws apigateway create-resource --rest-api-id 1234123412 --parent-id a1b2c3 --path-part 'new-resource'
- Python
-
- Kit SDK for Python (Boto3)
-
class ApiGatewayToService:
"""
Encapsulates Amazon API Gateway functions that are used to create a REST API that
integrates with another AWS service.
"""
def __init__(self, apig_client):
"""
:param apig_client: A Boto3 API Gateway client.
"""
self.apig_client = apig_client
self.api_id = None
self.root_id = None
self.stage = None
def add_rest_resource(self, parent_id, resource_path):
"""
Adds a resource to a REST API.
:param parent_id: The ID of the parent resource.
:param resource_path: The path of the new resource, relative to the parent.
:return: The ID of the new resource.
"""
try:
result = self.apig_client.create_resource(
restApiId=self.api_id, parentId=parent_id, pathPart=resource_path
)
resource_id = result["id"]
logger.info("Created resource %s.", resource_path)
except ClientError:
logger.exception("Couldn't create resource %s.", resource_path)
raise
else:
return resource_id
- SAP ABAP
-
- Kit SDK pour SAP ABAP
-
TRY.
oo_result = lo_agw->createresource(
iv_restapiid = iv_rest_api_id
iv_parentid = iv_parent_id
iv_pathpart = iv_resource_path ).
DATA(lv_resource_id) = oo_result->get_id( ).
MESSAGE 'Resource created with ID: ' && lv_resource_id TYPE 'I'.
CATCH /aws1/cx_agwbadrequestex INTO DATA(lo_bad_request).
MESSAGE lo_bad_request->get_text( ) TYPE 'I'.
RAISE EXCEPTION lo_bad_request.
CATCH /aws1/cx_agwnotfoundexception INTO DATA(lo_not_found).
MESSAGE lo_not_found->get_text( ) TYPE 'I'.
RAISE EXCEPTION lo_not_found.
CATCH /aws1/cx_agwtoomanyrequestsex INTO DATA(lo_too_many).
MESSAGE lo_too_many->get_text( ) TYPE 'I'.
RAISE EXCEPTION lo_too_many.
ENDTRY.