Upload and replace image saved in google.drive and shown on the client

I am trying to upload an image into a google drive that is saved as the id I’m using from postgresSQL. If the user logs back in, and replaces their image, I want to delete the one that is saved in google drive and replace it with the new one uploaded. The user should only have one image at all times. Here’s my upload code that doesn’t work.

    self.profile_image.source = app_files.profile_photo.create_file(str(user_info_list[0]['anvil_id'])+".jpg")

There’s no error. It’s saved as the correct name but is showing up blank and looks like a word document in the google drive. Here’s how I’m viewing the image when the initially loads page loads:

 self.profile_image.source = app_files.profile_photo.get(str(user_info_list[0]['anvil_id'])+".png") or app_files.profile_photo.get(str(user_info_list[0]['anvil_id'])+".jpg") or app_files.profile_photo.get(str('01')+".jpg") 

Any ideas? Thanks

I’m not an expert in google files, but …

in your example, where is the actual media object that represents the file? It looks like you are passing a string.

Maybe that’s why it’s blank. The way I am trying to do this is by having the user click the File loader icon, and chooses a folder. Then I am trying to save it as a specific name (the string) and store it in the google drive so I can call it later when the user opens up the page. I don’t think I’m passing the media object into the google drive and perhaps that’s the reason it’s a blank document with the correct name (which is the string).

This is closer… still trying to figure it out though.

   c = FileLoader()  
    upload_folder = app_files.profile_photo
    for f in c.files:
      small_img = anvil.image.generate_thumbnail(f, 640)
      uploaded = upload_folder.create_file(f(str(user_info_list[0]['anvil_id'])))
      uploaded.set_media(small_img)

I’m still learning python (programming in general) so I’m kind of all over the place on this one.

From the docs :

You can also pass an optional second argument, providing the initial content of the file. This can either be a string, or a Media object. If it is a Media object, the new file is automatically created with the correct MIME type.

If possible, you should provide initial content when creating a file, rather than creating an empty file and then uploading its content. This way, if something goes wrong during upload then you won’t end up with a new empty file.

So swap the above for this :

self.profile_image.source = app_files.profile_photo.create_file(
   str(user_info_list[0]['anvil_id'])+".jpg",
   file_from_uploader
)
2 Likes

Thank you! So this works:

    self.profile_image.source = app_files.profile_photo.create_file(
      str(user_info_list[0]['anvil_id'])+".jpg",
      self.file_loader_1.file
    )

My last question is do you know what the the best way would be to delete the file with the name before it? I don’t want multiple files with the same name in google drive. Thank you for all the help!!!

Use one of these :

self.profile_image.source.trash()  # moves to trash folder
self.profile_image.source.delete() # permanently deletes the file

You would check for the presence of the file first, then delete it.

Do read that documentation link in my post above, most of this stuff is in there.

2 Likes

Doing it now, thank you

1 Like