SDK for TypeScript and JavaScriptのクラウド・シェル・クイック・スタート
このクイック・スタートでは、クラウド・シェルを使用してOracle Cloud Infrastructure SDK for TypeScript and JavaScriptでサンプル・コードの実行を簡単に開始する方法について説明します。
このクイック・スタートでは、クラウド・シェルを使用してOracle Cloud Infrastructure SDK for TypeScript and JavaScriptでサンプル・コードの実行を簡単に開始する方法について説明します。OCI SDK for TypeScript and JavaScript SDKは、npmを介してグローバルに事前インストールされます。
JavaScript例
- コンソールにログインします。
- コンソール・ヘッダーで「クラウド・シェル」アイコンをクリックします。クラウド・シェルでは、クラウド・シェルの起動時に、コンソールの「リージョン」選択メニューで選択されているリージョンに対してコマンドが実行されることに注意してください。
- 現在のテナンシのサブスクライブ済リージョンをリストする次のコード例を使用して、
region_subscriptions_example.js
という名前のファイルを作成します:const identity = require("oci-sdk/node_modules/oci-identity"); const common = require("oci-sdk/node_modules/oci-common"); const provider = new common.ConfigFileAuthenticationDetailsProvider(); const compartmentId = provider.getTenantId() || ""; let identityClient; async function getSubscriptionRegions(tenancyId) { const regions = await identityClient.listRegionSubscriptions({ tenancyId: tenancyId }); return regions.items.map(region => { return region.regionName; }); } (async () => { identityClient = await new identity.IdentityClient({ authenticationDetailsProvider: provider }); const regions = await getSubscriptionRegions(compartmentId); console.log("Currently subscribed to the following region(s): ", regions) })();
- 現在のテナンシのサブスクライブ済リージョンをリストする次のコード例を使用して、
region_subscriptions_example.js
という名前のファイルを作成します:npm link oci-sdk
- 例を実行します:
node region_subscriptions_example.js
TypeScript例
このTypeScriptの例では、現在のテナンシのサブスクライブ済リージョンをリストします。
- 次のコマンドを使用して、ts_demoという名前の空のプロジェクトを作成し、oci-sdkライブラリのグローバル・インストールをリンクします:
# create a new project folder and move into it mkdir ts_demo cd ts_demo # initialize a new javascript/typescript project npm init --y # link the global installation of oci-sdk to the current project npm link oci-sdk npm link @types/node
- 次のコードを使用して、ts_demoプロジェクト内に
region_subscriptions_example.ts
という名前のファイルを作成します:import * as identity from "oci-sdk/node_modules/oci-identity"; import common = require("oci-sdk/node_modules/oci-common"); const provider: common.ConfigFileAuthenticationDetailsProvider = new common.ConfigFileAuthenticationDetailsProvider(); const compartmentId = provider.getTenantId(); let identityClient: identity.IdentityClient; export async function getSubscriptionRegions(tenancyId: string) { const listRegionSubscriptionsRequest: identity.requests.ListRegionSubscriptionsRequest = { tenancyId: tenancyId }; const regions = await identityClient.listRegionSubscriptions(listRegionSubscriptionsRequest); return regions.items.map(region => { return region.regionName; }); } (async () => { identityClient = await new identity.IdentityClient({ authenticationDetailsProvider: provider }); const regions = await getSubscriptionRegions(compartmentId); console.log("Currently subscribed to the following region(s): ", regions) })();
- 例をコンパイルします:
# use the TypeScript compiler to compile the example tsc region_subscriptions_example.ts
- 例を実行します:
# run the example using node node region_subscriptions_example.js