Vultr Goクライアントを使用したVultrオブジェクトストレージの作成

前書き

このガイドでは、Vultr Goクライアントを使用してGoアプリケーションでVultrオブジェクトストレージを作成する方法について説明します。Vultr Object StorageはS3 APIと互換性があります。

前提条件

  • Go 1.12+がインストールされたWindows、Linux、またはmacOSを実行しているコンピューター。
  • 基本的なプログラミングの知識。
  • オプション:GoをサポートするIDE(Visual Studio Code、emacs、Atomなど)。

Vultr Goクライアントを使用したオブジェクトストレージの作成

Vultr囲碁クライアントはと対話するために使用されているVultrのAPI。Vultr APIを使用すると、Vultrオブジェクトストレージなど、アカウントに関連付けられたリソースを制御できます。Vultr Goクライアントの詳細については、この記事をご覧 ください

プロジェクトフォルダを作成する

現在のディレクトリにフォルダを作成します。

$ mkdir vultr_demo

新しいフォルダを入力します。

$ cd vultr_demo

モジュールを初期化する

$ go mod init vultr_demo

ライブラリをダウンロード

$ go get github.com/vultr/govultr

APIキーの環境変数を設定する

VULTR_API_KEY環境変数をAPIキーに設定します。APIキーは次の場所にあります:https : //my.vultr.com/settings/#settingsapi

警告:パスワードを保護するのと同じように、APIキーを保護してください。APIキーは、請求、サーバー、ストレージを含むアカウント全体にアクセスできます。

たとえば、Ubuntu Linuxでは、この行をに追加し~/.profileます。

export VULTR_API_KEY=YOUR_API_KEY_HERE

Goプログラムを作成する

次の各コードフラグメントをという名前の新しいファイルに貼り付けますmain.go完全なファイルもダウンロードできます

パッケージをセットアップし、必要なライブラリをインポートします。

package main

import (
    "context"
    "log"
    "os"
    "time"

    "github.com/vultr/govultr"
)

main()関数を開始します。

func main() {

APIキーの環境変数を読み取ります。

var (
    // Get our Vultr API Key from an environment variable.
    VultrAPIKey = os.Getenv("VULTR_API_KEY")
)

Vultrクライアントを作成します。

// Create a Vultr client with our API Key.
vultr := govultr.NewClient(nil, VultrAPIKey)

Object Storageを作成する場所を指定します。クラスターの場所を取得し、使用できることを確認し、クラスターを選択して表示します。

// Find the clusters we can create our Object Storage in.
clusters, err := vultr.ObjectStorage.ListCluster(ctx)
if err != nil {
    log.Fatalf("Error listing clusters: %s", err)
}

// Verify there's at least one cluster.
if len(clusters) < 1 {
    log.Fatal("Could not find any clusters to create our Object Storage")
}

// Choose the first cluster, and print it.
cluster := clusters[0]
log.Printf("Chosen cluster: %+v", cluster)

選択したクラスターにオブジェクトストレージを作成します。

// Create our Object Storage in the first cluster listed with our custom label.
label := "my-object-storage"
storageID, err := vultr.ObjectStorage.Create(ctx, cluster.ObjectStoreClusterID, label)
if err != nil {
    log.Fatalf("Error creating storage: %s", err)
}

log.Printf("Created our Object Storage with the ID: %d", storageID.ID)

オブジェクトストレージがアクティブになるまで待ちます。

var storage govultr.ObjectStorage

// Query the API every five seconds to until our server is ready.
for {
    // List all of the Object Storage containers with our label and include the S3 credentials.
    list, err := vultr.ObjectStorage.List(ctx, &govultr.ObjectListOptions{
        Label:     label,
        IncludeS3: true,
    })
    if err != nil {
        log.Fatalf("Error listing storage with label \"%s\": %s", label, err)
    }

    // Make sure we found one (and only one) Object Storage container.
    if len(list) != 1 {
        log.Fatalf("%d object storage containers exist with the label \"%s\"; we need 1", len(list), label)
    }

    storage = list[0]

    // If the server is active, break out of this loop.
    if storage.Status == "active" {
        break
    }

    // Wait for five seconds before querying the API again.
    log.Printf("The Object Storage's status is currently \"%s\", waiting for another five seconds to check if it's \"active\".", storage.Status)
    time.Sleep(time.Second * 5)
}

エンドポイント名と接続資格情報を表示します。

// Print the information of our new Object Storage.
log.Print("Successfully created and listed our Object Storage!")
log.Printf("Object Storage: %+v", storage)

// We also have S3 credentials here now, so we could open an S3 compatible client.
log.Printf("S3 credentials: %s - %s - %s", storage.S3Hostname, storage.S3AccessKey, storage.S3SecretKey)

メイン関数を終了します。

// end main()
}

完成したmain.goファイルを保存して実行します。

go run main.go

出力例

2020/03/03 13:05:48 Chosen cluster: {ObjectStoreClusterID:2 RegionID:1 Location:New Jersey Hostname:ewr1.vultrobjects.com Deploy:yes}
2020/03/03 13:05:48 Created our Object Storage with the ID: xxxxxxxx
2020/03/03 13:05:49 The Object Storage's status is currently pending, waiting for another five seconds to check if it's active.
2020/03/03 13:06:06 Object Storage: {ID:34214512 DateCreated:2020-03-03 13:05:47 ObjectStoreClusterID:2 RegionID:1 Location:New Jersey Label:my-object-storage Status:active S3Keys:{S3Hostname:ewr1.vultrobjects.com S3AccessKey:[REDACTED ACCESS KEY] S3SecretKey:[REDACTED SECRET KEY]}}
2020/03/03 13:06:06 S3 credentials: ewr1.vultrobjects.com - [REDACTED ACCESS KEY] - [REDACTED SECRET KEY]

この例では、エンドポイント名はewr1.vultrobjects.comであり、キーは編集されています。Goプログラムで表示されるエンドポイント名、アクセスキー、シークレットキーを使用して、S3互換クライアントで新しいオブジェクトストレージにアクセスします。



Leave a Comment

CentOS 7にApacheをインストールする方法

CentOS 7にApacheをインストールする方法

CentOS 7サーバーにApache 2.4をインストールする方法を説明します。安定したウェブサーバーを構築するための前提条件と手順を解説します。

FreeBSD 11.1にBlacklistdをインストールする方法

FreeBSD 11.1にBlacklistdをインストールする方法

FreeBSD 11.1におけるBlacklistdのインストール方法について詳しく解説します。この方法を通じて、強力なセキュリティ対策を実装できます。

Windows Serverのサーバーマネージャーを使用した複数サーバーの管理

Windows Serverのサーバーマネージャーを使用した複数サーバーの管理

サーバーマネージャーを使用して、Windows Serverの管理が向上します。セキュリティリスクを軽減し、効率的な管理を実現します。

CentOS 7にSeafileサーバーをインストールする方法

CentOS 7にSeafileサーバーをインストールする方法

CentOS 7にSeafileサーバーをインストールする方法。Seafile(コミュニティバージョン)は、ownCloudに似た無料のオープンソースファイル同期および共有ソリューションです。

DebianでSnortを設定する方法

DebianでSnortを設定する方法

Snortは無料のネットワーク侵入検知システムです。最新の方法で、SnortをDebianにインストールし、設定する手順を紹介します。ネットワークのセキュリティを強化しましょう。

CentOS 7にGraylogサーバーをインストールする方法

CentOS 7にGraylogサーバーをインストールする方法

CentOS 7にGraylogサーバーをインストールし、ログ管理を行う方法を学びます。

WindowsでhMailServerを使用してメールサーバーを構築する

WindowsでhMailServerを使用してメールサーバーを構築する

WindowsサーバーでWebサイトを実行している場合、電子メールも受信できるようにするためにhMailServerを使用する方法を解説します。

Ubuntu 19.04にFiveMサーバーをインストールする方法

Ubuntu 19.04にFiveMサーバーをインストールする方法

FiveMサーバーをUbuntu 19.04にインストールするための詳細なガイド。必要条件からインストール、起動、トラブルシューティングまで、すべてのステップを含みます。

WsgiDAVを使用してDebian 10にWebDAVをデプロイする

WsgiDAVを使用してDebian 10にWebDAVをデプロイする

Debian 10にWebDAVをデプロイする方法を学び、WsgiDAVとSSL証明書で安全な接続を実現しましょう。

ヘルスケア2021における人工知能の影響

ヘルスケア2021における人工知能の影響

ヘルスケアにおけるAIは、過去数十年から大きな飛躍を遂げました。したがって、ヘルスケアにおけるAIの未来は、日々成長を続けています。