title: How to enforce unique filenames on Google Drive?
body: <p>I'm using Google Drive to serve as a central place for various scripts to store common files. However, I've noticed that despite pretending to function like a filesystem, Google Drive does not observe normal filesystem conventions, such as enforcing unique filenames. This is allowing different users to upload separate files with identical filenames.</p>
  
  <p>Is there any way to prevent this?</p>
  
  <p>My scripts all use the same code to access the Drive API to upload files:</p>
  
  <pre><code>import os
  import httplib2
  from apiclient import discovery, errors
  from apiclient.http import MediaFileUpload
  
  credentials = get_google_drive_credentials()
  service = discovery.build('drive', 'v2', http=credentials.authorize(httplib2.Http()))
  
  dir_id = 'google_drive_folder_hash'
  filename = '/some/path/blah.txt'
  service.files().insert(
      body={
          'title': os.path.split(filename)[-1],
          'parents': [{'id': dir_id}],
      },
      media_body=MediaFileUpload(filename, resumable=True),
  ).execute()
  </code></pre>
  
  <p>If two or more scripts ran this, and assuming no race conditions, why would this code result in duplicate uploads of "blah.txt"? How would I prevent this?</p>