Uploading File using Credentials
The upload policy credentials JSON is used for uploading files to your VdoCipher account, using an HTTP POST request. You would create a form-data object containing the following keys received from the API server response in Step 1:
- policy
- x-amz-signature
- x-amz-date
- key
- x-amz-algorithm
- x-amz-credential
The request for upload credentials also returns the uploadLink URL, to which the API POST request is to be made. In addition to the above parameters, the following need to be added:
- success_action_status - 201
- success_action_redirect - Set this to empty string if you do not have a specific redirect page
- file - This is the path to the file on your content provider's device. The file should always be appended to the end of the upload policy.
Upload File Sample Code#
The values to be input from the upload policy are entered in the following format - {{policy}}
- CURL
- NODE
- PHP
- C#
- Python
- Ruby
curl -X POST \ 'https://{s3-bucket-url}}.amazonaws.com' \ -H 'Content-Type: application/x-www-form-urlencoded' \ -H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \ -F 'policy={{policy}}' \ -F 'key={{key}}' \ -F 'x-amz-signature={{x-amz-signature}}' \ -F 'x-amz-algorithm={{x-amz-algorith}}' \ -F 'x-amz-date={{x-amz-date}}' \ -F 'x-amz-credential={{x-amz-credential}}' \ -F success_action_status=201 \ -F 'success_action_redirect='\'''\''' \ -F 'file=@fileDir/fileName.mp4'var fs = require("fs");var request = require("request");
var options = { method: "POST", url: "https://{s3-bucket-url}}.amazonaws.com", headers: { "content-type": "multipart/form-data" }, formData: { policy: "{{policy}}", key: "{{key}}", "x-amz-signature": "{{x-amz-signature}}", "x-amz-algorithm": "{{x-amz-algorith}}", "x-amz-date": "{{x-amz-date}}", "x-amz-credential": "{{x-amz-credential}}", success_action_status: "201", success_action_redirect: "", file: { value: fs.createReadStream("/fileDir/fileName.mp4"), options: { filename: "/fileDir/fileName.mp4", contentType: null }, }, },};
request(options, function (error, response, body) { if (error) throw new Error(error); console.log(body);});<?php// below response was obtained in Step 1$responseObj = json_decode($response);$uploadCredentials = $responseObj->clientPayload;
// save this id in your database with status 'upload-pending'var_dump($responseObj->videoId);
$filePath = '/home/username/sample-videos/The Daily Dweebs.mp4';$ch = curl_init($uploadCredentials->uploadLink);curl_setopt ($ch, CURLOPT_POST, 1);curl_setopt ($ch, CURLOPT_POSTFIELDS, [ 'policy' => $uploadCredentials->policy, 'key' => $uploadCredentials->key, 'x-amz-signature' => $uploadCredentials->{'x-amz-signature'}, 'x-amz-algorithm' => $uploadCredentials->{'x-amz-algorithm'}, 'x-amz-date' => $uploadCredentials->{'x-amz-date'}, 'x-amz-credential' => $uploadCredentials->{'x-amz-credential'}, 'success_action_status' => 201, 'success_action_redirect' => '', 'file' => new \CurlFile($filePath, 'image/png', 'filename.png'),]);curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
// get response from the server$response = curl_exec($ch);$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);$err = curl_error($curl);
curl_close($ch);
if (!$err && $httpcode === 201) { // upload is successful // update database echo "upload successful";} else { // write to error logs echo "upload failed due to " . (($err) ? $err : $response);}var client = new RestClient("https://{s3-bucket-url}}.amazonaws.com");var request = new RestRequest(Method.POST);request.AddHeader("Content-Type", "application/x-www-form-urlencoded");request.AddHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");request.AddParameter("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"policy\"\r\n\r\n{{policy}}\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"key\"\r\n\r\n{{key}}\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"x-amz-signature\"\r\n\r\n{{x-amz-signature}}\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"x-amz-algorithm\"\r\n\r\n{{x-amz-algorith}}\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"x-amz-date\"\r\n\r\n{{x-amz-date}}\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"x-amz-credential\"\r\n\r\n{{x-amz-credential}}\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"success_action_status\"\r\n\r\n201\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"success_action_redirect\"\r\n\r\n''\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"file\"; filename=\"{{fileName}}.mp4\"\r\nContent-Type: video/mp4\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--", ParameterType.RequestBody);IRestResponse response = client.Execute(request);$ pip install requests-toolbeltimport jsonimport requestsfrom requests_toolbelt import MultipartEncoder
############# STEP 1 ####################
api_secret_key = "________ENTER KEY HERE"querystring = {"title":"_________ENTER VIDEO TITLE_______"}
url = "https://dev.vdocipher.com/api/videos"headers = { 'Authorization': "Apisecret " + api_secret_key}
response = requests.request("PUT", url, headers=headers, params=querystring)
############# STEP 2 ####################
uploadInfo = response.json()clientPayload = uploadInfo['clientPayload']uploadLink = clientPayload['uploadLink']filename = 'sample.mp4' # use file name here
m = MultipartEncoder(fields=[ ('x-amz-credential', clientPayload['x-amz-credential']), ('x-amz-algorithm', clientPayload['x-amz-algorithm']), ('x-amz-date', clientPayload['x-amz-date']), ('x-amz-signature', clientPayload['x-amz-signature']), ('key', clientPayload['key']), ('policy', clientPayload['policy']), ('success_action_status', '201'), ('success_action_redirect', ''), ('file', ('filename', open(filename, 'rb'), 'text/plain')) ])
response = requests.post( uploadLink, data=m, headers={'Content-Type': m.content_type})
response.raise_for_status()require 'uri'require 'net/http'
url = URI("https://{s3-bucket-url}}.amazonaws.com")
http = Net::HTTP.new(url.host, url.port)
request = Net::HTTP::Post.new(url)request["content-type"] = 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW'request["Content-Type"] = 'application/x-www-form-urlencoded'request.body = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"policy\"\r\n\r\n{{policy}}\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"key\"\r\n\r\n{{key}}\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"x-amz-signature\"\r\n\r\n{{x-amz-signature}}\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"x-amz-algorithm\"\r\n\r\n{{x-amz-algorith}}\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"x-amz-date\"\r\n\r\n{{x-amz-date}}\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"x-amz-credential\"\r\n\r\n{{x-amz-credential}}\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"success_action_status\"\r\n\r\n201\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"success_action_redirect\"\r\n\r\n''\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"file\"; filename=\"{{fileName}}.mp4\"\r\nContent-Type: video/mp4\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"
response = http.request(request)puts response.read_body