我想在我的移动应用中调用 InvokeApiAsync() 时返回在 Azure 中创建的 Blob 的名称
移动应用程序中的功能:
private const string PhotoResource = "photo";
public async Task UploadPhoto(MediaFile photo)
{
using (var s = photo.GetStream())
{
var bytes = new byte[s.Length];
await s.ReadAsync(bytes, 0, Convert.ToInt32(s.Length));
var content = new
{
Photo = Convert.ToBase64String(bytes)
};
var json = JToken.FromObject(content);
await Client.InvokeApiAsync(PhotoResource, json);
}
}
Azure 函数 - run.csx:
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req,
ILogger log)
{
dynamic data = await req.Content.ReadAsAsync<object>();
string photo = data?.Photo;
var imageBytes = Convert.FromBase64String(photo);
var connectionString =
ConfigurationManager.AppSettings["BlobStorageConnectionString"];
CloudStorageAccount storageAccount;
CloudStorageAccount.TryParse(connectionString, out storageAccount);
var blobClient = storageAccount.CreateCloudBlobClient();
var blobContainer = blobClient.GetContainerReference("beerphotos");
var blobName = Guid.NewGuid().ToString();
var blob = blobContainer.GetBlockBlobReference(blobName);
blob.Properties.ContentType = "image/jpg";
await blob.UploadFromByteArrayAsync(imageBytes, 0, imageBytes.Length);
log.LogInformation($"Blob {blobName} created");
//return req.CreateResponse(HttpStatusCode.OK);
//NEW CODE ADDED AFTER ANSWER FROM JASON
var response = req.CreateResponse();
response.StatusCode = HttpStatusCode.OK;
response.Content = new StringContent(blobName);
return response;
}
如果我尝试写 返回 req.CreateResponse(HttpStatusCode.OK,blobName); 我收到此错误:run.csx(35,16):错误CS1501:方法“CreateResponse”没有重载需要2个参数
我在 azure 函数中错过了什么?
在我的移动应用程序中调用 InvokeApiAsync() 应该如何读取 blob 名称?
编辑:
在 Azure 函数中添加新代码后,我收到以下未处理的异常: Newtonsoft.Json.JsonReaderException:输入字符串“813255ca-02d0-4feb-8012-2d5a0ad49464”不是有效数字。路径 '',第 1 行,位置 36。
移动函数中的 Client.InvokeApiAsync(PhotoResource, json) 返回响应时会引发异常。 “813255ca-02d0-4feb-8012-2d5a0ad49464”实际上是照片的名称。
最佳答案
返回响应正文中的数据
return req.CreateResponse(HttpStatusCode.OK) { Content = new StringContent(blobName) };
然后调用它时
var resp = await Client.InvokeApiAsync(PhotoResource, json);
if (resp.StatusCode == HttpStatusCode.OK) {
var guid = await resp.Content.ReadAsStringAsync();
}
关于c# - 如何在Azure API函数中返回变量并通过移动应用程序中的调用异步函数读取它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53980768/