sql-server - 来自 SQL Server 的 Azure 存储中的 blob 列表

标签 sql-server azure azure-blob-storage

我们目前正在使用 Azure 存储来备份 SQL Server 数据库,我的任务是重新创建数据库中的 Blob 列表。目前,我们使用 BACKUP TO URL 以及存储在 sys.credentials 中的凭据。

是否可以使用数据库中保存的凭据直接从 SQL Server 获取存储帐户中的容器列表和/或获取容器中的 blob 列表?

最佳答案

Is it possible to get a list of containers in a storage account and/or get a list of blobs in a container straight from SQL Server with the saved credentials from the database?

正如 David Makogon 所说,如果您想列出 SQL Server 中的容器/blob,您可以尝试使用 SQL CLR 存储过程。我有一个工作示例来列出容器中的 blob,您可以引用它。

SQL CLR 存储过程

public partial class StoredProcedures
{
[Microsoft.SqlServer.Server.SqlProcedure]
public static void SqlStoredProcedure1 ()
{

    // Put your code here

    string StorageAccount = "myaccount";
    string StorageKey = "accountkey";

    string containername = "mycontainer";

    string requestMethod = "GET";
    string mxdate = "";
    string storageServiceVersion = "2014-02-14";

    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(string.Format(CultureInfo.InvariantCulture,
            "https://{0}.blob.core.windows.net/{1}?restype=container&comp=list",
            StorageAccount,
            containername
            ));

    req.Method = requestMethod;

    //specify request header

    mxdate = DateTime.UtcNow.ToString("R");

    string canonicalizedHeaders = string.Format(
            "x-ms-date:{0}\nx-ms-version:{1}",
            mxdate,
            storageServiceVersion);

    string canonicalizedResource = string.Format("/{0}/{1}\ncomp:list\nrestype:container", StorageAccount, containername);

    string stringToSign = string.Format(
        "{0}\n\n\n\n\n\n\n\n\n\n\n\n{1}\n{2}",
        requestMethod,
        canonicalizedHeaders,
        canonicalizedResource);

    HMACSHA256 hmac = new HMACSHA256(Convert.FromBase64String(StorageKey));

    string signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));

    String authorization = String.Format("{0} {1}:{2}",
        "SharedKey",
        StorageAccount,
        signature
        );
    string AuthorizationHeader = authorization;

    req.Headers.Add("Authorization", AuthorizationHeader);
    req.Headers.Add("x-ms-date", mxdate);
    req.Headers.Add("x-ms-version", storageServiceVersion);
    DataTable dt = new DataTable();
    using (HttpWebResponse response = (HttpWebResponse)req.GetResponse())
    {
        var stream = response.GetResponseStream();

        StreamReader reader = new StreamReader(stream);
        string content = reader.ReadToEnd();

        StringReader theReader = new StringReader(content);
        DataSet theDataSet = new DataSet();
        theDataSet.ReadXml(theReader);

        dt = theDataSet.Tables[2];
    }

    string blobs = "";

    foreach (DataRow item in dt.Rows)
    {
        blobs += item[0].ToString() + ";";
    }

    SqlContext.Pipe.Send(blobs);
}
}

公共(public)语言运行时 (CLR) 集成功能在 Microsoft SQL Server 中默认处于关闭状态,必须启用才能使用 SQL Server 项目项。至enable CLR integration ,使用 sp_configure 存储过程的 clr 启用选项。

您可以引用this tutorial了解如何使用SQL CLR存储过程。

关于sql-server - 来自 SQL Server 的 Azure 存储中的 blob 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40262977/

相关文章:

c# - 如何使用 C# 将一个文件从 Azure 文件共享中的一个文件夹复制到另一个文件夹?

azure - 是否可以在没有公共(public) CNAME 记录的情况下为 Azure Blob 存储静态网站添加自定义域?

Azure Blob - 是否有查询字符串选项来指定内容类型?

sql-server - 从本地时间获取美国的日期时间

sql-server - SQL Server 是否受到 SSD 存储上的碎片索引的影响

sql-server - 如何对总数求和但只保留多个名字中的一个

mysql - 动态地从 hive/sql 表中选择列

azure - 连接到新的 Azure 缓存(DataCache、DataCacheFactory 和连接池)

c# - 在本地调试 Azure 函数提供了一个有趣的 AppDomain.CurrentDomain.BaseDirectory 路径

azure - 在没有移动服务的情况下将图像上传到 Azure Blob 存储的体系结构