这是一个多么有趣的问题!我必须将许多我从未尝试过的部分拼接在一起。
我首先基于HOW TO: Blob Storage 上的精美手册构建了一个简单的控制台应用程序。知道我有可以工作的代码让我可以根据 SSIS 调整它。
我在包级别创建了 3 个 SSIS 变量。 AccountName、AccountKey 和 ContainerName。它们都是数据类型字符串。这些提供凭据 + 我上传的数据所在的文件夹。
数据流
您的数据流的总体外观相当简单。将充当目标的脚本组件的数据源。您将需要两列:一列为 blob 提供唯一名称,另一列是二进制位。
我的来源是一张普通的桌子。它有国家名称和它们的标志(存储为 varbinary(max)),如果你愿意的话,你可以自己从 CIA 世界手册中刮下来。
Destination 会有点 C#。添加一个 Destination 类型的脚本组件。
在脚本选项卡上,我列出了 3 个只读变量User::AccountKey,User::AccountName,User::ContainerName
在“输入列”选项卡上,我选择CountryName 和FlagImage。
脚本本身如下。如操作方法中所述,您需要添加对 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的照片