我正在实施对JWPlayer Management API的Web客户端调用。我已经使用非加速上传方法成功创建了Web客户端调用。我现在正尝试使用WebClient实现一个PUT调用,以实现S3加速上传方法。
/*The documentation states:
Two-step process
S3 file upload is done using a two-step process:
First, /videos/create or /videos/update API call is done. The call will return an upload URL.
*/
其次,使用HTTP PUT方法将文件提交到此上传URL。
/*Upload URL
Response of the create or update API call includes a link block with parameters that should be used to construct the upload URL. For example, response of the /videos/create API call:
<?xml version="1.0" encoding="UTF-8"?>
<response>
<status>ok</status>
<video key="tL17msiU">
<link>
<protocol>https</protocol>
<address>upload-s3.jwplatform.com</address>
<path>/tL17msiU</path>
<query>
<AWSAccessKeyId>AKIAIRXCJ3TPZA4HVNYZ</AWSAccessKeyId>
<Expires>1482770374</Expires>
<Signature>1/l+L6/yOE05dNEbXHW8sw7TGF4=</Signature>
</query>
</link>
</video>
</response>
*/
应使用以下方案从链接块的各个部分连接上传URL:
upload_url = <link/protocol> + "://" + <link/address> + <link/path> +
"?AWSAccessKeyId=" + <link/query/AWSAccessKeyId> +
"&Expires=" + <link/query/Expires> +
"&Signature=" + urlencode(<link/query/Signature>)
这应该使用上面的响应示例生成以下上传URL:
https://upload-s3.jwplatform.com/tL17msiU
?AWSAccessKeyId=AKIAIRXCJ3TPZA4HVNYZ
&Expires=1482770374
&Signature=1%2Fl%2BL6%2FyOE05dNEbXHW8sw7TGF4%3D
上传文件
构建上传网址后,可以使用它来上传文件,例如curl工具:
/*
curl --request PUT --upload-file /file_path/file_name.mp4 \
"https://upload-s3.jwplatform.com /tL17msiU?AWSAccessKeyId=AKIAIRXCJ3TPZA4HVNYZ&Expires=1482770374& Signature=1%2Fl%2BL6%2FyOE05dNEbXHW8sw7TGF4%3D"***
*/
我已经成功生成了上传网址,但是我在使用.NET webClient调用替换通常在PHP中完成的curl调用时遇到了问题。我已经尝试了webClient.UploadString和WebClient.UpdateData,但我一直收到无效请求格式404错误。我的代码是:
//Ignore this line, this simply uses a 3rd party tool to generate the upload URL
BotRAPI api = new BotR.API.BotRAPI("xxx", "yyy");
//params to store with a new video
NameValueCollection col = new NameValueCollection() {
{"up load_method", "s3"}
};
//create the new video
string xml = api.Call("/videos/create", col);
XDocument doc = XDocument.Parse(xml);
var result = (from d in doc.Descendants("status")
select new
{
Status = d.Value
).FirstOrDefault();
//make sure the status was "ok" before trying to upload
if (result.Status.Equals("ok", StringComparison.CurrentCultureIgnore Case))
{
var response = doc.Descendants("link").FirstOrDefault();
String url = string.Format("{0}://{1}{2}", response.Element("protocol").Value, response.Element("address").Value, response.Element("path").Value);
response = doc.Descendants("query").FirstOrDefault();
url = string.Format(url + "?AWSAccessKeyId={0}&Expires={1}& Signature={2}", response.Element("AWSAccessKeyId").Value, response.Element("Expires").Value, response.Element("Signature").Value);
}
然后我尝试使用WebClient启动PUT调用:
string filePath = Path.Combine(Environment.CurrentDirectory, "test.mp4");
byte[] data = ReadAllBytes(FilePath);
MyWebClient client = WebClient();
client.UploadData(url,data);