SDK for TypeScript and JavaScriptのクラウド・シェル・クイック・スタート

このクイック・スタートでは、Oracle Cloud Infrastructure SDK for TypeScriptおよびJavaScriptでクラウド・シェルを使用してサンプル・コードの実行を簡単に開始する方法について説明します。

このクイック・スタートでは、Oracle Cloud Infrastructure SDK for TypeScriptおよびJavaScriptでクラウド・シェルを使用してサンプル・コードの実行を簡単に開始する方法について説明します。OCI SDK for TypeScript and JavaScript SDKは、npmを介してグローバルに事前インストールされます。

JavaScriptの例

  1. コンソールにログインします。
  2. コンソール・ヘッダーで「クラウド・シェル」アイコンをクリックします。クラウド・シェルでは、クラウド・シェルの起動時に、コンソールの「リージョン」選択メニューで選択されているリージョンに対してコマンドが実行されることに注意してください。
  3. 現在のテナンシのサブスクライブ済リージョンをリストする次のコード例を使用して、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)
    })();
  4. 現在のテナンシのサブスクライブ済リージョンをリストする次のコード例を使用して、region_subscriptions_example.jsという名前のファイルを作成します:
    npm link oci-sdk
  5. 例を実行します:
    node region_subscriptions_example.js

TypeScriptの例

このTypeScriptの例では、現在のテナンシのサブスクライブ済リージョンをリストします。
  1. 次のコマンドを使用して、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
  2. 次のコードを使用して、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)
    })();
  3. 例をコンパイルします:
    # use the TypeScript compiler to compile the example
    tsc region_subscriptions_example.ts
  4. 例を実行します:
    # run the example using node
    node region_subscriptions_example.js