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);
}
public static async Task UploadFile()
{
FileStream fs = File.OpenRead("{{filePath}}");
var _httpClient = new HttpClient();
var form = new MultipartFormDataContent();
form.Add(new StringContent("{{policy}}"), "policy");
form.Add(new StringContent("{{key}}"), "key");
form.Add(new StringContent("{{x-amz-signature}}"), "x-amz-signature");
form.Add(new StringContent("{{x-amz-algorithm}}"), "x-amz-algorithm");
form.Add(new StringContent("{{x-amz-date}}"), "x-amz-date");
form.Add(new StringContent("x-amz-credential"), "x-amz-credential");
form.Add(new StringContent("201"), "success_action_status");
form.Add(new StringContent(""), "success_action_redirect");
form.Add(new StreamContent(fs), "file", Path.GetFileName("{{filePath}}"));
var response = await _httpClient.PostAsync("{{uploadLink}}", form);
if ((int)response.StatusCode == 201)
{
Console.WriteLine("upload successful");
}
else
{
var responseContent = await response.Content.ReadAsStringAsync();
Console.WriteLine($"Error : {responseContent}");
}
}
$ pip install requests-toolbelt
import json
import requests
from 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'
require 'json'
# this files shows both step 1 (obtaining credentials) and step 2 (uploading file)
# step 1 (obtaining credentials)
url = URI("https://dev.vdocipher.com/api/videos?title=title&folderId=root")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Put.new(url)
request["Authorization"] = 'Apisecret _______'
response = http.request(request)
responseObj = JSON.parse(response.read_body)
# this value should be saved in your DB
puts responseObj['videoId']
# step 2 (uploading file)
url = URI(responseObj['clientPayload']['uploadLink'])
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
clientPayload = responseObj['clientPayload'];
request = Net::HTTP::Post.new(url)
form_data = [
['policy', clientPayload['policy']],
['key', clientPayload['key']],
["x-amz-signature", clientPayload['x-amz-signature']],
["x-amz-algorithm", clientPayload['x-amz-algorithm']],
["x-amz-date", clientPayload['x-amz-date']],
["x-amz-credential", clientPayload['x-amz-credential']],
['success_action_status', '201'],
['success_action_redirect', ""],
['file', File.open('./upload.rb')],
]
request.set_form form_data, 'multipart/form-data'
response = http.request(request)
puts response.read_body