Generate presigned URL - Python
You have the option to create presigned URL, allowing you to upload your recordings to AWS, Azure, or GCP securely. presigned URL provide a secure and temporary way to upload objects to your cloud storage. Instead of sharing your entire credentials, you can create a special URL that allows uploading a specific file for a limited time.
important
Code Sample
This guide will walk you through the process of generating a presigned URL for uploading recordings to your cloud storage of choice. You can use this generated presigned URL in Start Recording API.
- AWS
- AZURE
- GCP
const { S3Client, PutObjectCommand } = require("@aws-sdk/client-s3");
const { getSignedUrl } = require("@aws-sdk/s3-request-presigner");
import fetch from 'node-fetch';
const region = <YOUR S3 REGION>;
const bucket = <YOUR S3 BUCKET NAME>;
const key = <YOUR FILE NAME>; // test/abc.mp4
const accessKeyId = <YOUR S3 ACCESS KEY ID>;
const secretAccessKey = <YOUR S3 SECRET ACCESS KEY>;
const expiresIn = <URL EXPIRY IN SECONDS>; // this number should be higher than duraion of recording
const s3Client = new S3Client({
credentials: {
accessKeyId: accessKeyId,
secretAccessKey: secretAccessKey,
},
region: region,
});
const createPresignedUrlWithClient = async () => {
const command = new PutObjectCommand({ Bucket: bucket, Key: key });
const preSignedUrl = await getSignedUrl(s3Client, command, { expiresIn: expiresIn });
return preSignedUrl;
};
// you can use this presigned url like this
async function main() {
let preSignedUrl = await createPresignedUrlWithClient();
const options = {
method: "POST",
headers: {
Authorization: "$YOUR_TOKEN",
"Content-Type": "application/json",
},
body: JSON.stringify({
roomId: "abcd-efgh-ijkl",
preSignedUrl: preSignedUrl
}),
};
const url = `https://api.videosdk.live/v2/recordings/start`;
const response = await fetch(url, options);
const data = await response.json();
console.log(data);
}
main();
const { BlobServiceClient, BlobSASPermissions } = require("@azure/storage-blob");
import fetch from 'node-fetch';
const connectionString = <YOUR AZURE STORAGE CONNECTION STRING>;
const containerName = <YOUR AZURE CONTAINER NAME>;
const blobName = <YOUR AZURE BLOB NAME>;
const startsOn = <YOUR SASURL START TIME>; // (optional, defaults to now)
const expiresOn = <YOUR SASURL EXPIRY TIME>; // expiry time should be higher than duration of recording
const generateSASUrl = async () => {
let blobServiceClient = BlobServiceClient.fromConnectionString(connectionString);
let containerClient = blobServiceClient.getContainerClient(containerName);
await containerClient.createIfNotExists({
access: "container",
});
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
const sasUrl = await blockBlobClient.generateSasUrl({
startsOn: startsOn, // sasUrl start time
expiresOn: expiresOn, // sasUrl expiration time
permissions: BlobSASPermissions.parse("write"),
});
return sasUrl;
};
// you can use this presigned url like this
async function main() {
let preSignedUrl = await generateSASUrl();
const options = {
method: "POST",
headers: {
Authorization: "$YOUR_TOKEN",
"Content-Type": "application/json",
},
body: JSON.stringify({
roomId: "abcd-efgh-ijkl",
preSignedUrl: preSignedUrl
}),
};
const url = `https://api.videosdk.live/v2/recordings/start`;
const response = await fetch(url, options);
const data = await response.json();
console.log(data);
}
main();
const { Storage } = require("@google-cloud/storage");
import fetch from 'node-fetch';
const projectId = <YOUR GCP PROJECT ID>;
const clientEmail = <YOUR GCP CLIENT EMAIL>;
const privateKey = <YOUR GCP PRIVATE KEY>;
const expires =<URL EXPIRY IN SECONDS>; // this number should be higher than duraion of recording
const bucketName = <YOUR GCP BUCKET NAME>;
const fileName = <YOUR FILE NAME>; // test/abc.mp4
const storage = new Storage({
projectId: projectId,
credentials: {
client_email: clientEmail,
private_key: privateKey,
},
});
const generateSignedUrl = async () => {
const options = {
version: "v4",
action: "write",
expires: expires,
contentType: "video/mp4",
};
const [signedUrl] = storage.bucket(bucketName).file(fileName).getSignedUrl(options);
return signedUrl;
};
// you can use this presigned url like this
async function main() {
let preSignedUrl = await generateSignedUrl();
const options = {
method: "POST",
headers: {
Authorization: "$YOUR_TOKEN",
"Content-Type": "application/json",
},
body: JSON.stringify({
roomId: "abcd-efgh-ijkl",
preSignedUrl: preSignedUrl
}),
};
const url = `https://api.videosdk.live/v2/recordings/start`;
const response = await fetch(url, options);
const data = await response.json();
console.log(data);
}
main();
API Reference
The API references for all the methods utilized in this guide are provided below.
Got a Question? Ask us on discord