gpt4 book ai didi

c# - 将来自客户端的图像保存在 Azure 存储中

转载 作者:行者123 更新时间:2023-12-03 02:41:19 25 4
gpt4 key购买 nike

我有以下代码。它没有给我任何错误,但我的 Azure 存储文件夹中没有图像。

            var bytes = person.Image.GetBytes();
MemoryStream ms = new MemoryStream(bytes.Result);
var storageCredentials = new StorageCredentials("AccountName", "***/pEYRskVHuAtUhcvLT/Ct*****/71lLMUCgTybnm****B4WO/AGFe****==");
var cloudStorageAccount = new CloudStorageAccount(storageCredentials, true);
var cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();

var container = cloudBlobClient.GetContainerReference("https://*********.blob.core.windows.net/images");

var newBlob = container.GetBlockBlobReference("myfile.jpg");
newBlob.UploadFromStreamAsync(ms);

我应该在这里做什么?我存储了空图片?

private async Task UploadFile(string path,Stream stream)
{

CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=f********ge1;AccountKey=w9S**************==;EndpointSuffix=core.windows.net");

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

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

// Retrieve reference to a blob named "myblob".
CloudBlockBlob blockBlob = container.GetBlockBlobReference("img.jpg");
blockBlob.Properties.ContentType = "image/jpeg";

// Create or overwrite the "myblob" blob with contents from a local file.
using (var fileStream = stream)
{
await blockBlob.UploadFromStreamAsync(fileStream);
}

}

最佳答案

您遇到此问题很可能是因为您正在调用异步操作并且没有等待它完成:

newBlob.UploadFromStreamAsync(ms);

您可以通过等待此操作完成来解决此问题,例如:

ms.Position = 0;
await newBlob.UploadFromStreamAsync(ms);

或使用相同方法的sync版本:

ms.Position = 0;
newBlob.UploadFromStream(ms);

关于c# - 将来自客户端的图像保存在 Azure 存储中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61069469/

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