Feb 18, 2023 01:32 AM - edited Feb 18, 2023 01:34 AM
So im trying to upload videos to youtube via an airtable database. Currently my application sends video files to a database in airtable and I want to build a script that sends the attachment to a specific URL. Im having trouble figuring out the script to query the URL and then how do I format it correctly so the PUT call will accept it.
Ive provided a screenshot of what my simple database looks like. I need to have the attachment in the "Videos" field be sent in the "<file contents here>" portion of the follow script:
Feb 18, 2023 01:38 AM
I tried to make some sample code and idk if this will help but its the start I have
// Airtable Script to Find a File in a Table and Upload it using Fetch API
let table = base.getTable("upload");
let query = await table.selectRecordsAsync();
// Find the file record in the "upload" table with the video file attachment
let record = query.records.find(record => record.getCellValue("Video") !== null);
// Check if a file record was found
if (record) {
// Get the file contents from the attachment field
let attachment = record.getCellValue("Video");
let url = attachment[0].url;
// Set the headers and request options for the fetch API
let myHeaders = new Headers();
myHeaders.append("Content-Type", "video/mp4");
let requestOptions = {
method: 'PUT',
headers: myHeaders,
body: await fetch(url).then(response => response.blob()),
redirect: 'follow'
};
// Make the fetch request to upload the video file to YouTube
fetch("https://www.googleapis.com/upload/youtube/v3/videos", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
}
else {
console.log("No file record found in the 'upload' table with a video attachment.");
}