如何将ZIP文件从API管理上传到Blob存储 [英] How to upload ZIP File from API Management to Blob Storage
问题描述
- 我正在通过其中一个Azure Api Mangement API发送POST请求
-
在此发布请求中,有一个json正文,其中包含base64编码的数据(zip文件),如以下示例所示:
- I am sending a POST Request on one of my Azure Api Mangement APIs
In this post request there is a json body which contains base64 encoded data (zip file) like in this example:
{"foo":"bar","data":您的base64字符串"}
{ "foo": "bar", "data": "your-base64-string" }
在API策略中,我想向Azure存储REST API发送一个单独的请求,以将上述base64字符串另存为zip文件.
In the API Policy, I want to send a separate request to the Azure Storage REST API to save the above mentioned base64 string as a zip file.
现在的问题是:我可以成功地向Azure存储REST API发送请求,并通过以下语句下载生成的zip文件:
Now the problem: I can successfully send a request to the Azure Storage REST API and download the resulting zip file via this statement:
<set-body>@{
var base64String = (string)context.Variables.GetValueOrDefault<JObject>("body")["data"]) ;
var bytes = Convert.FromBase64String(base64String);
var ascii = Encoding.ASCII.GetString(bytes);
return ascii;
}</set-body>
问题是,我可以下载并打开zip文件,但是由于存档损坏,我无法提取其中的内容.
The thing is, I can download and open the zip file but I cannot extract the content of it as the archive got damaged.
这是一个示例base64编码的zip文件字符串(包含text.txt的ZIP文件):
Here is an example base64 encoded zip file string (ZIP File containing a text.txt):
<代码> UEsDBBQAAAAIAL1ZxE7MTbG/EwAAABMAAAAIAAAAdGVzdC50eHQLycgsVgCiRIWS1OKStMycVD0AUEsBAh8AFAAAAAgAvVnETsxNsb8TAAAAEwAAAAgAJAAAAAAAAAAgAAAAAAAAAHRlc3QudHh0CgAgAAAAAAABABgAI8Ac2LUa1QEjwBzYtRrVAZFuI861GtUBUEsFBgAAAAABAAEAWgAAADkAAAAAAA ==
这是我将其发布到Rest API的代码:
Here is my code for posting it to the Rest API:
<send-request mode="new" response-variable-name="putStorageRequest" timeout="5" ignore-error="true">
<set-url>@((string)context.Variables["blobUrl"])</set-url>
<set-method>PUT</set-method>
<set-header name="x-ms-date" exists-action="override">
<value>@((string) context.Variables["date"] )</value>
</set-header>
<set-header name="x-ms-version" exists-action="override">
<value>@((string) context.Variables["version"] )</value>
</set-header>
<set-header name="x-ms-blob-type" exists-action="override">
<value>BlockBlob</value>
</set-header>
<set-header name="Content-Type" exists-action="override">
<value>application/zip</value>
</set-header>
<set-header name="Authorization" exists-action="override">
<value>@{
string body = context.Request.Body.As<string>(preserveContent: true);
string contentType = "application/zip";
var base64String = (string)context.Variables.GetValueOrDefault<JObject>("body")["data"]) ;
var bytes = Convert.FromBase64String(base64String);
var ascii = Encoding.ASCII.GetString(bytes)
var contentLength = ascii.Length;
var hmacSha256 = new System.Security.Cryptography.HMACSHA256 { Key = Convert.FromBase64String(context.Variables.GetValueOrDefault<string>("storageKey")) };
var payLoad = string.Format("{0}\n\n\n{1}\n\n{2}\n\n\n\n\n\n\nx-ms-blob-type:BlockBlob\nx-ms-date:{3}\nx-ms-version:{4}\n{5}",
"PUT",
contentLength,
contentType,
context.Variables["date"],
context.Variables["version"],
"/" + context.Variables.GetValueOrDefault<string>("storageAccountName") + context.Variables.GetValueOrDefault<string>("resource"));
return "SharedKey "+ context.Variables.GetValueOrDefault<string>("storageAccountName") + ":" + Convert.ToBase64String(hmacSha256.ComputeHash(Encoding.UTF8.GetBytes(payLoad)));
}</value>
</set-header>
<set-body>@{
var base64String = (string)context.Variables.GetValueOrDefault<JObject>("body")["data"]) ;
var bytes = Convert.FromBase64String(base64String);
var ascii = Encoding.ASCII.GetString(bytes);
return ascii;
}</set-body>
</send-request>
推荐答案
Azure API Management现在在正文中支持byte []:
Azure API Management now supports byte[] in body:
https://azure.microsoft.com/en-us/updates/azure-api-management-update-september-2019
set-body策略还接受字节数组作为正文内容.
The set-body policy also accepts byte array as body content.
通过在Blob-Storage中创建一个zip文件,下载,打开该zip文件并打开包含的文本文件,我的测试成功了.
My test was successful by creating a zip-file in Blob-Storage, downloaded it, opened the zip-file and opened the containing Text-file.
使用了以下代码:
<set-header name="Authorization" exists-action="override">
<value>@{
string body = context.Request.Body.As<string>(preserveContent: true);
string contentType = "application/zip";
var base64String = (string)context.Variables.GetValueOrDefault<JObject>("body")["data"] ;
var bytes = Convert.FromBase64String(base64String);
var contentLength = bytes.Length;
var hmacSha256 = new System.Security.Cryptography.HMACSHA256 { Key = Convert.FromBase64String(context.Variables.GetValueOrDefault<string>("storageKey")) };
var payLoad = string.Format("{0}\n\n\n{1}\n\n{2}\n\n\n\n\n\n\nx-ms-blob-type:BlockBlob\nx-ms-date:{3}\nx-ms-version:{4}\n{5}",
"PUT",
contentLength,
contentType,
context.Variables["date"],
context.Variables["version"],
"/" + context.Variables.GetValueOrDefault<string>("storageAccountName") + context.Variables.GetValueOrDefault<string>("resource"));
return "SharedKey "+ context.Variables.GetValueOrDefault<string>("storageAccountName") + ":" + Convert.ToBase64String(hmacSha256.ComputeHash(Encoding.UTF8.GetBytes(payLoad)));
}</value>
</set-header>
<set-body>@{
var base64String = (string)context.Variables.GetValueOrDefault<JObject>("body")["data"] ;
var bytes = Convert.FromBase64String(base64String);
return bytes;
}</set-body>
我只需要删除:
var ascii = Encoding.ASCII.GetString(bytes);
这篇关于如何将ZIP文件从API管理上传到Blob存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!