upload: Moving locking code to earlier stage

To avoid races, it's important to get the lock as soon as possible.