We are aware of the issue with the badge emails resending to everyone, we apologise for the inconvenience - learn more here.

Forum Discussion

MorgsCode's avatar
MorgsCode
Explorer | Level 3
6 years ago

Setting Metadata on files - Python API

Hello! 

I've just started using the Dropbox Python API, but am a bit confused on how to add custom metadata to new or exists files. 

I've tried to defined my own PropertyGroup with the data stored in a PropertyField (as shown in the code below). However, passing this as a parameter of dbx.files_upload() causes the process to freezes up.  Should the template_id of the property group be defined differently? 

 

data =  dropbox.file_properties.PropertyField('foo', 'bar')

my_property_group = dropbox.file_properties.PropertyGroup()

my_property_group .template_id = '/|test_template'
my_property_group .fields = [data]

with open(r'C:\test_file.txt', "rb") as f:
    dbx.files_upload(f.read(), '/test_file.txt', mute = True, property_groups=[my_property_group ])

Any help on this would be much appreciated! 

 

  • Greg-DB's avatar
    Greg-DB
    6 years ago

    Thanks for following up. I'm glad to hear you already sorted most of this out.

    • The PropertyType.other option is just a catch-all for forwards compatibility with any potential new PropertyType fields. It's not actually something you can use as an alternative to PropertyType.string. Currently the Dropbox API only supports the string type. What other data type in particular would you be looking to use? I'll be happy to pass it along as a feature request.
    • You can change the property values on an existing file without re-uploading it. To do so, you would use the file_properties_properties_update method and/or the file_properties_properties_overwrite method. (Be sure to read the documentation for information on the difference between the two.)
  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Staff rankDropbox Staff

    Thanks for the report. I'll be happy to help with this, but I could use a few points of clarification:

    • What do you mean exactly when you say "the process to freezes up"? How are you checking this and what exactly do you see? Also, depending on the size of the file and your network connection, it may take some time to upload, so make sure you give it enough time to complete.
    • Is '/|test_template' the actual value you're sending, or is that just something you put there for this post in order to not post the actual ID? Actual template ID values should start with "ptid:", e.g., as returned by file_properties_templates_add_for_user or file_properties_templates_list_for_user

    I also notice that you're not using the return value of 'dbx.files_upload'. You may want to print or save that value somewhere for reference. If the call succeeds, it will contain the metadata of the uploaded file.

    • MorgsCode's avatar
      MorgsCode
      Explorer | Level 3

      Thanks for pointing a few things out that I seem to have overlooked. The freezing up, ended up being due to wrong formatting and an incorrect unique id for the custom string template - now sourced correctly using the following code: 

      template = dropbox.file_properties.PropertyFieldTemplate(name="MyTemplate", description="description",type=dropbox.file_properties.PropertyType.string)
      my_template_id = dbx.file_properties_templates_add_for_user("MyTemplate","description",[template]).template_id
      
      
      data =  dropbox.file_properties.PropertyField(name='MyTemplate', value='foobar')
      • Regarding the PropertyFieldTemplate the available types seems to be either 'string' or 'other'. Just curious, how you would use the PropertyType 'other' to store data?
      • Can property group data be altered on uploaded files or does it have to be added as part an upload process? 
      • Greg-DB's avatar
        Greg-DB
        Icon for Dropbox Staff rankDropbox Staff

        Thanks for following up. I'm glad to hear you already sorted most of this out.

        • The PropertyType.other option is just a catch-all for forwards compatibility with any potential new PropertyType fields. It's not actually something you can use as an alternative to PropertyType.string. Currently the Dropbox API only supports the string type. What other data type in particular would you be looking to use? I'll be happy to pass it along as a feature request.
        • You can change the property values on an existing file without re-uploading it. To do so, you would use the file_properties_properties_update method and/or the file_properties_properties_overwrite method. (Be sure to read the documentation for information on the difference between the two.)

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

5,877 PostsLatest Activity: 12 months ago
325 Following

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X or Facebook.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!