gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:49:09 26 4
gpt4 key购买 nike

我们目前正在使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com