首页 文章

SQL SSIS包中的Windows azure blob存储

提问于
浏览
6

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

2 回答

  • 13

    这是多么有趣的问题!我要拼凑很多我从未尝试过的作品 .

    我首先根据HOW TO: Blob Storage上的精细手册构建了一个简单的控制台应用程序 . 知道我有工作代码允许我适应SSIS .

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

    data flow and variables

    数据流

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

    我的来源是一个简单的表 . 它有国家名称和他们的国旗(存储为varbinary(max)),如果你愿意,你可以从中央情报局世界手册中获取 .

    目的地将是一些C# . 添加Destination类型的脚本组件 .

    在“脚本”选项卡上,我列出了3个ReadOnly变量 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的一个镜头

    blobs everywhere

  • 3

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

    例 . "Microsoft SQL Server 2016 Integration Services Feature Pack for Azure":https://www.microsoft.com/en-us/download/details.aspx?id=49492

    只搜索2012年和2014年,如果这是你正在使用的 .

    希望有所帮助!

相关问题