Google Photos API and Python | How To Upload Media Items (Photos, Videos) (Part 4)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to part 4 of the Google Photos API to to series my name is Jay this lesson is going to be the continuation from the previous video from the last video I cover basically every single method except the bad screen method and the batch cream my face used to upload media items to your Google accounts and the reason why I decided to make that cream effort as its own video is because uploading media items requires a couple steps and it may take more than 10 20 minutes to go to the material now let's just take a look at the documentation if we go to batch create and if we click on this uploading media link and right here is the upload process so to upload your media item you'll first need to upload the route bytes to a Google server and this that does not meaning that your image has been uploaded to your Google account I'm ready once you upload the image and Google's going to return a token based on just request that you set o API call you sense and once you have the the token ID you can take that token ID and oops let me go back you can take that token ID and if we go to media items magically we can pass the media ID to do some simple media either an option if you look at the the request body and waiting simple media item object it is asking for upload token let's see so oh so here are all the available media types the Google server accepts so for image files such as photos it takes basically just about every single format jpg PNG and gif file and even some raw files and for videos you can upload pretty much just about every single video format so mp4 is the most popular format these days and MOV that's also not a popular one and the rest of video formats I'm never used in before and below awesome just take samples again noticing that there's no pie style option available how we know we can make a REST API code and that's what going to do now that's if someone to cover in terms of documentation now let's go back to Python and this can be bad create method so like I said before to be able to upload image throughout Google photo account we need to make a REST API call so here I'm going to import the pay code library and the request is library so this call me step number one upload bytes data to Google server let me open my folder real quick [Music] so my images are saved on the set under this image to upload folder and see some country where be able to store the image for the dua tree and the folder name is image images to upload if you need to have to upload your out if you don't know how to fund the upload yeah so if you go to the upload media documentation and on the rest so here's the basically the headers and I need to provide to make an API call and we're going to send a post request to this link right here it's also base address hope so I also need to import OS module and you need to basically go about talking ID so I'll load the token file and which is this token file generate we authenticate the permission given permission to the app and I need to open the token file I need to read the data as binary and here I need to clear my head so here is just this information right so for authorization it will be beer space plus the token and content type it will be application and Oct et string and for the protocol X go go goog upload protocol it's gonna be raw format and that's everything I need from the header now I need image file some come to this call this object image file and I know from the English the battery available and that's the folder against our point the my image to upload photo and let me pick a fuller so as I want to upload this a corgi image so just this image right here I'm going to grab the file name and if we go back to the documentation we also need to provide header it's not hit our family at this point you have the option to change the file name when you upload the image but usually I'd like to keep my image name or file name to be the same so I mean insert new property value so this will be X X Google upload file name and this can be a new family it sure let's do it so here I'm going to name the file name let's do change too kuma the Kogi just the Kogi's name dot JPEG so to this point well just actually making trying to construct the the rest api headers and she hopes it is now see those takes to prompt Oh so here I made a typo okay so me try again so highlight line tool to to 215 inch from the statements you know input image file and panda then we had a property value now we need to open image because here we only have the image file to a tree and now we need to open the image file as binary now we have everything we need to make an API request Co just nice name the let's name the the output response I will do a post API code and upload your data is gonna be our image and the headers it's gonna be our headers Appa yes oh so I forgot the s now if I execute line 221 oh I forgot execute 229 if I ask you lyin 221 okay mmm that went to successfully now if I premise bounce I'm going to get o for one so that means I need to let me restart the session let me go back so just we round the the statements again you're supposed okay now this one I get response 200 so I was trying to attempt to use the the same session from last night by forgot ah after couple hours Google Silva will disconnect you but now get response 200 in that means the postcode was was able to ask you successfully now if I prayer response da Compton and that's going to return the token and ID in bytes format and I still need to convert this to just the puppet the published string type so let's do that mmm actually I can do that later but now we have the token ID now we can upload the image or the image item using the batch create method so here me make a request body and if I go back founder Chris body who can have album ID so this one is optional you want to upload media items to an elbow you can provide an elbow ID and we have new media items and within the new media items option we need to provide new media item object so when it's proclaim is singular in the album position that's the older way we want to insert the image now we know the first object is new media items and cumulus and we can provide description 12 image whom on the Kogi photos are other properties it will be simple media item and we need to provide the upload token and from response object contents we need to decode that to UTF - 8 now if we send so now if we make the tape like up so service media items tap batch create and the body will be in crossbody that ask you now if I just run this to statements that know best kre-o batch create and here's another type of decode let me try again so ask you all right no da da you mean Scot upload successfully now if I print the upload response here's the upload token that we initially provide and here's the status so it's printer status see it's me media ident results gets so you won't get socialist and it's coming the first item all right he'll get the message success that means the image guy upload successfully now if I go back and go to my photos and press f5 to refresh yeah she research by upload dates so recently add and here's our Corgi photo so if I go into image and click on info me I'll click that back and here's the mmm come on JPEG I was in this the image I upload so meaty needs photo and we upload the image again if i refresh the page alright so here's the image I upload today I guess with Google photos maybe you cannot have duplicate file name but here if we look at the new file name so here when you rename the file to kuma the Kogi dot JPEG here's the description come on the Kogi and as well as the metadata associated to this English now that's how you upload an image using a REST API along with the bad screen method now if want to ease the upload process then we can write a function to upload image to Google server so to convert the entire statement to a function let's create a function and we'll call this an upload image and I'm going to have three parameters in that you have in upload file name so it's gonna be the name that you want to use when you upload the file and your token ID so will you have the headers because we are putting everything inside function so can we don't have to insert a new property value twelve headers dictionary and the file name will be coming from upload file name and right here we're going to repeat the same process again so we'll grab these two statements I can just pop in pace I'm going to print the upload token come to decode I would and will return the response right now here's our function so if we need to upload multiple images so here can pair a container which is gonna be loose and the girl should I need that URL see yep I do so make an API REST API code so she mean craft the upload girl and token talk up these three lines just in case all right now let's say I want to upload these are this sunset photo and this and as well as this are Tokyo Sky Tower image so just two are just these two photos so I need to create two images so image so be image one this condition let's call this one Sky Tower English cinders wasn't a list API code so upload image and will provide image path and if I want to rename the file I can given your name so sky tower Tokyo Sky Tower by forum to just use the the family and we can use the based in function from the OS macho and which I'll show you when we upload the second image so we also need to provide the token it will append the token ID to the token object should this should be tokens not token tokens I will grab the token so that come turns dqo Beauty utf-8 and that's the first image and the second image is is the sunset photo so I'll grab the file name sure I'll do this I'll use the oil stop have top base name and to grab the file name so if we just print this highlighted statement and based in not base its name this function is going to English oh oops sighs I need to provide the fact that you found me oh okay so uh so this will be sunset dot jpg so if I make a second response code second dress API code upload image and should be image sunset and the file name is going to be always top half top base name and will provide the image some sets directory so this one should give us just some set oops image size naughty file let me try again and the piston function will return sunset dot jpg removing or removing the the folder path and I will just make a copy of these just Lorenza now if I ask you this block of statements right here and tokens nada fine oh can you see so nice tall begin so he'll have the token image file response lt-col oh here I have a typo inside my function so that was causing the issue okay now let me try again so if I just around these two statements and the first one SQ okay and the second wise missing the token oh so he I've got two about two so for the second image which the the sunset file why create a function I why use the function I forgot to provide the token op job let me try again okay that looks pretty good now if I printed tokens Bob Joe and within token lists safe I print the total number items o elements M comes get three because before I we upload the same image twice now so let's do this I'm going to we stop saying from from scratch so have the function now if I rerun the highlighted statement now both image got upload to to the Google server if I burn so you face the Len function to print how many elements are in the token op-shop now I get to and we know the batch kriya can take Alice so here I'm going to create another logical media items and we're going to use this convention and basically to make a list up job that contains all the the simple media items object and this will be upload talking okay in here I can say for token and tokens if I print new media item items no ID new media items oops media items and that's going to give me a list with all the simple media items put together so inside a single list now we have tests so can just make a request body and this will be new media oops media items and will provide a new media items list yeah that's it no can upload to Google photo account okay so here we're not getting any air back so if I go back and search for my image from the most recently ad i refresh my page and I miss my photo hmm you see all right so I'm going to pause real quick and I'll come back I'm going to I'm going to troubleshoot to figure out I was going out back some back and so I figure out the issue here let me delete this food to me t did these two photos so the issue was the inside the function and before hours using image see if only I created the image object make sure you opening the just an image file and the image file is nowhere to be found within the function and it should be image on the scope half mm in the open method will look for the image pad and opened the image file and I stored in a binary so that was the issue and by once I fixed that everything works just fine so let's try again so we fixed the so I fixed the function now if I rerun the entire statement and now the whole upload process is complete and before I delete those two photos like if i refresh my album Oh album my accounts and here's the sky towel image as well as the sunset image so this concludes the media islands resource lesson for next video I'll cover how to use the cher album resource
Info
Channel: Jie Jenn
Views: 4,107
Rating: undefined out of 5
Keywords: Google Photos API Tutorial, Google Photos API in Python, Google Photos API with Python, Python Application Development, Google Cloud and Python, GCP and Python, Google Photos API MediaItem Resource Tutorial, Upload image Google Photos API, Upload video Google Photos API, upload media Google Photos API
Id: wwAwG-SEaRM
Channel Id: undefined
Length: 29min 52sec (1792 seconds)
Published: Tue Jan 21 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.