0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[MR Dev Tips #5] Azure Blob Storage へファイルをアップロードする

Last updated at Posted at 2023-08-01

はじめに

本日は Unity を使用した HoloLens 2 アプリケーションから Azure Blob Storage へファイルをアップロードする方法について、ご紹介したいと思います。

検証環境

Unity 2020.3.48f1

Azure Blob Storage へファイルをアップロードする

Unity プロジェクト (C#) から Azure Blob Storage へファイルをアップロードするには、Microsoft が提供する Azure Blob Storage SDK for .NET を使用します。この SDK は NuGet のパッケージとして公開されています。今回は Unity プロジェクトで NuGet パッケージを管理するために、NuGetForUnity を使用します。NuGetForUnity は Unity Editor で使用できる NuGet パッケージ管理システムです。

以下、GitHub の Release ページから NuGetForUnity.3.1.3.unitypackage をダウンロードします。

Assets > Import Package > Custom Package... を選択して、ダウンロードしたファイルをインポートします。

image.png

すべてのファイルにチェックが入っていることを確認して Import ボタンを押下します。NuGet フォルダは Assets 配下に追加されます。

image.png

インポートが完了したら、グローバルメニューに NuGet が追加されるのでクリックします。

image.png

image.png

Azure Blob Storage を検索して、インストールボタンをクリックします。

image.png

インストールが完了すると、Assets > Packages 配下に Azure Blob Storage のフォルダが追加されます。

image.png

これで Unity 上で Azure Blob Storage SDK の使用する準備ができました。

以下のスクリプトに、Azure Blob Storage への接続文字列、コンテナ名、ファイル名を修正して、空の GameObject にアタッチして、Unity Editor 上で 再生 (Play) ボタンを押すと、対象のコンテナにファイルをアップロードすることができます。

AzureBlobStorageUploader.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using Azure.Storage.Blobs;

/// <summary>
/// ローカルファイルを Azure Blob Storage へアップロードするためのサンプル
/// </summary>
public class AzureBlobStorageUploader : MonoBehaviour
{
    // Azure Blob Storage Account の接続文字列
    private string connectionString = <ConnectionString>;

    // データをアップロードする Azure Blob Storage のコンテナ名
    private string containerName = <containerName>;

    // アップロードするファイルのパス
    private string filePath = Path.Combine(Application.streamingAssetsPath, <fileName>);

    void Start()
    {
        // BlobContainerClient の取得
        BlobContainerClient container = new BlobContainerClient(connectionString, containerName);
        // BlobClient の取得
        BlobClient blob = container.GetBlobClient(<blobName>);
        // BlobClient を通じて、ローカルファイルを Azure Blob Storage へアップロード
        blob.Upload(filePath);
    }
}

参考情報
NuGet とは? ー Microsoft がサポートする .NET (.NET Core を含む) のコード共有メカニズムであり、.NET のパッケージマネージャーです。.NET 用のパッケージを作成、公開、使用する方法が定義されています。NuGet パッケージは拡張子が .nupkg の1つの ZIP ファイルであり、コンパイル済みのコード (.dll)、そのコードに関連する他のファイル、パッケージバージョン番号などの情報が記述されているマニフェストを含んでいます。

NuGet は Visual Studio に統合されています。ただし、Unity ではプロジェクトファイルを Unity で開くと、その Visual Studio プロジェクトファイルが再生成され、必要な構成が元に戻されてしまうため、NuGet パッケージを追加するには特別なプロセスが必要になります。

(NuGet の概要 より抜粋、加筆)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?