【问题标题】:Windows azure blob storage from a SQL SSIS packageSQL SSIS 包中的 Windows azure blob 存储
【发布时间】:2013-11-06 21:29:46
【问题描述】:

是否可以将图像从 SQL SSIS 包上传到 Windows azure blob 存储? SSIS 将从我的一个本地 SQL Server(表)中读取新图像(每天),并将图像上传到 blob 存储。

【问题讨论】:

  • 我的回答中有什么没有解决的吗?这对你有用吗?
  • 感谢 billinkc 抽出时间为我找到解决方案。我正在考虑将与天蓝色相关的代码推送到 Web 服务中,并在我的脚本组件中包含服务引用。但是,我更喜欢您的解决方案。再次感谢。

标签: image ssis azure-storage


【解决方案1】:

这是一个多么有趣的问题!我必须将许多我从未尝试过的部分拼接在一起。

我首先基于HOW TO: Blob Storage 上的精美手册构建了一个简单的控制台应用程序。知道我有可以工作的代码让我可以根据 SSIS 调整它。

我在包级别创建了 3 个 SSIS 变量。 AccountName、AccountKey 和 ContainerName。它们都是数据类型字符串。这些提供凭据 + 我上传的数据所在的文件夹。

数据流

您的数据流的总体外观相当简单。将充当目标的脚本组件的数据源。您将需要两列:一列为 blob 提供唯一名称,另一列是二进制位。

我的来源是一张普通的桌子。它有国家名称和它们的标志(存储为 varbinary(max)),如果你愿意的话,你可以自己从 CIA 世界手册中刮下来。

Destination 会有点 C#。添加一个 Destination 类型的脚本组件。

在脚本选项卡上,我列出了 3 个只读变量User::AccountKey,User::AccountName,User::ContainerName

在“输入列”选项卡上,我选择CountryNameFlagImage

脚本本身如下。如操作方法中所述,您需要添加对 Microsoft.WindowsAzure.Storage 程序集的引用,然后才能访问那里的最后 3 个程序集。

using System;
using System.Data;
using System.IO;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;

// Must add reference to Microsoft.WindowsAzure.Storage for this to work
// http://www.windowsazure.com/en-us/develop/net/how-to-guides/blob-storage/
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;

/// <summary>
/// Watch me load data to Azure from SSIS
/// </summary>
[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{

    /// <summary>
    /// The storage account used
    /// </summary>
    private CloudStorageAccount storageAccount;

    /// <summary>
    /// An entity to work with the Blobs
    /// </summary>
    private CloudBlobClient blobClient;

    /// <summary>
    /// Blobs live in containers
    /// </summary>
    private CloudBlobContainer container;

    /// <summary>
    /// blockBlob instead of a pageBlob
    /// </summary>
    private CloudBlockBlob blockBlob;

    /// <summary>
    /// This method is called once, before rows begin to be processed in the data flow.
    ///
    /// You can remove this method if you don't need to do anything here.
    /// </summary>
    public override void PreExecute()
    {
        base.PreExecute();
        string cs = string.Empty;
        string csTemplate = string.Empty;
        string accountName = string.Empty;
        string accountKey = string.Empty;
        string containerName = string.Empty;

        accountName = Variables.AccountName;
        accountKey = Variables.AccountKey;
        containerName = Variables.ContainerName;
        csTemplate = "DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}";
        cs = string.Format(csTemplate, accountName, accountKey);

        this.storageAccount = CloudStorageAccount.Parse(cs);
        this.blobClient = this.storageAccount.CreateCloudBlobClient();
        this.container = this.blobClient.GetContainerReference(containerName);
        this.container.CreateIfNotExists();
        this.container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });

    }

    /// <summary>
    /// For each row passing through, upload to Azure
    /// </summary>
    /// <param name="Row">The row that is currently passing through the component</param>
    public override void Input0_ProcessInputRow(Input0Buffer Row)
    {
        string blobName = string.Empty;

        using (MemoryStream memStream = new MemoryStream(Row.FlagImage.GetBlobData(0, (int)Row.FlagImage.Length)))
        {
            this.blockBlob = this.container.GetBlockBlobReference(Row.CountryName);
            this.blockBlob.UploadFromStream(memStream);
        }
    }

}

全局程序集缓存 (GAC)

您希望在 SSIS 中使用的程序集必须位于 GAC 中。除非经过签名,否则程序集不能进入 GAC。幸运的是,Azure 程序集是从 Visual Studio 命令提示符中签名的,因此键入 gacutil -if "C:\Program Files\Microsoft SDKs\Windows Azure\.NET SDK\v2.1\ref\Microsoft.WindowsAzure.Storage.dll" 或与您的程序集版本所在位置等效的位置

加载成功

作为证据,这是来自Azure Storage Explorer的照片

【讨论】:

  • GAC 和 BLOB ......听起来像个兄弟会
  • 好问题,好答案!
【解决方案2】:

SSIS 2012 及更高版本现在具有 Microsoft 支持的任务,可将数据上传/下载到 Azure 存储:

示例。 “用于 Azure 的 Microsoft SQL Server 2016 集成服务功能包”:https://www.microsoft.com/en-us/download/details.aspx?id=49492

如果您使用的是 2012 和 2014,只需搜索即可。

希望对您有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-08
    • 2012-08-29
    • 1970-01-01
    • 2021-07-09
    • 1970-01-01
    • 2015-01-08
    • 2017-11-10
    相关资源
    最近更新 更多