C2- Transcode/Optimize Gif & Upload Your AWS S3 Bucket

FREEMIUM
By ozgurcagdas | Updated a month ago | Video, Images
Health Check

N/A

Back to All Tutorials (2)

Axios example for all OPTION2 group (including this API)

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//--------------READ ME---------------------------------------------------------
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

/*
this is “OPTION2” in postman collection.

“B” APIs are for upload, download and delete
This group help to upload original file to s3
Download securely from s3 (so you can sell as digital product or broadcast like netflix)
Delete file

b1 = generate upload url
// b2 outside of our app, it is directly upload to s3
b3 = generate download url
b4 = delete API

“C” APIs are for transcode and upload to s3 (it is done in same api with single call)

c1 = photo
c2 = animated phot (gif)
c3 = audio
c4 = mp4 video
c5 = hls video
c6 = mix

cloudCreadentials REQUIRED
filesList REQUIRED
thenOrCatchProcess REQUIRED

check documantation for more details:
https://documenter.getpostman.com/view/7262582/2s9YJW5m6Q

for AWS creadentials:
AWS IAM policy to process delete, upload and download operations are these:
(for the “Principle of least privilege”, just give these rights.)
{
“Version”: “2012-10-17”,
“Statement”: [
{
“Sid”: “VisualEditor0”,
“Effect”: “Allow”,
“Action”: [
“s3:PutObject”, // required for all of the “C” APIs and B1 API
"s3:GetObject", // required only for “B3” API. If you won’t use “B3”, then no need it
"s3:DeleteObject" // required only for “B4” API. If you won’t use “B4”, then no need it
],
“Resource”: “*” // or just name here your bucket for the least privilege.
}
]
}

*/

“use strict”;

const axios = require(“axios”);

const {
AWS_S3_ACCESS_KEY_ID,
AWS_S3_SECRET_ACCESS_KEY,
AWS_S3_REGION,
AWS_S3_BUCKET_NAME,
AWS_S3_FOLDER_NAME,

YOUR_CALLBACK_URL, // your callback endpoint url like “https://your-backend.com/your-endpoint

X_RAPID_API_KEY_a1, // your rapid api password for a1 …
X_RAPID_API_KEY_a2,
X_RAPID_API_KEY_a3,
X_RAPID_API_KEY_a4,
X_RAPID_API_KEY_a5,
X_RAPID_API_KEY_b1,
X_RAPID_API_KEY_b3,
X_RAPID_API_KEY_b4,
X_RAPID_API_KEY_c1,
X_RAPID_API_KEY_c2,
X_RAPID_API_KEY_c3,
X_RAPID_API_KEY_c4,
X_RAPID_API_KEY_c5,
X_RAPID_API_KEY_c6,

X_RAPID_API_HOST_a1,
X_RAPID_API_HOST_a2,
X_RAPID_API_HOST_a3,
X_RAPID_API_HOST_a4,
X_RAPID_API_HOST_a5,
X_RAPID_API_HOST_b1,
X_RAPID_API_HOST_b3,
X_RAPID_API_HOST_b4,
X_RAPID_API_HOST_c1,
X_RAPID_API_HOST_c2,
X_RAPID_API_HOST_c3,
X_RAPID_API_HOST_c4,
X_RAPID_API_HOST_c5,
X_RAPID_API_HOST_c6,
} = require("./testConfig");

const cloudCreadentials = [
{
cloudProvider: “AWS_S3”, // Azure Blob support will come
AWS_S3_ACCESS_KEY_ID: AWS_S3_ACCESS_KEY_ID,
AWS_S3_SECRET_ACCESS_KEY: AWS_S3_SECRET_ACCESS_KEY,
AWS_S3_REGION: AWS_S3_REGION,
},
];

const options_b1 = {
method: “POST”,
url: “https://” + X_RAPID_API_HOST_b1 + “/apifsp/transcoder/generate_signed_urls”,
headers: {
“Content-Type”: “application/json”,
“X-RapidAPI-Key”: X_RAPID_API_KEY_b1,
“X-RapidAPI-Host”: X_RAPID_API_HOST_b1,
},
data: {
cloudCredentials: cloudCreadentials,
signedUrlData: [
{
Bucket: AWS_S3_BUCKET_NAME,
Key: “user1/orginal-image3.jpeg”,
ContentType: “image/jpeg”,
commandName: “putObject”,
extras: {
fileId: 1,
your_optinal_key: “this extras is for your tracking. add here id or that kind of tracking info if you want (optional)”,
},
},
],
},
};
// b2 is for AWS upload, it is not directly related with our app.

const options_b3 = {
method: “POST”,
url: “https://” + X_RAPID_API_HOST_b3 + “/apifsp/transcoder/generate_signed_urls”,
headers: {
“Content-Type”: “application/json”,
“X-RapidAPI-Key”: X_RAPID_API_KEY_b3,
“X-RapidAPI-Host”: X_RAPID_API_HOST_b3,
},
data: {
cloudCredentials: cloudCreadentials,
signedUrlData: [
{
Bucket: AWS_S3_BUCKET_NAME,
Key: “user1/orginal-image3.jpeg”,
commandName: “getObject”,
extras: {
id: 1,
your_key: “this extras is for your tracking. add here id or that kind of tracking info if you want”,
},
},
],
},
};
const options_b4 = {
method: “POST”,
url: “https://” + X_RAPID_API_HOST_b4 + “/apifsp/transcoder/delete_files”,
headers: {
“Content-Type”: “application/json”,
“X-RapidAPI-Key”: X_RAPID_API_KEY_b4,
“X-RapidAPI-Host”: X_RAPID_API_HOST_b4,
},
data: {
cloudCredentials: cloudCreadentials,
filesListForDelete: [
{
Bucket: AWS_S3_BUCKET_NAME,
Key: “user1/photo-xl.webp”,
},
],
},
};

const options_c1 = {
method: “POST”,
url: “https://” + X_RAPID_API_HOST_c1 + “/apifsp/transcoder/upload-to-aws-s3/photo”,
headers: {
“Content-Type”: “application/json”,
“X-RapidAPI-Key”: X_RAPID_API_KEY_c1,
“X-RapidAPI-Host”: X_RAPID_API_HOST_c1,
},
data: {
cloudCredentials: cloudCreadentials,
thenOrCatchProcess: {
callbackEndpointUrl: YOUR_CALLBACK_URL,
callbackHeaders: {
“x-any-of-your-callback-header”: “any value…”,
},
callbackDataExtras: {
uploaderProfileId: 10074,
uploadType: “PUBLIC”,
fileId: “a44”,
anyKey: “you can add any value for your callback like this…”,
},
},
filesList: [
{
sourceInfo: {
sourceFileFormat: “PHOTO”,
sourceUrl: “https://picsum.photos/5000/3000.jpg”,
sourceFileExtension: “jpg”,
},
targetInfo: {
s3Bucket: AWS_S3_BUCKET_NAME,
s3FolderName: AWS_S3_FOLDER_NAME,
targetVersions: [
{
compressType: “PHOTO_WEBP”,
compressQuality: 60,
targetFileNameWithoutExtension_photo: “photo-xl”,
maxWidth: 2000,
maxHeight: 1500,
},
],
extras: {
originalFileName: “my-original-image-name.jpg”,
fileId: 1,
c20_file_upload_metadata: {},
anyKeyForCallback: “This is file level extras for your callback.”,
},
},
},
],
extras: {
requestTrackingId: “track_id_1”,
anyKeyForYourCallback: “This is request level extras to track your callback. We directly pass these to your callback in the ‘thenOrCatchProcess’. (There is also another ‘extras’ in ‘filesList’ for file-level data tracking.)”,
},
},
};
const options_c2 = {
method: “POST”,
url: “https://” + X_RAPID_API_HOST_c2 + “/apifsp/transcoder/upload-to-aws-s3/animated-photo”,
headers: {
“Content-Type”: “application/json”,
“X-RapidAPI-Key”: X_RAPID_API_KEY_c2,
“X-RapidAPI-Host”: X_RAPID_API_HOST_c2,
},
data: {
cloudCredentials: cloudCreadentials,
thenOrCatchProcess: {
callbackEndpointUrl: YOUR_CALLBACK_URL,
callbackHeaders: {
“x-any-of-your-callback-header”: “any value…”,
},
callbackDataExtras: {
uploaderProfileId: 10074,
uploadType: “PUBLIC”,
fileId: “a44”,
anyKey: “you can add any value for your callback like this…”,
},
},
filesList: [
{
sourceInfo: {
sourceFileFormat: “ANIMATED_IMAGE”,
sourceUrl: “https://devcontentfilespublic.fullsepp.tv/_0test/color-gif-example.gif”,
sourceFileExtension: “gif”,
},
targetInfo: {
s3Bucket: AWS_S3_BUCKET_NAME,
s3FolderName: AWS_S3_FOLDER_NAME,
targetVersions: [
{
compressType: “ANIMATED_PHOTO_WEBP”,
compressQuality: 50,
targetFileNameWithoutExtension_animatedPhoto: “my-animated-image”,
maxWidth: 700,
maxHeight: 700,
},
],
},
extras: null,
},
],
extras: {
requestTrackingId: “track_id_1”,
anyKeyForYourCallback: “This is request level extras to track your callback. We directly pass these to your callback in the ‘thenOrCatchProcess’. (There is also another ‘extras’ in ‘filesList’ for file-level data tracking.)”,
},
},
};
const options_c3 = {
method: “POST”,
url: “https://” + X_RAPID_API_HOST_c3 + “/apifsp/transcoder/upload-to-aws-s3/audio”,
headers: {
“Content-Type”: “application/json”,
“X-RapidAPI-Key”: X_RAPID_API_KEY_c3,
“X-RapidAPI-Host”: X_RAPID_API_HOST_c3,
},
data: {
cloudCredentials: cloudCreadentials,
thenOrCatchProcess: {
callbackEndpointUrl: YOUR_CALLBACK_URL,
callbackHeaders: {
“x-any-of-your-callback-header”: “any value…”,
},
callbackDataExtras: {
uploaderProfileId: 10074,
uploadType: “PUBLIC”,
fileId: “a44”,
anyKey: “you can add any value for your callback like this…”,
},
},
filesList: [
{
sourceInfo: {
sourceFileFormat: “AUDIO”,
sourceUrl: “https://devcontentfilespublic.fullsepp.tv/_0test/audio-test-jingle.mp3”,
sourceFileExtension: “mp3”,
},
targetInfo: {
s3Bucket: AWS_S3_BUCKET_NAME,
s3FolderName: AWS_S3_FOLDER_NAME,
targetVersions: [
{
compressType: “AAC_AUDIO”,
compressQuality: 60,
targetFileNameWithoutExtension_aacAudio: “transcodedFullAudio”,
},
],
},
extras: {
originalFileName: “audio-test-jingle.mp3”,
fileId: 250,
},
},
],
extras: {
requestTrackingId: “track_id_1”,
anyKeyForYourCallback: “This is request level extras to track your callback. We directly pass these to your callback in the ‘thenOrCatchProcess’. (There is also another ‘extras’ in ‘filesList’ for file-level data tracking.)”,
},
},
};
const options_c4 = {
method: “POST”,
url: “https://” + X_RAPID_API_HOST_c4 + “/apifsp/transcoder/upload-to-aws-s3/video-mp4”,
headers: {
“Content-Type”: “application/json”,
“X-RapidAPI-Key”: X_RAPID_API_KEY_c4,
“X-RapidAPI-Host”: X_RAPID_API_HOST_c4,
},
data: {
cloudCredentials: cloudCreadentials,
thenOrCatchProcess: {
callbackEndpointUrl: YOUR_CALLBACK_URL,
callbackHeaders: {
“x-any-of-your-callback-header”: “any value…”,
},
callbackDataExtras: {
uploaderProfileId: 10074,
uploadType: “PUBLIC”,
fileId: “a44”,
anyKey: “you can add any value for your callback like this…”,
},
},
filesList: [
{
sourceInfo: {
sourceFileFormat: “VIDEO”,
sourceUrl: “https://devcontentfilespublic.fullsepp.tv/_0test/video+(1080p).mp4”,
sourceFileExtension: “mp4”,
},
targetInfo: {
s3Bucket: AWS_S3_BUCKET_NAME,
s3FolderName: AWS_S3_FOLDER_NAME,
targetVersions: [
{
compressType: “MP4_VIDEO”,
targetFileNameWithoutExtension_mp4Video: “mp4_version720p_transcodedFullVideo”,
targetFileNameWithoutExtension_videoThumbsFullSize: “mp4_version720p_thumbMaxSize”,
targetFileNameWithoutExtension_videoThumbs240p: “mp4_version720p_thumb240p”,
maxWidth: 1280,
maxHeight: 1280,
},
],
},
extras: {
fileId: 100,
ANY_KEY: “value for callback…”,
},
},
],
extras: {
requestTrackingId: “track_id_1”,
anyKeyForYourCallback: “This is request level extras to track your callback. We directly pass these to your callback in the ‘thenOrCatchProcess’. (There is also another ‘extras’ in ‘filesList’ for file-level data tracking.)”,
},
},
};
const options_c5 = {
method: “POST”,
url: “https://” + X_RAPID_API_HOST_c5 + “/apifsp/transcoder/upload-to-aws-s3/video-hls”,
headers: {
“Content-Type”: “application/json”,
“X-RapidAPI-Key”: X_RAPID_API_KEY_c5,
“X-RapidAPI-Host”: X_RAPID_API_HOST_c5,
},
data: {
cloudCredentials: cloudCreadentials,
thenOrCatchProcess: {
callbackEndpointUrl: YOUR_CALLBACK_URL,
callbackHeaders: {
“x-any-of-your-callback-header”: “any value…”,
},
callbackDataExtras: {
uploaderProfileId: 10074,
uploadType: “PUBLIC”,
fileId: “a44”,
anyKey: “you can add any value for your callback like this…”,
},
},
filesList: [
{
sourceInfo: {
sourceFileFormat: “VIDEO”,
sourceUrl: “https://devcontentfilespublic.fullsepp.tv/_0test/video+(1080p).mp4”,
sourceFileExtension: “mp4”,
},
targetInfo: {
s3Bucket: AWS_S3_BUCKET_NAME,
s3FolderName: AWS_S3_FOLDER_NAME,
targetVersions: [
{
compressType: “HLS_VIDEO”,
targetFileNameWithoutExtension_hlsVideoChunk: “hls_version720p_videoChunk”,
targetFileNameWithoutExtension_videoThumbsFullSize: “hls_version720p_thumbMaxSize”,
targetFileNameWithoutExtension_videoThumbs240p: “hls_version720p_thumb240p”,
targetFileNameWithoutExtension_hlsVideoPlaylist: “hls_version720p_playlist”,
maxWidth: 1280,
maxHeight: 1280,
},
],
},
extras: {
originalFileName: “video (1080p).mp4”,
fileId: 1,
},
},
],
extras: {
requestTrackingId: “track_id_1”,
anyKeyForYourCallback: “This is request level extras to track your callback. We directly pass these to your callback in the ‘thenOrCatchProcess’. (There is also another ‘extras’ in ‘filesList’ for file-level data tracking.)”,
},
},
};
const options_c6 = {
method: “POST”,
url: “https://” + X_RAPID_API_HOST_c6 + “/apifsp/transcoder/transcode_and_upload_file”,
headers: {
“Content-Type”: “application/json”,
“X-RapidAPI-Key”: X_RAPID_API_KEY_c6,
“X-RapidAPI-Host”: X_RAPID_API_HOST_c6,
},
data: {
cloudCredentials: cloudCreadentials,
thenOrCatchProcess: {
callbackEndpointUrl: YOUR_CALLBACK_URL,
callbackHeaders: {
“x-any-of-your-callback-header”: “any value…”,
},
callbackDataExtras: {
uploaderProfileId: 10074,
uploadType: “PUBLIC”,
fileId: “a44”,
anyKey: “you can add any value for your callback like this…”,
},
},
filesList: [
{
sourceInfo: {
sourceFileFormat: “PHOTO”,
sourceUrl: “https://picsum.photos/5000/3000.jpg”,
sourceFileExtension: “jpg”,
},
targetInfo: {
s3Bucket: AWS_S3_BUCKET_NAME,
s3FolderName: AWS_S3_FOLDER_NAME,
targetVersions: [
{
compressType: “PHOTO_WEBP”,
compressQuality: 60,
targetFileNameWithoutExtension_photo: “photo-xl”,
maxWidth: 2000,
maxHeight: 1500,
},
{
compressType: “PHOTO_WEBP”,
compressQuality: 60,
targetFileNameWithoutExtension_photo: “photo-xs”,
maxWidth: 400,
maxHeight: 300,
},
],
extras: {
originalFileName: “my-original-image-name.jpg”,
fileId: 1,
c20_file_upload_metadata: {},
anyKeyForCallback: “This is file level extras for your callback.”,
},
},
},
{
sourceInfo: {
sourceFileFormat: “ANIMATED_IMAGE”,
sourceUrl: “https://devcontentfilespublic.fullsepp.tv/_0test/color-gif-example.gif”,
sourceFileExtension: “gif”,
},
targetInfo: {
s3Bucket: AWS_S3_BUCKET_NAME,
s3FolderName: AWS_S3_FOLDER_NAME,
targetVersions: [
{
compressType: “ANIMATED_PHOTO_WEBP”,
compressQuality: 50,
targetFileNameWithoutExtension_animatedPhoto: “my-animated-image”,
maxWidth: 700,
maxHeight: 700,
},
],
},
extras: null,
},
{
sourceInfo: {
sourceFileFormat: “VIDEO”,
sourceUrl: “https://devcontentfilespublic.fullsepp.tv/_0test/video+(1080p).mp4”,
sourceFileExtension: “mp4”,
},
targetInfo: {
s3Bucket: AWS_S3_BUCKET_NAME,
s3FolderName: AWS_S3_FOLDER_NAME,
targetVersions: [
{
compressType: “MP4_VIDEO”,
targetFileNameWithoutExtension_mp4Video: “mp4_version720p_transcodedFullVideo”,
targetFileNameWithoutExtension_videoThumbsFullSize: “mp4_version720p_thumbMaxSize”,
targetFileNameWithoutExtension_videoThumbs240p: “mp4_version720p_thumb240p”,
maxWidth: 1280,
maxHeight: 1280,
},
{
compressType: “MP4_VIDEO”,
targetFileNameWithoutExtension_mp4Video: “mp4_version1020p_transcodedFullVideo”,
targetFileNameWithoutExtension_videoThumbsFullSize: “mp4_version1020p_thumbMaxSize”,
targetFileNameWithoutExtension_videoThumbs240p: “mp4_version1020p_thumb240p”,
maxWidth: 1920,
maxHeight: 1920,
},
],
},
extras: {
fileId: 100,
ANY_KEY: “value for callback…”,
},
},
{
sourceInfo: {
sourceFileFormat: “VIDEO”,
sourceUrl: “https://devcontentfilespublic.fullsepp.tv/_0test/video+(1080p).mp4”,
sourceFileExtension: “mp4”,
},
targetInfo: {
s3Bucket: AWS_S3_BUCKET_NAME,
s3FolderName: AWS_S3_FOLDER_NAME,
targetVersions: [
{
compressType: “HLS_VIDEO”,
targetFileNameWithoutExtension_hlsVideoChunk: “hls_version720p_videoChunk”,
targetFileNameWithoutExtension_videoThumbsFullSize: “hls_version720p_thumbMaxSize”,
targetFileNameWithoutExtension_videoThumbs240p: “hls_version720p_thumb240p”,
targetFileNameWithoutExtension_hlsVideoPlaylist: “hls_version720p_playlist”,
maxWidth: 1280,
maxHeight: 1280,
},
{
compressType: “HLS_VIDEO”,
targetFileNameWithoutExtension_hlsVideoChunk: “hls_version1080p_videoChunk”,
targetFileNameWithoutExtension_videoThumbsFullSize: “hls_version1080p_thumbMaxSize”,
targetFileNameWithoutExtension_videoThumbs240p: “hls_vers1080p_thumb240p”,
targetFileNameWithoutExtension_hlsVideoPlaylist: “hls_version1080p_playlist”,
maxWidth: 1920,
maxHeight: 1920,
},
],
},
extras: {
originalFileName: “video (1080p).mp4”,
fileId: 1,
},
},
{
sourceInfo: {
sourceFileFormat: “AUDIO”,
sourceUrl: “https://devcontentfilespublic.fullsepp.tv/_0test/audio-test-jingle.mp3”,
sourceFileExtension: “mp3”,
},
targetInfo: {
s3Bucket: AWS_S3_BUCKET_NAME,
s3FolderName: AWS_S3_FOLDER_NAME,
targetVersions: [
{
compressType: “AAC_AUDIO”,
compressQuality: 60,
targetFileNameWithoutExtension_aacAudio: “transcodedFullAudio”,
},
],
},
extras: {
originalFileName: “audio-test-jingle.mp3”,
fileId: 250,
},
},
],
extras: {
requestTrackingId: “track_id_1”,
anyKeyForYourCallback: “This is request level extras to track your callback. We directly pass these to your callback in the ‘thenOrCatchProcess’. (There is also another ‘extras’ in ‘filesList’ for file-level data tracking.)”,
},
},
};

const testFuntion = async (options) => {
try {
console.log("options: ", options);
const response = await axios.request(options);
console.log(response.data);
} catch (error) {
console.error(error);
}
};

/*
test one by one to prevent limit access.

  // testFuntion(options_b1);
  // testFuntion(options_b3);
  // testFuntion(options_b4);
  // testFuntion(options_c1);
  // testFuntion(options_c2);
  // testFuntion(options_c3);
  // testFuntion(options_c4);
  // testFuntion(options_c5);
  // testFuntion(options_c6);

*/