Ada lebih banyak contoh AWS SDK yang tersedia di repo Contoh SDK AWS Doc. GitHub
Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.
Gunakan CreateResource
dengan AWS SDK atau CLI
Contoh kode berikut menunjukkan cara menggunakanCreateResource
.
Contoh tindakan adalah kutipan kode dari program yang lebih besar dan harus dijalankan dalam konteks. Anda dapat melihat tindakan ini dalam konteks dalam contoh kode berikut:
- CLI
-
- AWS CLI
-
Untuk membuat sumber daya di API
Perintah:
aws apigateway create-resource --rest-api-id 1234123412
--parent-id a1b2c3
--path-part 'new-resource
'
- Python
-
- SDK untuk Python (Boto3)
-
Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.
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