简体   繁体   English

从 Azure SAS URI 创建 BlobContainerClient

[英]Create BlobContainerClient from Azure SAS URI

In the Microsoft.Azure.Storage.Blob library you could create a blob upload client using a SAS URI.在 Microsoft.Azure.Storage.Blob 库中,您可以使用 SAS URI 创建 blob 上传客户端。

var cloudBlockBlob = new CloudBlockBlob(blobSasUri);

However, this library has been deprecated in favor of Azure.Storage.Blobs.但是,此库已被弃用,取而代之的是 Azure.Storage.Blobs。 I am unsure how to create an instance of BlobContainerClient using a SAS URI since none of the class's constructors allow for a URI parameter.我不确定如何使用 SAS URI 创建 BlobContainerClient 的实例,因为该类的构造函数均不允许使用 URI 参数。 Is this even possible?这可能吗? Or do I need to just use an HttpClient to call the Blob service?或者我是否只需要使用 HttpClient 来调用 Blob 服务?

In the library Azure.Storage.Blobs , you can create the BlobContainerClient instance by using this constructor:在库Azure.Storage.Blobs中,您可以使用此构造函数创建BlobContainerClient实例:

public BlobContainerClient(Uri blobContainerUri, BlobClientOptions options = null);

For example, you have a container sas uri like below:例如,您有一个container sas uri ,如下所示:

"https://xxx.blob.core.windows.net/mycontainer111?sv=2019-12-12&ss=bfqt&srt=sco&sp=rwdlacupx&se=2020-10-27T08:45:57Z&st=2020-10-27T00:45:57Z&spr=https&sig=xxxxxx";

Then you can use the code below to create BlobContainerClient instance:然后你可以使用下面的代码创建BlobContainerClient实例:

        string sasuri = "https://xxx.blob.core.windows.net/mycontainer111?sv=2019-12-12&ss=bfqt&srt=sco&sp=rwdlacupx&se=2020-10-27T08:45:57Z&st=2020-10-27T00:45:57Z&spr=https&sig=xxxxxx";
        Uri container_uri = new Uri(sasuri);
        var container_client = new BlobContainerClient(container_uri);

        //other code

Indeed, it seems that it has been deprecated in favor of Azure.Storage.Blobs.事实上,它似乎已被弃用,取而代之的是 Azure.Storage.Blobs。

You need to create get a reference to the CloudBlobContainer from the container where you want to upload to:您需要从要上传到的容器中创建对CloudBlobContainer的引用:

var connectionString = String.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}",
    storageAccountName, // your storage account name
    accessKey); // your storage account access key

var storageAccount = CloudStorageAccount.Parse(connectionString);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("my-container");

Specify and retrieve the sas URI指定并检索 sas URI

SharedAccessBlobPolicy sasConstraints = new SharedAccessBlobPolicy();
sasConstraints.SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(30);
sasConstraints.Permissions = SharedAccessBlobPermissions.Write | SharedAccessBlobPermissions.Create;

var sasUri = blob.GetSharedAccessSignature(sasConstraints);

Upload files:上传文件:

var cloudBlockBlob = new CloudBlockBlob(new Uri(sasUri));
await cloudBlockBlob.UploadFromFileAsync(@"c:\myfile.txt");

More details at this link链接的更多详细信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 创建一个使用 Azure 函数(HTTP 触发器)将图像发布到 Azure Blob 容器并使用 SAS 返回同一图像的 URI 的 Web 服务 - create a web service which uses Azure Functions (HTTP Trigger) to Post images to a Azure Blob Container and return the URI of the same image using SAS 使用 BlobContainerClient 使用 Blob 存储将大文件上传到 Azure - Large Fileupload to Azure with Blob Storage with BlobContainerClient 如何模拟 Azure.Storage.Blobs 的 BlobContainerClient()? - How to Mock BlobContainerClient() of Azure.Storage.Blobs? 由于缺少 Azure 存储连接字符串和 SAS 连接 uri,从 Blob 存储的秘密初始化失败 - Secret initialization from Blob storage failed due to missing both an Azure Storage connection string and a SAS connection uri TimeTrigger 异常“无法为 ScheduleMonitor 创建 BlobContainerClient。” - TimeTrigger Exception "Could not create BlobContainerClient for ScheduleMonitor." 使用最新的 azure 版本生成 SAS uri 时出错 - Getting error while generating SAS uri using latest azure version Azure表存储 - 从只读SAS令牌创建连接字符串 - Azure Table Storage - Create Connection String from a read-only SAS token 从IS绝对Uri创建新Uri? - Create a new Uri from an Absolute Uri AS IT IS? 尝试为(Azurite)Azure BLOB 存储生成 SAS URI 时,“服务器无法对请求进行身份验证” - 'Server failed to authenticate the request' when attempting to generate SAS URI for (Azurite) Azure BLOB storage Azure Blob 403禁止使用SAS创建和使用BlobContainer - Azure Blob 403 Forbidden using SAS to create and use BlobContainer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM