1. Sitecoreブロブ ストレージ

BLOBストレージのAPIリファレンス

Version:
日本語翻訳に関する免責事項

このページの翻訳はAIによって自動的に行われました。可能な限り正確な翻訳を心掛けていますが、原文と異なる表現や解釈が含まれる場合があります。正確で公式な情報については、必ず英語の原文をご参照ください。

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

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

Sitecoreは、基になるBLOBプロバイダー (AzureStorageBlobProviderなど) を含むBlobStorage APIを公開し、BLOBストレージを操作するための便利なAPIを提供します。既定では、BlobStorage APIは次のアセンブリで構成されます。

  • Sitecore.Framework.Data.Blobs.Abstractions - Sitecore Blob StorageモジュールのBLOBプロバイダーを実装するための抽象化を提供します。

  • Sitecore.Framework.Data.Blobs - 便利なAPIを提供するため、Sitecoreで設定されたBLOBプロバイダーについて知らなくてもBLOBストレージを操作できます。

メモ

BLOB識別子の最大長には制限があり、BlobIdentifierクラスのコンストラクタの文字列引数の長さは200シンボルを超えてはなりません。

BlobStorage API

Sitecoreのデータベースは、BLOBストレージに1つ以上のストレージを使用できます。Sitecoreでは、Database.BlobStorageプロパティにSitecoreデータベースと連携するBLOBプロバイダーのリストが含まれています。このプロパティには、Sitecoreで設定されたBLOBプロバイダーについて知らなくても、BLOBストレージの拡張メソッドとして使用できるAPIもリストされています。

次のコードを使用して、SitecoreのBlobStorageにアクセスします。

BlobStorage blobStorage = Database.BlobStorage;

次のクラス名の例は、BlobStorage拡張機能からSitecore APIを使用する方法を示しています。

Method

BlobExists

Description

このAPIをBlob IDと共に使用して、Blob StorageアカウントにBLOBが存在するかどうかを確認します。

Example

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

Method

GetBlob

Description

このAPIをBlob IDと共に使用して、Blob Storageから特定のBLOBを取得します。

Example

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

Method

RemoveBlob

Description

このAPIをBlob IDと共に使用して、Blob StorageからBLOBを削除します。

Example

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

Method

SetBlob

Description

このAPIをBlob IDと共に使用して、Blob StorageにBLOBを保存します。

Example

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

孤立したブロブを削除する

SitecoreのBLOBストレージ ガベージ コレクション機能は、孤立したBLOBをSitecoreから削除するのに役立ちます。次の方法を使用して削除を開始します。 Sitecore.Data.DefaultDatabase.BlobStorage.CleanupOrphanBlobs.

例えば:

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

AzureStorageBlobProvider API

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

  • 例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を使用する方法を示しています。

Method

BlobExists

Description

このAPIをBlob IDと共に使用して、BLOBがBlob Storageに存在するかどうかを確認します。

Example

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

Method

GetBlob

Description

このAPIをBlob IDと共に使用して、Blob Storageから特定のBLOBを取得します。

Example

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

Method

RemoveBlob

Description

このAPIをBlob IDと共に使用して、Blob StorageからBLOBを削除します。

Example

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

Method

SetBlob

Description

このAPIをBlob IDと共に使用して、Blob StorageにBLOBを保存します。

Example

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

AzureStorageBlobProvider APIの拡張

たとえば、GetBlob APIを拡張する場合など、BLOBプロバイダー メソッドを拡張できます。

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を作成する

たとえば、InMemoryBlobProvider APIを作成する場合など、Sitecore.Framework.Data.Blobs.Abstractions APIを使用して独自のBLOBプロバイダーを作成できます。

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(blobs[identifier.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);

            blobs[identifier.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());
        }
    }
}
この記事を改善するための提案がある場合は、 お知らせください!