CloudBlockBlob.UploadFromByteArrayAsync returns,但没有创建图像

CloudBlockBlob.UploadFromByteArrayAsync returns, but no image has been created

我有一种方法可以将图像上传到 Azure blob 存储。我已经创建了我的帐户,并且在我的应用程序中放置了名称和密钥。我看到的行为是 await UploadFromByteArrayAsync(...) returns 和我的方法 returns a URL。但是,当我在 Microsoft Azure 存储资源管理器中导航到我的 azure blob 存储时,我可以看到没有创建任何 blob。显然,导航到 returns 方法返回的 URL 也是 404。该方法已成功创建我的容器,因此与我的存储帐户的适当权限有明确的联系,我检查了字节数组的内容并且它包含实际数据。有谁知道为什么我的图片从来没有上传过?

public async Task<string> UploadImage(byte[] imageByteArr)
{
    // Retrieve storage account from the connection string.
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=redacted;AccountKey=redacted;EndpointSuffix=core.windows.net");

    // Create the blob client.
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

    // Retrieve a reference to a previously created container.
    CloudBlobContainer container = blobClient.GetContainerReference("user-images");

    // Create the container if it doesn't already exist.
    await container.CreateIfNotExistsAsync().ConfigureAwait(false);

    var docId = Guid.NewGuid().ToString();
    CloudBlockBlob blockBlob = container.GetBlockBlobReference(docId);

    await blockBlob.UploadFromByteArrayAsync(imageByteArr, 0, imageByteArr.Length);

    blockBlob.Properties.ContentType = "image/jpg";
    await blockBlob.SetPropertiesAsync();

    return blockBlob.Uri.ToString();
}

我在创建 blob 教程时错过了一个步骤。 我们需要在代码隐藏中创建容器时调用以下代码,以便我们可以 public 访问上传的图像。

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