Upload to Azure Blob Storage with Shared Access Key

后端 未结 1 869
抹茶落季
抹茶落季 2021-02-02 00:01

UPD: Here is my implemented solution to this problem

I\'m trying to upload to Azure blob storage via Azure.Storage library (not REST API) and authentica

相关标签:
1条回答
  • 2021-02-02 00:34

    The problem comes up on GetBlobReferenceFromServer - I get 404 error from Azure Storage. Yes, the blob does not exist and there is no reference. So given CloudBlobClient, how can I upload a file to a blob?

    For GetBlobReferenceFromServer to work, the blob must be present in the blob storage. This is useful in the scenario when you know that the blob exist in storage and would want to find out the type of blob - Block Blob or Page Blob.

    If you want to create a block blob by uploading a file from the local computer you could do something like:

        var blob = new CloudBlockBlob(new Uri(blobWithSas.AbsoluteUri), new StorageCredentials(blobWithSas.Sas));
    using (var stream = new FileStream(fullFilePath, FileMode.Open))
    {
        blob.UploadFromStream(stream);
    }
    

    Coming to shared access signature functionality, I wrote a blog post not too long ago about this: http://gauravmantri.com/2013/02/13/revisiting-windows-azure-shared-access-signature/. You can call it version 2 of Steve's blog post:). I've shown examples of uploading blobs with shared access signature using both REST API and Storage Client Library 2.0.

    Some code samples from the blog post:

    Using Storage Client Library:

    /// <summary>
    /// Uploads a blob in a blob container where SAS permission is defined on a blob container using storage client library.
    /// </summary>
    /// <param name="blobContainerSasUri"></param>
    static void UploadBlobWithStorageClientLibrarySasPermissionOnBlobContainer(string blobContainerSasUri)
    {
        CloudBlobContainer blobContainer = new CloudBlobContainer(new Uri(blobContainerSasUri));
        CloudBlockBlob blob = blobContainer.GetBlockBlobReference("sample.txt");
        string sampleContent = "This is sample text.";
        using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(sampleContent)))
        {
            blob.UploadFromStream(ms);
        }
    }
    

    Using REST API:

    /// <summary>
    /// Uploads a blob in a blob container where SAS permission is defined on a blob container using REST API.
    /// </summary>
    /// <param name="blobContainerSasUri"></param>
    static void UploadBlobWithRestAPISasPermissionOnBlobContainer(string blobContainerSasUri)
    {
        string blobName = "sample.txt";
        string sampleContent = "This is sample text.";
        int contentLength = Encoding.UTF8.GetByteCount(sampleContent);
        string queryString = (new Uri(blobContainerSasUri)).Query;
        string blobContainerUri = blobContainerSasUri.Split('?')[0];
        string requestUri = string.Format(CultureInfo.InvariantCulture, "{0}/{1}{2}", blobContainerUri, blobName, queryString);
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);
        request.Method = "PUT";
        request.Headers.Add("x-ms-blob-type", "BlockBlob");
        request.ContentLength = contentLength;
        using (Stream requestStream = request.GetRequestStream())
        {
            requestStream.Write(Encoding.UTF8.GetBytes(sampleContent), 0, contentLength);
        }
        using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse())
        {
    
        }
    }
    

    You may also find this blog post useful: http://gauravmantri.com/2013/02/16/uploading-large-files-in-windows-azure-blob-storage-using-shared-access-signature-html-and-javascript/

    0 讨论(0)
提交回复
热议问题