Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ResourceNotFound when accessing blob uri

I am trying to retrieve an image via its URL in an Azure Blob Storage, but it doesn't find it, this is what I get:

enter image description here

My code is as follows:

public async Task<bool> UploadFileAsync(string containerReference, string blobReference, string route)
    {
        CloudBlobContainer container = blobClient.GetContainerReference(containerReference);
        container.CreateIfNotExists();
        CloudBlockBlob blob = container.GetBlockBlobReference(blobReference);

        try
        {
            using (var fileStream = System.IO.File.OpenRead(route))
            {
                await blob.UploadFromStreamAsync(fileStream);
            }
        }
        catch (System.Exception)
        {
            return false;
        }

        return true;
    }

Which successfully uploads the file to the blob:

enter image description here

And then I try to retrieve its URL to directly access it:

public string GetBlobUrl(string containerReference, string blobReference)
    {
        CloudBlobContainer container = blobClient.GetContainerReference(containerReference);
        CloudBlockBlob blob = container.GetBlockBlobReference(blobReference);

        return blob.Uri.ToString();
    }

What am I doing wrong?

like image 896
Lisgaira Avatar asked Jun 27 '17 12:06

Lisgaira


2 Answers

By default, the container and its blobs can be accessed only by the storage account owner. If you want blobs within the private container can be read by anonymous request, you could grant public read access for blobs. Besides, as David Makogon mentioned in comment, you can also grant temporary public access to a private blob via shared access signature.

CloudBlobContainer container = cloudBlobClient.GetContainerReference("mycontainer");   
CloudBlockBlob blob = container.GetBlockBlobReference("testimg.PNG");

SharedAccessBlobPolicy sasConstraints = new SharedAccessBlobPolicy();
sasConstraints.SharedAccessStartTime = DateTimeOffset.UtcNow.AddMinutes(-5);
sasConstraints.SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddDays(7);
sasConstraints.Permissions = SharedAccessBlobPermissions.Read;


string sasBlobToken = blob.GetSharedAccessSignature(sasConstraints);
string URL = blob.Uri + sasBlobToken;
like image 165
Fei Han Avatar answered Sep 28 '22 18:09

Fei Han


Your code contains a line to create the container if it doesn't exist. By default, a blob container is created to be private:

By default, the new container is private, meaning that you must specify your storage access key to download blobs from this container.

If you want to make the files in the container publicly available, you can set the container to be public using the following code:

container.SetPermissions(new BlobContainerPermissions 
                             { 
                                 PublicAccess = BlobContainerPublicAccessType.Blob 
                             });

For more information, see Get started with Azure Blob storage using .NET. The description and the code above are taken from the paragraph "Create a container" of that page.

like image 34
rickvdbosch Avatar answered Sep 28 '22 18:09

rickvdbosch