2
1

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 3 years have passed since last update.

Azure Storage (ファイル共有 / File Share)にファイルをプログラムからアップロードする方法

Last updated at Posted at 2020-09-30

Azure Storage にファイルを保存する方法を記載。
Azure Storageの種類はファイル共有(blobではない)を利用。

事前準備

Azureポータルにてstorage環境作成

  • ストレージ アカウントの作成
  • 作成したストレージ アカウントからファイル共有(File Share)を作成

Azure.Storageライブラリのインストール

[NuGet パッケージの管理]から以下をインストール

  • Azure.Storage.Common
  • Azure.Storage.Files.Shares

プログラム


using System.IO;
using Azure.Storage;
using Azure.Storage.Files.Shares;

public class SampleClass
{
    public void Upload()
    {
        ///Azureのストレージアカウントから情報を取得して設定
        string accountName = "{Azureのポータルサイトからアカウント名を取得する。}";
        string accessKey   = "{Azureのポータルサイトからアクセスキーを取得する。}";
        Uri    serverurl   = new Uri(@"{AzureのポータルサイトからURLを取得する。}") ;

        ///アップロード先(azure側)
        string azureDirectoryPath = @"{保存先(azure側)のディレクトリを指定}" ;
        string azureFileName      =  "{保存するファイル名称を指定}";

        ///アップロード対象(Local側)
        string localDirectoryPath = @"{アップロード対象(Local側)のディレクトリを指定}";
        string localFileName      =  "{アップロード対象(Local側)のファイル名を指定}";

        //SSL通信の許可設定
        //これをやっとかないと、SSL(https)通信でエラーが発生する。
        System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls12;
        

         try
         {
            //Azureへ接続準備:接続情報の設定
            StorageSharedKeyCredential credential = new StorageSharedKeyCredential(accountName, accessKey);

            //Azureへ接続
            ShareClient share = new ShareClient(serverurl ,  credential);

            ShareDirectoryClient directory = share.GetDirectoryClient(azureDirectoryPath);

            //Upload先(azure側)にフォルダがなければ作成。
            directory.CreateIfNotExists();

            //Upload先(azure側)にファイルインスタンスを作成。
            ShareFileClient file = directory.GetFileClient(azureFileName);

            //同名のファイルがあったら削除
            file.DeleteIfExists();
            
            //Upload対象のLocalファイルを開く。FileStream型で開くとバイナリ情報の取得が楽。
            FileStream stream = File.OpenRead( Path.Combine(localDirectoryPath , localFileName ) );

            //Upload先(azure側)にファイルインスタンスにバイナリ情報を注入
            file.Create(stream.Length);
            file.UploadRange(new Azure.HttpRange(0, stream.Length),stream);
            
            //ローカルファイルの開放
            stream.Dispose();
        }
        catch(Exception ex)
        {
            System.Console.WriteLine(ex.Message);
            return;
        }
    }
}

後記

  • ストレージへアクセスする際の推奨されているライブラリがWindowsAzure.StorageからAzure.Storageに変わった模様。Azure.Storageを利用したサンプルがなかったのでメモとして投稿。
  • Azure内で[ファイル共有]、[File Share]、[File サービス]と表記揺れがあった。(2020/09/30時点)同義語として解釈してよさそう。
2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?