---
title: "ブロブストレージのAPI参照"
slug: "api-reference-for-blob-storage"
abstract: "この参考文献を使って、Blobストレージの要件に合わせてソリューションをカスタマイズしてください。"
---
# ブロブストレージのAPI参照

Sitecore Azure Blob Storageモジュールは以下のアセンブリで構成されています:

-   Sitecore.Framework.Data.Blobs - AzureStorageBlobProvider クラスを提供し、APIがブロブストレージと連携できるようにします。

Sitecoreは基盤となるブロブ提供者(例:AzureStorageBlobProvider)を含むBlobStorage APIを公開し、ブロブストレージと連携するための便利なAPIを提供します。デフォルトでは、BlobStorage APIは以下のアセンブリで構成されています。

-   Sitecore.Framework.Data.Blobs.Abstractions - Sitecore Blob Storage モジュール向けの blob プロバイダーを実装するための抽象化を提供します。
-   Sitecore.Framework.Data.Blobs - Sitecoreで設定されたブロブプロバイダーを知らずに、Blobストレージを扱える便利なAPIを提供します。

## BlobStorage API

Sitecoreのデータベースは、1つ以上のストレージをBlobストレージに使用できます。Sitecoreでは、Database.BlobStorageプロパティにはSitecoreデータベースと連携するBlobプロバイダーのリストが含まれています。また、Blobストレージの拡張機能として機能するためのAPIも記載されており、Sitecoreで設定されたBlobプロバイダーについて知らなくても利用できます。

SitecoreのBlobStorageにアクセスするには以下のコードを使用します。

BlobStorage blobStorage = Database.BlobStorage;

以下のクラス名例は、BlobStorage拡張のSitecore APIの使い方を示しています。

-   **方法:**BlobExists
-   **説明**:このAPIをBlob IDで使用し、Blobストレージアカウントにblobが存在するかどうかを確認します。
-   **例:**

try
{
 var blobId = blobStorage.CreateBlobIdentifier(new Guid("B4D19D07-B3EB-4F7D-98EC-8BCB41CCC58E"));
 var isBlobExists = blobStorage.BlobExists(blobId);
}
catch (BlobProviderException ex)
{
 // Handle exception
}

-   **方法:**GetBlob
-   **説明**:このAPIをBlob IDで使い、Blob Storageから特定のブロブを取得します。
-   **例:**

try
 {
 var blobId = blobStorage.CreateBlobIdentifier(new Guid("B4D19D07-B3EB-4F7D-98EC-8BCB41CCC58E"));
 var blobStream = blobStorage.GetBlob(blobId);
 }
 catch (BlobProviderException ex)
 {
 // Handle exception
 }

-   **方法:**RemoveBlob
-   **説明**:このAPIをBlob IDで使用し、Blob StorageからBlobを削除してください。
-   **例:**

try
 {
 var blobId = blobStorage.CreateBlobIdentifier(new Guid("B4D19D07-B3EB-4F7D-98EC-8BCB41CCC58E"));
 blobStorage.RemoveBlob(blobId);
 }
 catch (BlobProviderException ex)
 {
 // Handle exception
 }

-   **方法:**SetBlob
-   **説明**:このAPIをBlob IDで使用し、BlobをBlob Storageに保存します。
-   **例:**

try
 {
 var blobId = blobStorage.CreateBlobIdentifier();
 using (var blobStream = new MemoryStream(Encoding.UTF8.GetBytes("SampleStream")))
 {
 blobStorage.SetBlob(blobStream, blobId);
 }
 }
 catch (BlobProviderException ex)
 {
 // Handle exception
 }
## 孤児の塊を除去する

Sitecoreのブロブストレージ・ガベージコレクション機能は、Sitecoreから孤立したブロブを削除するのに役立ちます。削除を開始するには以下の方法があります: Sitecore.Data.DefaultDatabase.BlobStorage.CleanupOrphanBlobs。

例えば:

var database = Sitecore.Configuration.Factory.GetDatabase("master");
database.BlobStorage.CleanupOrphanBlobs();
## AzureStorageBlobProvider APIs

必要に応じて、ブロブプロバイダーに直接アクセスする方法がいくつかあります:

-   例1:
    
    IBlobProviderAsync blobProvider = blobStorage.GetBlobProvider<AzureStorageBlobProvider>();
    
-   例2:
    
    IBlobProviderAsync blobProvider = blobStorage.GetDefaultBlobProvider<IBlobProviderAsync>();
    
-   例3:
    
    BlobIdentifier blobId = new BlobIdentifier("B4D19D07-B3EB-4F7D-98EC-8BCB41CCC58E");
    IBlobProviderAsync blobProvider = blobStorage.GetBlobProvider<IBlobProviderAsync>(blobId);
    

以下のクラス名例は、BlobProviderメソッドのSitecore APIの使い方を示しています。

-   **方法:**BlobExists
-   **説明**:このAPIをBlob IDで使用し、BlobがBlob Storageに存在しているか確認します。
-   **例:**

try
 {
 var blobId = blobProvider.CreateBlobIdentifier(new Guid("B4D19D07-B3EB-4F7D-98EC-8BCB41CCC58E"));
 var isBlobExists = blobProvider.BlobExists(blobId);
 }
 catch (BlobProviderException ex)
 {
 // Handle exception
 }

-   **方法:**GetBlob
-   **説明**:このAPIをBlob IDで使い、Blob Storageから特定のブロブを取得します。
-   **例:**

try
 {
 var blobId = blobProvider.CreateBlobIdentifier(new Guid("B4D19D07-B3EB-4F7D-98EC-8BCB41CCC58E"));
 var blobStream = blobProvider.GetBlob(blobId);
 }
 catch (BlobProviderException ex)
 {
 // Handle exception
 }

-   **方法:**RemoveBlob
-   **説明**:このAPIをBlob IDで使用し、Blobストレージからブロブを削除します。
-   **例:**

try
            {
               blobProvider.RemoveBlob(blobId);
            }
           catch (BlobProviderException ex)
            {
                // Handle exception
            }

-   **方法:**SetBlob
-   **説明**:このAPIをBlob IDで使用し、Blob StorageにBlobを保存します。
-   **例:**

try
 {
 var blobId = blobProvider.CreateBlobIdentifier();
 using (var blobStream = new MemoryStream(Encoding.UTF8.GetBytes("SampleStream")))
 {
 blobProvider.SetBlob(blobStream, blobId);
 }
 }
 catch (BlobProviderException ex)
 {
 // Handle exception
 }
## Extend AzureStorageBlobProvider APIs

例えば、GetBlob APIを拡張したい場合は、ブロブプロバイダーメソッドを拡張できます。

using Sitecore.Framework.Data.Blobs.Abstractions;
using Sitecore.Framework.Data.Blobs.Azure;
using System.IO;

namespace Documentation
{
 public class ExtendGetBlob : AzureStorageBlobProvider
 {
 public ExtendGetBlob(string connectionStringName, IPagedListProvider usedBlobIdsPagedListProvider) :
 base(connectionStringName, usedBlobIdsPagedListProvider)
 {
 }

 public override Stream GetBlob(BlobIdentifier identifier)
 {
 var blobStream = base.GetBlob(identifier);

 // Code to extend the GetBlob default behavior

 return blobStream;
 }
 }
}
## 新しいBlobProviderを作成する

例えば、Sitecore.Framework.Data.Blobs.Abstractions APIを使って自分でブロブプロバイダーを作成できます。例えば、InMemoryBlobProvider APIを作成したい場合は:

using Sitecore.Framework.Conditions;
using Sitecore.Framework.Data.Blobs.Abstractions;
using System;
using System.Collections.Generic;
using System.IO;

namespace Documentation
{
 public class InMemoryBlobProvider : IBlobProvider
 {
 private readonly string \_scheme = "memoryblob";
 private IDictionary<Uri, byte> blobs = new Dictionary<Uri, byte>();

 private void ValidateIdentifier(BlobIdentifier identifier)
 {
 if (!CanHandle(identifier))
 throw new InvalidOperationException(
 $"The identifier '{identifier}' is not in acceptable format by this provider");
 }

 private byte GetBytes(Stream input)
 {
 using (MemoryStream ms = new MemoryStream())
 {
 input.CopyTo(ms);
 return ms.ToArray();
 }
 }

 public bool BlobExists(BlobIdentifier identifier)
 {
 Condition.Requires(identifier, nameof(identifier)).IsNotNull();

 if (string.IsNullOrEmpty(identifier.ToString()))
 {
 return false;
 }

 ValidateIdentifier(identifier);

 try
 {
 return blobs.ContainsKey(identifier.ToUri());
 }
 catch (Exception ex)
 {
 throw new BlobProviderException(ex.Message, ex.InnerException);
 }
 }

 public bool CanHandle(BlobIdentifier identifier)
 {
 return (identifier != null) &&
 Uri.TryCreate(identifier.ToString(), UriKind.Absolute, out Uri result) &&
 result.Scheme != null &&
 result.Scheme.Equals(\_scheme, StringComparison.InvariantCultureIgnoreCase);
 }

 public BlobIdentifier CreateBlobIdentifier()
 {
 return new BlobIdentifier($"{\_scheme}://{Guid.NewGuid()}");
 }

 public BlobIdentifier CreateBlobIdentifier(Guid guid)
 {
 Condition.Requires(guid, nameof(guid)).IsNotEqualTo(Guid.Empty);

 return new BlobIdentifier($"{\_scheme}://{guid}");
 }

 public Guid ToGuid(BlobIdentifier identifier)
 {
 Condition.Requires(identifier, nameof(identifier)).IsNotNull();
 ValidateIdentifier(identifier);

 return new Guid(new Uri(identifier.ToString()).Host);
 }

 public Stream GetBlob(BlobIdentifier identifier)
 {
 Condition.Requires(identifier, nameof(identifier)).IsNotNull();

 if (string.IsNullOrEmpty(identifier.ToString()))
 {
 return null;
 }

 ValidateIdentifier(identifier);

 try
 {
 if (blobs.ContainsKey(identifier.ToUri()))
 {
 return new MemoryStream(blobsidentifier.ToUri());
 }
 else
 {
 return null;
 }
 }
 catch (Exception ex)
 {
 throw new BlobProviderException(ex.Message, ex.InnerException);
 }
 }

 public void RemoveBlob(BlobIdentifier identifier)
 {
 Condition.Requires(identifier, nameof(identifier)).IsNotNull();

 ValidateIdentifier(identifier);

 if (blobs.ContainsKey(identifier.ToUri()))
 {
 blobs.Remove(identifier.ToUri());
 }
 }

 public void SetBlob(Stream stream, BlobIdentifier identifier)
 {
 Condition.Requires(stream, nameof(stream)).IsNotNull();
 Condition.Requires(identifier, nameof(identifier)).IsNotNull();

 ValidateIdentifier(identifier);

 blobsidentifier.ToUri() = GetBytes(stream);
 }

 }

 public static class BlobIdentifierExtensions
 {
 public static bool IsUri(this BlobIdentifier identifier)
 {
 if (identifier != null && !string.IsNullOrWhiteSpace(identifier.ToString()))
 {
 string blobId = identifier.ToString();
 if (Uri.IsWellFormedUriString(blobId, UriKind.Absolute))
 {
 return true;
 }
 }
 return false;
 }

 public static Uri ToUri(this BlobIdentifier identifier)
 {
 Condition.Requires(identifier, nameof(identifier)).IsNotNull();

 if (!identifier.IsUri())
 {
 throw new ArgumentException($"Provided identifier {identifier.ToString()} is not a valid Uri.");
 }

 return new Uri(identifier.ToString());
 }
 }
}